Every controller
has a template
object as one of its public variables. Basically the allows you to show views (html files with data) to your user. It’s a best practice to not do any logic or database work inside your views. Views are stored in the paths.views
configuration setting folder. Views should be named int he format of file.view.php.
Parent Class
None
Public Properties
template
. Essentially, your controller builds a “model” by storing values in this associative array. The view then has access to that model via referencing the values in this array. In this way, you can create a list of $vars
from a variety of sources and so long as the data exists in this array, your view won’t care how it got there. This helps separate the business logic (controller logic) from the data itself (the model) and the view.Data Type: array
Default Value: Associative array with precisely 3 key value pairs… ‘skin’, ‘header’, and ‘json’. skin is a string that is the name of the jQueryUI skin. header is an array of values to put in the HTTP headers and json is an array of values to send as json when applicable.
Data Type: boolean
Default Value: false
Public Methods
defaultHtmlGenerator
unless you provide your own. $vars[‘skin’] is set by default to ‘cupertino’. $vars[‘header’] and $[‘json’] are set to empty arrays by default as well.Parameters:
$htmlGenerator
(\ipinga\htmlGenerator: optional) an instance of the html generator you want this template/view to use. It should be an object that extends the base htmlGenerator
class. Default: an instance of the \ipinga\defaultHtmlGenerator
classReturns:
An instance of \ipinga\template
$vars
modelParameters:
$index
(string) is the name of variable you wish to pass to the template
$value
(mixed) is the value of the variable is can be of any data typeReturns:
null
Example:
1 2 3 |
// code inside of your controller... $this->template->myVar = 'some value'; |
$vars
modelParameters:
$index
(string) is the name of variable you wish to locate from within your view
Returns:
The value associated with the
$index
Example:
1 2 3 4 5 6 |
// code inside of your controller... $this->template->myVar = 'some value'; $this->template->show('xyz'); // show the xyz.view.php view file // code inside your xyz.view.php echo $myVar; // outputs 'some value' |
Parameters:
$filename
(string) is the base filename for your view file. If you pass ‘xyz’, then this function will expect to find a file called ‘xyz.view.php’ in the folder specified in the paths.views
configuration setting$newVars
(array: optional) allows you to completely replace anything that might currently be in the $vars
array. Default: nullReturns:
null
Example:
1 2 3 4 5 6 |
// code inside of your controller... $this->template->myVar = 'some value'; $this->template->show('xyz'); // show the xyz.view.php view file // code inside your xyz.view.php echo $myVar; // outputs 'some value' |
$vars
model to the file we are including. This little function takes care of that for you. If you don’t need access to that model, then it would be faster to use the normal PHP include or require functions.Parameters:
$filename
(string) is the base filename for your include file. If you pass ‘xyz’, then this function will expect to find a file called ‘xyz.php’ in the folder specified in the paths.views
configuration settingReturns:
null
Example:
1 2 3 4 5 |
// code inside of your VIEW file... <?php $this->include_file('some_other_file_in_the_view_folder'); ?> |
Example Controller/Method and View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
// Code in the controller's method... public function signupForm() { // create a reference to the table in the default database and make that available to template $this->template->user = \ipinga\table('user'); $this->template->show('signupForm'); } // sample ./views/signupForm.view.php... <body> <div id="layout-container"> <div id="layout-content"> <div class="ui-accordion ui-widget ui-helper-reset"> <h3 class="ui-accordion-header ui-state-default ui-accordion-header-active ui-state-active ui-corner-top" style="border-bottom: none;">Create Account</h3> </div> <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active"> <div style="margin: 50px auto; text-align: center"> <form id="frm" action="/signup" method="post"> <?php $this->html->field(array( 'table' => $user, 'field_name' => 'first_name', 'label' => 'First Name', 'checkpostvars' => true, 'showhints' => true )); $this->html->field(array( 'table' => $user, 'field_name' => 'last_name', 'label' => 'Last Name', 'checkpostvars' => true, 'showhints' => true )); $this->html->field(array( 'table' => $user, 'field_name' => 'email', 'label' => 'E-Mail Address', 'checkpostvars' => true, 'showhints' => true )); $this->html->field(array( 'table' => $user, 'field_name' => 'passwd', 'label' => 'Password', 'type' => 'password', 'checkpostvars' => true, 'showhints' => true )); ?> <br/><br/> <input type="submit" value="Create Account"> </form> </div> </div> </div> </div> <!-- container --> </body> </html> |