Developer's Guide :: Smarty Templates

From Delphi for PHP Documentation Wiki

Jump to: navigation, search

This topic discusses how to use Smarty template engine in your VCL for PHP projects. Using it, your designers can keep producing HTML (and css, and images, etc) templates and you can use VCL for PHP to add live content, like rich editors, dbgrids, etc.

The steps to use Smarty templates, are these ones:

Contents

[edit] Create your template

An smarty template can be a regular HTML file, and can be named with any extension, by convention you can use .tpl, but .html works too. You create a template by adding smarty tags inside, the tags are in the form of:

{%$tag%}

You can find more information about Smarty syntax here: Smarty : Template Engine

[edit] Add standard tags

The Smarty plugin needs some standard tags in your template to be able to insert the right content to make events work and to add javascript code and other assets, this is the code of a basic template with the standard tags added:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>VCL for PHP :: PHP Web Application Development Framework</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
{%$HeaderCode%}
</head>
<body>
{%$StartForm%}
{%$EndForm%}
</body>
</html>

As you can see, there are three tags (HeaderCode, StartForm, EndForm), with those tags, the template is ready to be used by the Page component.

[edit] Attach the template to your page

To get the template up and running, you just need to set two properties:

  • TemplateEngine - To "SmartyTemplate"
  • TemplateFilename - With the path to your template

And that's it, if you run your page, you will get your template working

[edit] Add tags to place components inside

The real power of combining Smarty and VCL for PHP is inserting components inside the template, and this is really easy to do, just insert a tag with your component name {%$componentname%} in your template, wherever you want.

That way, you can create a VCL for PHP form, holding controls like richedits and then place the component on any place in your document and get the events processed.

[edit] Accessing the internal Smarty object

To use all the power of Smarty templates, you can get access to the Smarty template object created to render the page, to do that, you can use the Page::OnTemplate event, which provides you the right moment to use the smarty object.

To use it, you can do it this way:

 
$template=$params['template'];
$template->_smarty->assign('latest_daily',$this->getDailyBuildsLeft());
 

So you can make any template operation, to know what you can do, checkout the Smarty documentation.

Personal tools