<?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 DealTakerviews » Inside DealTaker</title>
	<atom:link href="http://www.dealtaker.com/our-blog/tag/views/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dealtaker.com/our-blog</link>
	<description>Just another DealTaker.com site</description>
	<lastBuildDate>Fri, 10 Feb 2012 14:56:55 +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 2</title>
		<link>http://www.dealtaker.com/our-blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2/</link>
		<comments>http://www.dealtaker.com/our-blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 10:47:54 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[KO3]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=1835</guid>
		<description><![CDATA[Welcome to the second part in this series on how to develop with Kohana PHP V3 (KO3). If you haven&#8217;t read the first part, I would read it before going on. In this tutorial we will be going over how to develop views. Before we get into views, we&#8217;ll want to update the KO3 code [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the second part in this series on how to develop with Kohana PHP V3 (<acronym title="Kohana PHP 3">KO3</acronym>). If you haven&#8217;t read the first part, I would <a href="http://www.dealtaker.com/our-blog/2009/11/20/kohana-php-3-0-ko3-tutorial-part-1/"> read it before going on</a>. In this tutorial we will be going over how to develop views.<br />
<span id="more-1835"></span><br />
Before we get into views, we&#8217;ll want to update the KO3 code base, So point your browser to <a href="http://dev.kohanaphp.com/projects/kohana3/files">http://dev.kohanaphp.com/projects/kohana3/files</a>, download it, open it and extract everything inside the &#8220;kohana&#8221; folder to &#8220;mykohana3&#8243;. Once you have done that, delete or rename the &#8220;install.php&#8221; in your &#8220;mykohana3&#8243; folder. Next, open up the &#8220;bootstrap.php&#8221; file in the &#8220;application&#8221; folder and change the following line:<br />
[php]Kohana::init(array(&#8216;base_url&#8217; =&gt; &#8216;/kohana/&#8217;));[/php]<br />
to<br />
[php]Kohana::init(array(&#8216;base_url&#8217;   =&gt; &#8216;/mykohana3/&#8217;,<br />
&#8216;index_file&#8217; =&gt; &#8221;));[/php]<br />
Now that we are updated to the latest and greatest, lets get right in to developing a view. Create a new folder within the &#8220;application&#8221; folder named &#8220;views&#8221; and inside the &#8220;views&#8221; folder, create another folder named &#8220;pages&#8221;. Now open up a new document in your editor and put the following in it:<br />
[php]&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Hello!&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;This is my first view&lt;/h1&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;[/php]<br />
Now save that in your &#8220;application/views/pages&#8221; as &#8220;ko3.php&#8221;. As you can tell, it&#8217;s a pretty simple HTML page with some PHP mixed in. Lets open the &#8220;ko3&#8243; controller (&#8220;application/classes/controller/ko3.php&#8221;). Replace the &#8220;action_index&#8221; class with the following:<br />
[php]    public function action_index()<br />
{<br />
$this-&gt;request-&gt;response = View::factory(&#8216;pages/ko3&#8242;);<br />
}[/php]<br />
Save it, and load up &#8220;http://yourserver/myfirstkohana3/&#8221; in your browser. You should see &#8220;This is my first view&#8221;. The code above is pretty simple, we use the &#8220;Facotry&#8221; method of the view class to load the file &#8220;application/views/pages/ko3.php&#8221;, renders it and outputs it. Not very exciting, so let go back to the view (&#8220;application/views/pages/ko3.php&#8221;) and add:<br />
[php]  &lt;?php echo $content;?&gt;[/php]<br />
After the &#8220;h1&#8243; tags. Your view should look like this:<br />
[php]&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Hello!&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;This is my first view&lt;/h1&gt;<br />
&lt;?php echo $content;?&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;[/php]<br />
If we were to refresh the browser, we would seen an exception error about an undefined variable. So let&#8217;s go ahead and fix that, by relating data to the view variable in our controller. So, change up &#8220;Action_Index&#8221; controller to look like this:<br />
[php]    public function action_index()<br />
{<br />
$view                       = View::factory(&#8216;pages/ko3&#8242;);<br />
$view-&gt;content              = &#8216;We have data!&#8217;;<br />
$this-&gt;request-&gt;response = $view-&gt;render();<br />
}[/php]<br />
Now if we refresh the browser, we should see &#8220;This is my first view&#8221; and under that &#8220;We have data!&#8221;. Lets explain the the code line by line.<br />
[php]$view = View::factory(&#8216;pages/ko3&#8242;);[/php]<br />
This load our view file (&#8220;application/views/pages/ko3.php&#8221;) into the view controller.<br />
[php]$view-&gt;content = &#8216;We have data!&#8217;;[/php]<br />
This assigns a variable for use by the view called &#8220;content&#8221; and we also assign data to that, in this case it&#8217;s &#8220;We have data!&#8221;<br />
[php]$this-&gt;request-&gt;response = $view-&gt;render();[/php]<br />
This renders the view and outputs it.</p>
<p>Simple enough, but there is another way of doing the above. We could have also done the following:<br />
[php]    public function action_index()<br />
{<br />
$data['content']         = &#8216;We have data!&#8217;;<br />
$view                    = View::factory(&#8216;pages/ko3&#8242;, $data);<br />
$this-&gt;request-&gt;response = $view-&gt;render();<br />
}[/php]<br />
Basically the above use an array with element keys as the template variable names to assign the data. We are not done yet! There&#8217;s 2 more ways we can do the above.<br />
[php]    public function action_index()<br />
{<br />
$view                    = View::factory(&#8216;pages/ko3&#8242;)<br />
-&gt;set(&#8216;content&#8217;, &#8216;We have data!&#8217;);</p>
<p>$this-&gt;request-&gt;response = $view-&gt;render();<br />
}[/php]<br />
The above used the set method of the view class, which you can use method chaining to set all your template variables. And now for forth way:<br />
[php]    public function action_index()<br />
{<br />
$content                 = &#8216;We have data!&#8217;;<br />
$view                    = View::factory(&#8216;pages/ko3&#8242;)<br />
-&gt;bind(&#8216;content&#8217;, $content);<br />
$this-&gt;request-&gt;response = $view-&gt;render();<br />
}[/php]<br />
Now the above is using the bind method of the view class. Once again, this you can do method chaining here. This is a little bit different from the set method, since it will create a reference to a variable. So $content might equal &#8216;We have data!&#8217; when we bound the variable to the template variable, but could change later on, since we are referencing the variable $content in our controller, instead of assigning data to the template variable.</p>
<p>If we were to do the following:<br />
[php]    public function action_index()<br />
{<br />
$content                 = &#8216;We have data!&#8217;;<br />
$view                    = View::factory(&#8216;pages/ko3&#8242;)<br />
-&gt;bind(&#8216;content&#8217;, $content);<br />
$content                 = &#8216;Our data changed&#8217;;<br />
$this-&gt;request-&gt;response = $view-&gt;render();<br />
}[/php]<br />
Instead of &#8220;We have data!&#8221; showing on our screen, we would have &#8220;Our data changed&#8221;.</p>
<p>Now lets do a view with in a view! Create a new folder in your &#8220;application/views/&#8221; named &#8220;blocks&#8221;. Lets create another view file named &#8220;ko3_inner.php&#8221; and put this in it:<br />
[php]  &lt;h3&gt;This is an inner view&lt;/h3&gt;[/php]<br />
Save that in your &#8220;application/views/blocks/&#8221; directory. Now lets edit the &#8220;ko3&#8243; view (&#8220;application/views/pages/ko3.php&#8221;) and add the following:<br />
[php]  &lt;?php echo View::factory(&#8216;blocks/ko3_inner&#8217;)-&gt;render(); ?&gt;[/php]<br />
You view should like this:<br />
[php]&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Hello!&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;This is my first view&lt;/h1&gt;<br />
&lt;?php echo $content;?&gt;<br />
&lt;?php echo View::factory(&#8216;blocks/ko3_inner&#8217;)-&gt;render(); ?&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;[/php]<br />
So if we run it we should see what we had before, then &#8220;This is an inner view&#8221;. This would be useful for static content, but we won&#8217;t be able to use variables directly to that view. So let fix this. Lets go back to our controller (&#8220;application/classes/controllers/ko3.php&#8221;) and edit the &#8220;Action_Index&#8221; method to look like this:<br />
[php]    public function action_index()<br />
{<br />
$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 />
$view                    = View::factory(&#8216;pages/ko3&#8242;, $ko3);<br />
$this-&gt;request-&gt;response = $view-&gt;render();<br />
}[/php]<br />
This will render the view to a array that then is rendered by the main view. If you noticed I did the inner stuff first and went back to using array template variable setting style. Next we will need to edit the main view (&#8220;application/views/pages/ko3.php&#8221;). The line we put in before, we will change it to:<br />
[php]  &lt;?php echo $ko3_inner; ?&gt;[/php]<br />
The view should look like this:<br />
[php]&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Hello!&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<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;/body&gt;<br />
&lt;/html&gt;[/php]<br />
And the last change we need to make is to the inner view (&#8220;application/views/blocks/ko3_inner.php&#8221;). Make it look like this:<br />
[php]  &lt;h3&gt;This is an inner view&lt;/h3&gt;<br />
&lt;?php echo $content;?&gt;[/php]<br />
If you refresh the browser after saving, you should now see the following:<br />
[php]This is my first view</p>
<p>We have data</p>
<p>This is an inner view</p>
<p>We have more data[/php]<br />
Pretty cool, now you can make your views more modular and reusable. Now lets get into making variables globally available to your views. Back to your controller (&#8220;applications/classes/controllers/ko3.php&#8221;) and edit the &#8220;Action_Index&#8221; method so the following is at the top of the method:<br />
[php]       View::set_global(&#8216;x&#8217;, &#8216;This is a global variable&#8217;);[/php]<br />
So now the method should look like this:<br />
[php]    public function action_index()<br />
{<br />
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 />
$view                    = View::factory(&#8216;pages/ko3&#8242;, $ko3);<br />
$this-&gt;request-&gt;response = $view-&gt;render();<br />
}[/php]<br />
Now if you were to edit your views and add in:<br />
[php]  &lt;br/&gt;&lt;?php echo $x;?&gt;[/php]<br />
You should now see &#8220;This is a global variable&#8221; two times on the page. As you can see this could be very handy. Basically, we are using the static method of &#8220;set&#8221; from the view class, so it ends up being available to all view object instances. You can also use the static method of &#8220;bind&#8221; to reference variables. This could be handy for dry things &#8220;<acronym title="Don't Repeat Yourself">DRY</acronym>&#8221; and could be used in other place, like a contruct.</p>
<p>Untill next time, when I go over &#8220;templates&#8221; (advanced views and controller stuff), happy coding!<br />
Sources used: <a href="http://kerkness.ca/wiki/doku.php?id=template-site:create_the_template" target="_blank">Unofficial Kohana 3 Wiki</a></p>
<p><a href="http://www.dealtaker.com/our-blog/2009/11/20/kohana-php-3-0-ko3-tutorial-part-1/">Part 1</a> | <a href="http://www.dealtaker.com/our-blog/2009/12/30/kohana-php-3-0-ko3-tutorial-part-3/">Part 3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2009/12/07/kohana-php-3-0-ko3-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>Kohana PHP 2.3.x Tutorial Part II</title>
		<link>http://www.dealtaker.com/our-blog/2009/05/21/kohana-php-23x-tutorial-part-ii/</link>
		<comments>http://www.dealtaker.com/our-blog/2009/05/21/kohana-php-23x-tutorial-part-ii/#comments</comments>
		<pubDate>Thu, 21 May 2009 09:51:27 +0000</pubDate>
		<dc:creator>ellisgl</dc:creator>
				<category><![CDATA[DealTaker.com Updates]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.dealtaker.com/blog/?p=633</guid>
		<description><![CDATA[Welcome to the second tutorial in this series on how to develop with Kohana PHP. If you haven&#8217;t read the first part, I would read Part 1 before going on. In this tutorial we will be going over views, wonderful views, glorious views. Let&#8217;s just dive straight into building our first picturesque view. Create the [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to the second tutorial in this series on how to develop with Kohana PHP. If you haven&#8217;t read the first part, I would <a href="http://www.dealtaker.com/blog/2009/04/23/kohana-php-23x-tutorial-part-1/">read Part 1</a> before going on. In this tutorial we will be going over views, wonderful views, glorious views.<br />
<span id="more-633"></span></p>
<p>Let&#8217;s just dive straight into building our first picturesque view.<br />
Create the file &#8220;myfirstkohana/application/views/hello.php&#8221; and put the following into it:</p>
<pre>&lt;html&gt;
 &lt;head&gt;
  &lt;title&gt;Hello!&lt;/title&gt;
 &lt;/head&gt;
 &lt;body&gt;
  &lt;h1&gt;This is my first view&lt;/h1&gt;
  &lt;?php echo $content;?&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Looking at the above, it&#8217;s pretty straight forward and simple HTML file with some simple PHP code thrown in.</p>
<p>Just because we put a view with the same name as our controller, it&#8217;s not going to be shown until we say so. Let&#8217;s go back to our  wonderful controller &#8220;myfirstkohana/application/controllers/hello.php&#8221; and edit the index method to look like the following:</p>
<pre>public function index()
 {
  $view          = new View('hello');
  $view-&gt;content = 'Hello World!';
  $view-&gt;render(TRUE);
 }</pre>
<p>Let go through this line by line. The first line:</p>
<pre>$view = new View('hello');</pre>
<p>tells the framework to create a new &#8220;view&#8221; object and load the file :myfirstkohana/application/views/hello.php&#8221; into it. The next line:</p>
<pre>$view-&gt;content = 'Hello World!';</pre>
<p>creates a new property called &#8220;content&#8221; in the object that has the value &#8220;Hello World!&#8221;. The last line</p>
<pre>$view-&gt;render(TRUE);</pre>
<p>compiles the template with our assigned values and outputs it to the screen. What we should get when we load &#8220;http://youserver/myfirstkohana/hello&#8221; is &#8220;This is my first view&#8221; and below that should read &#8220;Hello World!&#8221;.</p>
<p>This might the simplist view you&#8217;ll see. Of course, I don&#8217;t think we&#8217;ll be wanting something so simple, so lets knock it up notch and create views with nested views.</p>
<p>Create the file &#8220;myfirstkohana/application/views/default_template.php&#8221; and put this in it:</p>
<pre>&lt;?php echo $header; ?&gt;
&lt;?php echo $content; ?&gt;
&lt;?php echo $footer; ?&gt;</pre>
<p>The above code is just a master template, or the template of templates if you will. Next lets create &#8220;myfirstkohana/application/views/default_header.php&#8221; and put the following into it:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" &gt;
 &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;&lt;?php echo $pageTitle;?&gt;&lt;/title&gt;
 &lt;/head&gt;</pre>
<p>and then &#8220;myfirstkohana/application/views/default_content.php&#8221;:</p>
<pre> &lt;body&gt;
  &lt;h1&gt;This is my second view&lt;/h1&gt;
  &lt;?php echo $content;?&gt;</pre>
<p>finally &#8220;myfirstkohana/application/views/default_footer.php&#8221;:</p>
<pre> &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Above we taken a basic HTML page and split it up &#8220;logically&#8221; into a header, footer and a content section. Let open our controller to build out our more complex view.  Edit &#8220;myfirstkohana/application/controllers/hello.php&#8221; to make the index method look like this:</p>
<pre>  public function index()
   {
    $view                    = new View('default_template');
    $view-&gt;header            = new View('default_header');
    $view-&gt;content           = new View('default_content');
    $view-&gt;footer            = new View('default_footer');
    $view-&gt;header-&gt;pageTitle = 'I am on the top';
    $view-&gt;content-&gt;content  = 'Hello World!';

    $view-&gt;render(TRUE);
   }</pre>
<p>That wasn&#8217;t too hard was it? Basically we load our first view, which is the main template. Then we populate our properties with objects of our inner templates. It&#8217;s like working with multi-demential arrays in a sense.</p>
<p>With the ability to nest views/templates inside each other, things are a lot nicer to deal with. There is a problem thou. Each method (action) that needs to have a templated view, you need to do what we did to our index action to each one. Kohana does have an answer.</p>
<p>The Template_Controller allows to give a default view to actions, which if you have a lot of actions for a controller, it can really cut down on development and debugging time. Let go ahead and re-work out the hello controller (&#8220;myfirstkohana/application/controllers/hello.php&#8221;).</p>
<pre>&lt;?php
defined('SYSPATH') or die('No direct access allowed.');

class Hello_Controller extends Template_Controller
 {
    public $template    = 'default_template'; // Default template (view) to use
    public $auto_render = TRUE;               // Auto render template after controller is done

    public function __call($method, $arguments)
     {
        $this-&gt;name($method);
     }

    public function __construct()
     {
        parent::__construct(); // Has to be here!

        // Load the inner templates
        $this-&gt;template-&gt;header            = new View('default_header');
        $this-&gt;template-&gt;content           = new View('default_content');
        $this-&gt;template-&gt;footer            = new View('default_footer');
        $this-&gt;template-&gt;header-&gt;pageTitle = 'My first Kohana App';
     }

    public function index()
     {
        // Put something useful in our variables.
        $this-&gt;template-&gt;header-&gt;pageTitle .= ' ::: I am on the top';
        $this-&gt;template-&gt;content-&gt;content   = 'Hello World!';
     }

    public function america()
     {
        $this-&gt;template-&gt;content-&gt;content = 'Hello America!';
     }

    public function name($name)
     {
        $this-&gt;template-&gt;content-&gt;content = 'Hello ',$name,'!';
     }
 }</pre>
<p>Here we have defaulted our main template, so now we didn&#8217;t have to declare it in each method. Also in our constructor method, we loaded the sub templates in and gave a default value to one of the template variable that used in the title tag. In our index method we added on to the title tag (think SEO) and put some generic content in place.</p>
<p>Stay tuned for the third part of our series and find out about models and working with external data!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dealtaker.com/our-blog/2009/05/21/kohana-php-23x-tutorial-part-ii/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

