【问题标题】:Can Smarty read only one block instead of in template?Smarty 可以只读取一个块而不是模板吗?
【发布时间】:2023-04-06 03:26:01
【问题描述】:

一开始我对 Smarty 真的很陌生。 我有一些模板文件有时需要先解析。 因此,了解 Smarty 是否只能解析一个特殊块而不是包含许多块的整个文件会很有趣。 有一个名为fetch () 的函数,但它并不能像我希望的那样工作。

如果可以做类似的事情就好了

Smarty()->fetch($myTpl, array('blockname1'));

【问题讨论】:

标签: php templates smarty smarty3


【解决方案1】:

据我所知,您不能这样做,但您可以拥有多个 Smarty 文件。那么你可以做什么:

一个 Smarty 文件(例如 index.tpl):

bla bla bla 
{$content}
bla bla bla

另一个 Smarty 文件(例如 site.tpl

another bla bla bla 

在 PHP 中你现在可以这样做:

$site = $smarty->fetch('site.tpl');
$smarty->assign('content', $site . ' xxx'); // you modify in PHP content a bit
$smarty->display('index.tpl');

输出将是:

bla bla bla 
another bla bla bla xxx
bla bla bla

因此,如您所见,您需要将要单独解析的块放入另一个文件中,然后获取它,然后您可以将其内容分配给另一个 Smarty 文件(或用它做任何您想做的事情 - 保存到文件等等)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多