$tpl = $smarty->createTemplate('my.tpl'); $tpl->assign('foo','bar'); $smarty->display($tpl); // or $tpl->display(); $data = new Smarty_Data; $data->assign('foo','bar'); $smarty->display('my.tpl',$data); $tpl = $smarty->createTemplate('my.tpl',$data); $smarty->display('foo:bar.tpl'); {include file="foo:bar.tpl"} 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>
$smarty->registerFilter('variable','htmlspecialchars'); {* 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
{$ foo nocache} - do not cache the contents of this variable
{include file = "foo.tpl" nocache} - do not cache the contents of the include file
Source: https://habr.com/ru/post/108190/
All Articles