Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here' a simplified but functional view class that I've seen around quite a lot and use quite a lot.<br> As you can see in the code below: you instantiate a view with the filename of the template file.<br> The client code, probably a controller, can send data into the view. This data can be of any type you need even other views.<br> Nested views will be automatically rendered when the parent is rendered.<br> Hope this helps.</p> <pre><code>// simple view class class View { protected $filename; protected $data; function __construct( $filename ) { $this-&gt;filename = $filename; } function escape( $str ) { return htmlspecialchars( $str ); //for example } function __get( $name ) { if( isset( $this-&gt;data[$name] ) { return $this-&gt;data[$name]; } return false; } function __set( $name, $value ) { $this-&gt;data[$name] = $value; } function render( $print = true ) { ob_start(); include( $this-&gt;filename ); $rendered = ob_get_clean(); if( $print ) { echo $rendered; return; } return $rendered; } function __toString() { return $this-&gt;render(); } } </code></pre> <p>Usage</p> <pre><code>// usage $view = new View( 'template.phtml' ); $view-&gt;title = 'My Title'; $view-&gt;text = 'Some text'; $nav = new View( 'nav.phtml' ); $nav-&gt;links = array( 'http://www.google.com' =&gt; 'Google', 'http://www.yahoo.com' =&gt; 'Yahoo' ); $view-&gt;nav = $nav; echo $view; </code></pre> <p>The templates</p> <pre><code>//template.phtml &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;?php echo $this-&gt;title ?&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;?php echo $this-&gt;nav ?&gt; &lt;?php echo $this-&gt;escape( $this-&gt;text ) ?&gt; &lt;/body&gt; &lt;/html&gt; //nav.phtml &lt;?php foreach( $this-&gt;links as $url =&gt; $link ): ?&gt; &lt;a href="&lt;?php echo $url ?&gt;"&gt;&lt;?php echo $link ?&gt;&lt;/a&gt; &lt;?php endforeach ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload