简体   繁体   English

是否可以在没有tpl文件的情况下使用smarty模板?

[英]Is it possible to use smarty template without tpl file?

I need to assign data to javascript source. 我需要将数据分配给javascript源。 Is it possible to use smarty template without tpl file. 是否可以使用没有tpl文件的smarty模板。 Like this... 像这样...

$sHtml = '
    {foreach from=$myArray key=k item=v}
       <li>{$k}: {$v}</li>
    {/foreach}
';

$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding');
$oSmarty = new smarty();
$oSmarty->assign('myArray', $arr);
$result = $oSmarty->fetch($sHtml);

echo $result;

Return like this... 这样回来...

<li>9: Tennis</li>
<li>3: Swimming</li>
<li>8: Coding</li>

From Smarty 3 on you can do that with 从Smarty 3开始,您可以使用

$smarty->fetch('string:'.$content);

See here fore more information: String Template Resources 请参阅此处以获取更多信息: 字符串模板资源

If you are using Smarty < 3 you could save the content of $sHtml in a temporary file and pass the file name to the fetch call: 如果您使用的是Smarty <3,则可以将$sHtml的内容保存在一个临时文件中,并将文件名传递给fetch调用:

$smarty->fetch('file:/full/path/to/tmp/file');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM