⬆️ ⬇️

Smarty 3 Final has been released. What's new?

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





- Rewrote under PHP5, no longer works with PHP4 (requires PHP 5.1)

- Uses its class loader



- Own real parser and lexer for templates, which made it possible to introduce such innovations as complex mathematics in templates, error messages with a string accuracy and nested calls to template functions

')

- Template objects 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 streaming support

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


In these cases, it will first search for the registered foo resource to load the template. If it does not find one, Smarty will check for the presence of the foo: // stream.



- Pattern inheritance

 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>




- Automatic ignoring of curly brackets "{", "}" if they are surrounded by spaces (you no longer need to surround Javascript {literal} {/ literal} or use another tag marker).



- Filter to display all template variables by default

  $smarty->registerFilter('variable','htmlspecialchars'); 




- Functions in templates

 {* define 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




- Management of caching at the element level

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




- Almost complete compatibility with Smarty 2.

- The use of the {php} tag is disabled by default and is considered obsolete.

- Smarty now defaults to UTF-8 encoding.



The triple turned out pretty easy. The main file with all classes takes only 27Kb. Most of the functionality, including even service tags like foreach, is rendered into plug-ins and loaded, if required to compile the template. However, it should be borne in mind that at the very compilation at least the smarty_internal_templatelexer + smarty_internal_templateparser.php plugins will be loaded, and this is ~ 200Kb. However, on a heavily loaded site to compile templates, in general, in normal operation is not required, therefore, 30Kb of code is pleasantly pleasing.

Source: https://habr.com/ru/post/108190/



All Articles