Smarty 3 Final released. What's new?

    Most recently, the final version of the popular template engine Smarty 3 was released. Innovations of this version:


    - Rewritten under PHP5, no longer working with PHP4 (requires PHP 5.1)
    - Uses its own class loader

    - Own real parser and lexer for templates, which allowed us to introduce such innovations as complex mathematics in templates, error messages accurate to the line and nested template function calls

    - Template objects are available
    $tpl = $smarty->createTemplate('my.tpl');
    $tpl->assign('foo','bar');
    $smarty->display($tpl); // or $tpl->display();


    - Data objects:
    $data = new Smarty_Data;
    $data->assign('foo','bar');
    $smarty->display('my.tpl',$data);
    $tpl = $smarty->createTemplate('my.tpl',$data);
    


    - PHP thread support
    $smarty->display('foo:bar.tpl');
    {include file="foo:bar.tpl"}
    

    In these cases, the registered foo resource will be searched first to load the template. If this is not found, Smarty will check for the presence of the stream foo: //

    - Inheritance of templates
    parent .tpl
    <html>
      <head>
      	<title> {block name = title} default title {/ block} <title>
      </head>
      <body>
    	{block name = body} default body {/ block}
      </body>
    </html>
    child .tpl
    {extends file = "parent.tpl"}
    {block name = title} My Child Title {/ block}
    {block name = body} My Child Body {/ block}
    Result $ smarty-> display ('child.tpl');
    <html>
      <head>
      	<title> My Child Title <title>
      </head>
      <body>
    	My child body
      </body>
    </html>
    


    - Automatically ignoring curly braces "{", "}" if they are surrounded by spaces (you no longer need to surround Javascript {literal} {/ literal} or use a different tag marker).

    - Filter to display all default template variables
     $smarty->registerFilter('variable','htmlspecialchars');
    


    - Features in templates
    {* define a function *}
    {function name = menu level = 0}
      <ul class = "level {$ level}">
      {foreach $ data as $ entry}
        {if is_array ($ entry)}
          <li> {$ entry @ key} </li>
          {menu data = $ entry level = $ level + 1}
        {else}
          <li> {$ entry} </li>
        {/ if}
      {/ foreach}
      </ul>
    {/ function}
    {* Create an array *}
    {$ menu = ['item1', 'item2', 'item3' => ['item3-1', 'item3-2', 'item3-3' =>
    ['item3-3-1', 'item3-3-2']], 'item4']}
    {* run it through the function *}
    {menu data = $ menu}
    <pre>
    Conclusion
    * item1
    * item2
    * item3
          o item3-1
          o item3-2
          o item3-3
                + item3-3-1
                + item3-3-2
    * item4
    


    - Element-level caching management
     {$ foo nocache} - do not cache the contents of this variable
     {include file = "foo.tpl" nocache} - do not cache the contents of the included file
    


    - Almost full compatibility with Smarty 2.
    - The use of the {php} tag is disabled by default and is considered obsolete.
    - Smarty now defaults to UTF-8 pattern encoding.

    The three turned out pretty easy. The main file with all classes takes only 27Kb. Most of the functionality, including even utility tags like foreach, is plugged in and loaded if it is required to compile the template. However, it should be borne in mind that at compilation itself, at least plugins smarty_internal_templatelexer + smarty_internal_templateparser.php will be loaded, and this is already ~ 200Kb. However, compiling templates on a heavily loaded site is generally not required in normal operation, so 30Kb of code is pleasantly pleasing.

    Also popular now: