-
Notifications
You must be signed in to change notification settings - Fork 12
Templatemanager
Templates allow you to separate rendering logic from data handling, and allow you to define any number of views for the same controller. In PHP, templates are handled by the TemplateManager, which uses Smarty for processing. In addition to the standard built-in Smarty functions, Elation implements some basic variable scope, and provides a few convenient extensions.
Call the component specified by the "name" attribute, and render its contents as an HTML snippet. Can take any number of arguments, which are made available to the called component's controller in the $args array
Register a [dependency][DependencyManager] of the specified type, to ensure all required CSS, JS, meta tags, etc. are set up in the <head> before the component is rendered
Print a JSON-encoded representation of the specified variable
The TemplateManager varies slightly from standard Smarty functionality with the addition of the TemplateManager::GetTemplate($tplfile, $vars) function. This function allows Smarty to be called recursively without clobbering previously-assigned variables, or leaving garbage variables behind. Consider the following component:
<?
class Component_sample extends Component {
function controller_scopetest($args) {
$vars["testvar"] = any($args["testvar"], "default");
return $this->GetComponentResponse("./scopetest.tpl", $vars);
}
function controller_scopetestinner($args) {
$vars["testvar"] = any($args["testvar"], "innerdefault");
return $this->GetComponentResponse("./scopetest.tpl", $vars);
}
}
<h2>Simple Scope Test</h2>
<p>top level, $testvar started as "{$testvar|escape:html}"</p>
{component name="sample.scopetest2" testvar="hello"}
<p>back at top level, $testvar is now "{$testvar|escape:html}"</p>
<p>inner level, $testvar is "{$testvar|escape:html}"</p>
The testvar argument is accepted by the top level component (sample.scopetest), and we call Smarty to print its initial value. From within the first template, we then call the inner component (sample.scopetestinner), which also accepts an argument called testvar, and we give it a new value. Execution then returns to the top-level template, which prints the value of its testvar variable.
Traditionally, using Smarty::assign(name, value) and one global Smarty instance, the output would look like this:
top level, $testvar started as "default"
inner level, $testvar is "hello"
back at top level, $testvar is now "hello"
As you can see, the variables assigned from the inner template have clobbered the previous value for $testvar in the parent component. Elation's TemplateManager implements basic scoping which allows each template to have its own local scope for template variables, while still allowing access to the parent template's non-overridden variables. When a component template's template finishes execution, the Smarty variable stack is restored to its previous state. With these changes, the template correctly renders as:
top level, $testvar started as "default"
inner level, $testvar is "hello"
back at top level, $testvar is now "default"
(Note that this functionality may be obsolete now that Smarty 3 has Template objects. When the work is done to upgrade to Smarty 3.x we can possibly reevaluate the need)