2014-09-19 70 views
1

刚开始,我对Smarty非常陌生。 我得到了一些需要首先解析的模板文件。 因此,了解Smarty是否只能解析一个特殊块而不是整个具有多个块的文件会很有趣。 有一个叫做fetch()的函数,但它不能像我希望的那样工作。可以Smarty只读取一个模块而不是模板吗?

这将是很好,如果有可能做这样的事情

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

你也许能够修补这样的功能一起使用[模板继承(http://www.smarty.net/docs/en/advanced.features.template.inheritance.tpl)和[串模板资源( http://www.smarty.net/docs/en/resources.string.tpl)。 – IMSoP 2014-09-20 14:01:56

回答

0

据我知道你不能做到这一点,但你可以有多个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的文件(或者你想用它任何东西 - 保存到文件等等)。

相关问题