Tina MVC View Files
This explains how to use the helper functions to add view data to your view files.
The Helper Functions
There are two helper functions that allow you to pass view data to your view file automatically. From your page controller you can use
$this->add_var( 'my_var' , $variable_that_will_not_be_html_escaped );
and
$this->add_var_e( 'my_unescaped_var_name' , $variable_that_WILL_be_html_escaped );
This will make your view data available within your view files in the $V global variable as object properties – in the above cases
$V->my_var
and
$V->my_unescaped_var_name
Instead of passing view data in an object or an array, you just leave it out as follows:
$this->load_view( 'my_view_file' , $my_view_data )
becomes
$this->load_view( 'my_view_file' )
Example
This example is based on example_mvc_page.php and example_mvc_view.php in the ‘tina-mvc/sample_apps/tina’ folder.
The Controller file: example_mvc_page.php
<?php class example_mvc_page extends tina_mvc_base_page_class { function __construct( $request ) { parent::__construct( $request ); /** * We'll use the dispatcher * * Default method is $this->index() */ $this->use_dispatcher = TRUE; } public function index() { /** * Some data that requires html escaping */ $sample_unescaped_view_data = array('var1' => 'these should be escaped: & < > (ampersand, less-than, greater-than )', 'arrVar' => array( 1,2,3 ), 'var2' => 'Should match (first bit has been manually escaped, second part has not): <em>NOT emphasised</em> == <em>NOT emphasised</em>', ); /** * Add it to the page template variable property and escape */ $this->add_var_e( 'var_e' , $sample_unescaped_view_data ); /** * Some data that is already escaped */ $sample_view_data = 'Following text should appear bold: <b>BOLD</b>'; /** * Add it to the page template variable */ $this->add_var( 'var_valid' , $sample_view_data ); /** * Some unescaped data */ $sample_invalid_view_data = 'Following is unescaped: & <script>alert(\'Tina says: careful passing unescaped data to your view file!\');</script>'; /** * Add it to the page template variable (the wrong way) */ $this->add_var( 'var_invalid' , $sample_invalid_view_data ); /** * Because we are not passing view data to the load_view() method, the data * in $this->template_vars is used instead. * * The functions add_var_e() and add_var() add data as object properties of * $this->template_vars */ $this->set_post_content( $this->load_view('example_mvc') ); $this->set_post_title('Page title...'); } } ?>
The View File: example_mvc_view.php
<?php /** * You should include this check in every view file you write. The constant is defined in * tina_mvc_base_page->load_view() */ if( ! defined('TINA_MVC_LOAD_VIEW') ) exit(); ?> <h2>This is a view loaded from 'example-mvc_page.php'</h2> <p>This is a page of paragraph.</p> <p>Some data added using add_var_e():<br><br> $V->var_e['var1'] => <?php echo $V->var_e['var1'] ?><br> $V->var_e['arrVar'] => <?php print_r( $V->var_e['arrVar'] ) ?> (echo'd using print_r() )<br> $V->var_e['var2'] => <?php echo $V->var_e['var2'] ?><br> </p> <p>Some data added using add_var():<br><br> $V->var_valid => <?php echo $V->var_valid ?> </p> <p>Some invalid data added using add_var() - use add_var_e() instead:<br><br> $V->var_invalid => <?php echo $V->var_invalid ?> </p> <p> Looping over the array using foreach(): </p> <table> <tr> <td>Array Index</td><td>Array Value</td> </tr> <?php foreach( $V->var_e['arrVar'] as $i => $v): ?> <tr> <td><?php echo $i ?></td><td><?php echo $v ?></td> </tr> <?php endforeach; ?> </table> <hr>