<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Inside DealTakerhow-to » Inside DealTaker</title>
	<atom:link href="http://www.dealtaker.com/our-blog/tag/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dealtaker.com/our-blog</link>
	<description>Just another DealTaker.com site</description>
	<lastBuildDate>Mon, 13 Feb 2012 16:51:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Kohana PHP 3.0 (KO3) Tutorial Part 3</title>
		<link>http://www.dealtaker.com/our-blog/2009/12/30/kohana-php-3-0-ko3-tutorial-part-3/</link>
		<comments>http://www.dealtaker.com/our-blog/2009/12/30/kohana-php-3-0-ko3-tutorial-part-3/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 10:08:30 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[KO3]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=1873</guid>
		<description><![CDATA[Welcome to the third part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read the first and/or second parts yet, I would click here and read them before going on. In this tutorial we will be going over how to create a template. In the last tutorial, we [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the third part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read the <a href="http://www.dealtaker.com/our-blog/2009/11/20/kohana-php-3-0-ko3-tutorial-part-1/" target="_blank">first</a> and/or <a href="http://www.dealtaker.com/our-blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2/" target="_blank">second parts</a> yet, I would click here and read them before going on. In this tutorial we will be going over how to create a template.<br />
<span id="more-1873"></span><br />
In the last tutorial, we went over views and in this one we are going to extend the Controller classes which will allow us to create a template. A template, you might ask, is nothing more than a view that is more or less your base (X)HTML code. This will allow to keep things &#8220;DRY&#8221; in the view world. Before we get to putting code into a file, lets create a new directory under &#8220;/application/views/&#8221; named &#8220;templates&#8221;.  Now open up your favorite IDE and make a new file and put the following into it:<br />
[php]<br />
&lt;!DOCTYPE html PUBLIC &#8220;-//W3C//DTD XHTML 1.0 Transitional//EN&#8221;<br />
&#8220;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&gt;<br />
&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221; xml:lang=&#8221;en&#8221; lang=&#8221;en&#8221;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-type&#8221; content=&#8221;text/html; charset=UTF-8&#8243; /&gt;<br />
&lt;meta http-equiv=&#8221;Content-Language&#8221; content=&#8221;en-us&#8221; /&gt;<br />
&lt;title&gt;&lt;?php echo $title;?&gt;&lt;/title&gt;<br />
&lt;meta name=&#8221;keywords&#8221; content=&#8221;&lt;?php echo $meta_keywords;?&gt;&#8221; /&gt;<br />
&lt;meta name=&#8221;description&#8221; content=&#8221;&lt;?php echo $meta_description;?&gt;&#8221; /&gt;<br />
&lt;meta name=&#8221;copyright&#8221; content=&#8221;&lt;?php echo $meta_copywrite;?&gt;&#8221; /&gt;<br />
&lt;?php foreach($styles as $file =&gt; $type) { echo HTML::style($file, array(&#8216;media&#8217; =&gt; $type)), &#8220;n&#8221;; }?&gt;<br />
&lt;?php foreach($scripts as $file) { echo HTML::script($file, NULL, TRUE), &#8220;n&#8221;; }?&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;div id=&#8221;container&#8221;&gt;<br />
&lt;?php echo $header;?&gt;<br />
&lt;?php echo $content;?&gt;<br />
&lt;?php echo $footer;?&gt;<br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/php]<br />
Save the above as &#8220;default.php&#8221; in your &#8220;/application/views/templates/&#8221; folder.</p>
<p>As you can see the above pretty much looks like a view we have done before, a bit more expanded. I will go over the &#8220;foreach&#8221; a little bit later. Unlike a view, this will most likely be used by all of your project. Since we will point to this file for our template, or shell, this will reduce the amount of code you have to put in each view, keeping thing separated, easier to maintain, or basically &#8220;DRY&#8221;.</p>
<p>Now that we have a template, our system isn&#8217;t going to do anything with it until well tell it to use it. So lets go back to our IDE and create a new file and put the following into it:<br />
[php]<br />
&lt;?php<br />
defined(&#8216;SYSPATH&#8217;) or die(&#8216;No direct script access.&#8217;);</p>
<p>class Controller_DefaultTemplate extends Controller_Template<br />
{<br />
public $template = &#8216;templates/default&#8217;;</p>
<p>/**<br />
* Initialize properties before running the controller methods (actions),<br />
* so they are available to our action.<br />
*/<br />
public function before()<br />
{<br />
// Run anything that need ot run before this.<br />
parent::before();</p>
<p>if($this-&gt;auto_render)<br />
{<br />
// Initialize empty values<br />
$this-&gt;template-&gt;title            = &#8221;;<br />
$this-&gt;template-&gt;meta_keywords    = &#8221;;<br />
$this-&gt;template-&gt;meta_description = &#8221;;<br />
$this-&gt;template-&gt;meta_copywrite   = &#8221;;<br />
$this-&gt;template-&gt;header           = &#8221;;<br />
$this-&gt;template-&gt;content          = &#8221;;<br />
$this-&gt;template-&gt;footer           = &#8221;;<br />
$this-&gt;template-&gt;styles           = array();<br />
$this-&gt;template-&gt;scripts          = array();<br />
}<br />
}</p>
<p>/**<br />
* Fill in default values for our properties before rendering the output.<br />
*/<br />
public function after()<br />
{<br />
if($this-&gt;auto_render)<br />
{<br />
// Define defaults<br />
$styles                  = array(&#8216;assets/css/reset.css&#8217; =&gt; &#8216;screen&#8217;);<br />
$scripts                 = array(&#8216;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&#8217;);</p>
<p>// Add defaults to template variables.<br />
$this-&gt;template-&gt;styles  = array_reverse(array_merge($this-&gt;template-&gt;styles, $styles));<br />
$this-&gt;template-&gt;scripts = array_reverse(array_merge($this-&gt;template-&gt;scripts, $scripts));<br />
}</p>
<p>// Run anything that needs to run after this.<br />
parent::after();<br />
}<br />
}<br />
[/php]<br />
And save this as &#8220;defaulttemplate.php&#8221; under your &#8220;/application/classes/controller/&#8221; folder.</p>
<p>The above code extends the &#8220;Controller_Template&#8221; class and does three main things, initialize some properties (variables) to be accessable by our methods (actions) and appends default values to them and then gets associates them to our template variables before we render the final output to the screen. This is where the foreach() loop in the template comes into play. The foreach() loop uses a static method for the helper class &#8220;HTML&#8221; to <a href="http://v3.kohanaphp.com/guide/api/HTML#style" target="_blank">load CSS style sheets</a> and load JS files</a>. Both of these helper methods will iterate through an array and properly wrap it in the appropriate tag. They can take a path or a URL.</p>
<p>You might be wondering what a &#8220;Helper&#8221; is, so here is a quick definition from the Kohana PHP 2.x documents:</p>
<blockquote cite="http://docs.kohanaphp.com/general/helpers"><p>Helpers are simply &#8220;handy&#8221; functions that help you with development.</p>
<p>Helpers are similar to library methods, but there is a subtle difference. With a library, you have to create an instance of the library&#8217;s class to use its methods. Helpers are declared as static methods of a class, so there is no need to instantiate the class. You can think of them as &#8220;global functions&#8221;.</p>
<p>As with libraries, the helper classes are automatically loaded by the framework when they are used, so there is no need to load them yourself.</p></blockquote>
<p>OK, back on course. You might have noticed there is a reference to &#8220;assets/css/reset.css&#8221;, so lets go ahead and get that in place. In your root create a folder named &#8220;assets&#8221; and in that folder create one named &#8220;css&#8221;. For the actual &#8220;reset.css&#8221; file, I went to Serene Destiny and copied the code from the article titled &#8220;Create The Perfect CSS Reset&#8221; into a file and saved it as &#8220;reset.css&#8221; in the &#8220;assets/css/&#8221; folder. You might want to also set up some other folders within &#8220;/assets/&#8221;, maybe &#8220;images&#8221;, &#8220;js&#8221; and &#8220;files&#8221;. Basically the assets folder should be used to store and organize static files and such.</p>
<p>At this point, our application still doesn&#8217;t know what to do what we have done, so we need to modify our controller. So open &#8220;/application/classes/controller/ko3.php&#8221;.<br />
We want to change what class we are extending, so change the line:<br />
[php]class Controller_Ko3 extends Controller[/php]<br />
to:<br />
[php]lass Controller_Ko3 extends Controller_DefaultTemplate[/php]</p>
<p>We also need to change up our &#8220;index&#8221; action (action_index() method) to look like the following:<br />
[php]<br />
public function action_index()<br />
{<br />
$ko3_inner               = array();<br />
$ko3                     = array();<br />
$this-&gt;template-&gt;title   = &#8216;Kohana 3.0&#8242;;</p>
<p>View::set_global(&#8216;x&#8217;, &#8216;This is a global variable&#8217;);</p>
<p>$ko3_inner['content']    = &#8216;We have more data&#8217;;<br />
$ko3['content']          = &#8216;We have data&#8217;;<br />
$ko3['ko3_inner']        = View::factory(&#8216;blocks/ko3_inner&#8217;, $ko3_inner)<br />
-&gt;render();<br />
$this-&gt;template-&gt;content = View::factory(&#8216;pages/ko3&#8242;, $ko3);<br />
}<br />
[/php]<br />
Save it. Now you might noticed that we now have &#8220;$this-&gt;template-&gt;title   = &#8216;Kohana 3.0&#8242;;&#8221;, this will assign a value to our templates &#8220;title&#8221; variable. The next thing you might notice is the absence of the last &#8220;render()&#8221; method. The &#8220;factory()&#8221; method actually will &#8220;auto render&#8221; it to our template&#8217;s &#8220;content&#8221; variable. Pretty simple, yes?</p>
<p>There is something we should probably do before loading up the page, so load up the &#8220;ko3.php&#8221; view file located in &#8220;application/views/pages/&#8221;. You might noticed that we have all the &#8220;shell&#8221; code in our view, so lets remove it. What you should only have in the view file is this:<br />
[php]<br />
&lt;h1&gt;This is my first view&lt;/h1&gt;<br />
&lt;?php echo $content;?&gt;<br />
&lt;?php echo $ko3_inner; ?&gt;<br />
&lt;br/&gt;&lt;?php echo $x;?&gt;<br />
[/php]</p>
<p>Now if you load it up, you should see that page has a title of &#8220;Kohana 3.0&#8243; and the page should pretty much look the same as it did from the last tutorial. If you view the source thou, it would look much different. But you might be wondering about the other variables in the template and what to do with them. OK, let go back and edit our &#8220;index&#8221; action again. Make it look like:<br />
[php]<br />
public function action_index()<br />
{<br />
$ko3_inner                        = array();<br />
$ko3                              = array();<br />
$this-&gt;template-&gt;title            = &#8216;Kohana 3.0&#8242;;<br />
$this-&gt;template-&gt;meta_keywords    = &#8216;PHP, Kohana, KO3, Framework&#8217;;<br />
$this-&gt;template-&gt;meta_description = &#8216;A test of of the KO3 framework&#8217;;<br />
$this-&gt;template-&gt;styles           = array(&#8216;assets/css/red.css&#8217; =&gt; &#8216;screen&#8217;);<br />
$this-&gt;template-&gt;scripts          = array(&#8216;assets/js/jqtest.js&#8217;);</p>
<p>View::set_global(&#8216;x&#8217;, &#8216;This is a global variable&#8217;);</p>
<p>$ko3_inner['content']             = &#8216;We have more data&#8217;;<br />
$ko3['content']                   = &#8216;We have data&#8217;;<br />
$ko3['ko3_inner']                 = View::factory(&#8216;blocks/ko3_inner&#8217;, $ko3_inner)<br />
-&gt;render();<br />
$this-&gt;template-&gt;content          = View::factory(&#8216;pages/ko3&#8242;, $ko3);<br />
}<br />
[/php]<br />
Pretty simple. You will notice I didn&#8217;t fill in the header or footer. I&#8217;m sure you know what to do there. Hint: Render a view to that variable =). Another thing you might notice is that I have put in &#8220;assets/css/red.css&#8221; and &#8220;assets/css/jqtest.js&#8221;. Lets make those two files starting with &#8220;/assets/css/red.css&#8221;:<br />
[php]<br />
h1<br />
{<br />
color: #FF0000;<br />
}<br />
[/php]</p>
<p>Next &#8220;/assets/js/jqtest.js&#8221;:<br />
[php]<br />
$(&#8220;document&#8221;).ready(function()<br />
{<br />
alert(&#8216;Hello Kohana!&#8217;);<br />
});<br />
[/php]</p>
<p>Save them and refresh the site. You should see an alert pop up and the first line of text in red.</p>
<p>Today we have created a template file, extended a template controller and our controller use that template file. There is a lot of potential with what you could do with this already. So until next time when I will go over models, happy coding!<br />
Sources used: <a href="http://kerkness.ca/wiki/doku.php" target="_blank">Unofficial Kohana 3 Wiki</a>, <a href="http://docs.kohanaphp.com/" target="_blank">Kohana PHP 2.x Docs</a></p>
<p><a href="http://www.dealtaker.com/our-blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2/">Part 2</a> | <a href="http://www.dealtaker.com/our-blog/2010/02/01/kohana-php-3-0-ko3-tutorial-part-4/">Part 4</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2009/12/30/kohana-php-3-0-ko3-tutorial-part-3/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
	</channel>
</rss>

