$views
The SubTemplate System provides a means by which you can create and parse templates for variable identifiers, merge them with data from your application, and send the merged result back to the user.
If you are using The Jet Engine along with iTemplates, then you'll have all of your pre-defined sub-templates preloaded at runtime. At the beginning of application execution, The Jet Engine reads in all of the currently defined internal templates into a global variable called $templates. You may easily reference an internal template from within your function by bringing in the global variable, and then grabbing the template you want from this multidimensional associative array. This associative array is setup as follows:
$views['program name that owns the template']['template type']['template name']
Whether using the $templates variable provided for you, or if you're reading in your subtemplates from a file or other means ....once you have obtained the template text, all you need to do in order to use this template to create some output is to define the variables that you'll be merging into the template as an associative array, with the array keys matching up with the variables within the template.
Let's take a look at the simple example of a blog. If you imagine a blog article screen, it's going to have 2 main elements to it:
· The Article Itself
· Any Replies/Comments made by other users
Here's the principle plugin function:
Version 1: Embedded HTML (the old way)
function body() {
global $article;
global $replies;
$output = '';
// Let's assume here that we already have retrieved
// the article as an associative array and all of
// the replies as an array of associative arrays.
$output = "
<h2>$article[title]</h2>
<div style='padding:4px; background:black; color:white'>
$article[author] / $article[post_date]
</div>
<div style='padding:10px;'>$article[text]</div>
";
foreach ( $replies as $reply ) {
$output .= "
<div style='margin:left:25px; padding-bottom:10px;'>
$reply[reply_text]
<p align='right'>$reply[author]</p>
</div>
";
}
return $output;
}
Version 2: Using an iTemplate Subtemplate and doing this the MVC way ...
The code is a little bit longer, but it completely separates the HTML from the logic, making the code infinitely more readable, and allows you to make layout and style changes in sub-template files, and not have to monkey with your source code when the designers change their mind ;)
Elsewhere in your code, or an external file ...
$article_template:
<h2>[title]</h2>
<div style='padding:4px; background:black; color:white'>
[author] / [post_date]
</div>
<div style='padding:10px;'>$article[text]</div>
$replies_template:
<div style='margin:left:25px; padding-bottom:10px;'>
[reply_text]
<p align='right'>[author]</p>
</div>
function body() {
global $article;
global $replies;
global $article_template;
global $replies_template;
$output = '';
// Create 2 new template objects, one for the articles
// and one for replies
$a_template = new SubTemplate (
array(
"front" => '[',
"back" => ']',
"template_text" => $article_template
)
);
$r_template = new SubTemplate (
array(
"front" => '[',
"back" => ']',
"template_text" => $replies_template
)
);
// First the articles. Assign the [values] in the template
// with the matching values from the associative array
$a_template>set_values( $article );
$output .= $a_template>merge();
// Now, loop through the replies, doing the same thing
foreach ( $replies as $reply ) {
$r_template>set_values( $reply );
$output .= $r_template>merge();
}
return $output;
}
This help file was created with an unregistered evaluation copy of Help & Manual. © EC Software. All rights reserved. This message will not appear if you compile this help file with the registered version of Help & Manual.