Smarty

From HTML5 Builder
Jump to: navigation, search

Smarty is a powerful PHP template engine. This page is a simple introduction to help you get started writing Smarty template files. For additional information, check out the help resources below.

Note: For documentation on how to use templates on pages, check Interface Templates.

Usage

Smarty template files are regular HTML documents (they use the .tpl extension by convention) with additional Smarty-specific tags. Its syntax is shorter and more readable than embedding plain PHP on pages. For example, to print the content of a variable with PHP, you would use:

<?php echo $variable; ?>

With Smarty, you would simply write:

{%$variable%}

Note: The RPCL implementation of Smarty uses {% %} as delimiters, instead of just { } as you will see on the official documentation.

Base Template

When writing a Smarty template file, you should base it on the following example. The Smarty tags defined here are needed for the RPCL components to work properly:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>RPCL :: PHP Web Application Development Framework</title>
    <meta charset="utf-8">
    {%$HeaderCode%}
  </head>
  <body>
    {%$StartForm%}
    {%$EndForm%}
  </body>
</html>

References to Components

To include references to components on your template, just use the variable you use for the component inside the Page container. For example, for a component called Button1, its variable would be $Button1, and you would reference it from the Smarty template with the following code:

{%$Button1%}

Advanced

To take advantage of all the power of the Smarty templates, you can get access to the Smarty template object created to render the page from the OnTemplate event of the Page container.

For example, you can access the Smarty object itself to define Smarty variables:

$template = $params['template'];
$template->_smarty->assign("variableName","<p><em>Some <strong>HTML code</strong> to be output on calls to the variable.</em></p>");

Then you could use that variable on the template like any other variable:

{%$variableName%}

And the paragraph would be printed when the output code were generated:

Some HTML code to be output on calls to the variable.

Help Resources

Documentation

Sample Applications

On the Features/Templates folder inside the sample applications directory you will find a project showcasing Smarty:

  • TemplatedServerPage. A templated form that includes several components referenced from a Smarty template file.

Other

See Also