【发布时间】:2012-10-08 10:13:31
【问题描述】:
我知道第一部分是主观的,但我想听听人们使用的一些不同的技术。这是一个由两部分组成的问题:您在 PHP 中使用什么来处理复杂的多行字符串?还有,我可以使用 smarty 的组合类型的关系吗?
问题 1:我知道有 heredoc 和 "."操作员。如果有的话,我正在寻找新鲜的、更易读的想法。
问题 2:更具体地说,这是我想对 smarty 做的事情。
假设我有一个模板,base.tpl:
<html>
<head><title></title></head>
<body>
{$main_content}
</body>
</html>
我可以链接模板,即代表 $main_content 的另一个模板,比如 main.tpl:
<div id="header">$header</div>
<div id="container">
<h1>{$first_header}</h1>
<p>{$first_paragraph}</p>
<h1>{$second_header}</h1>
<p>{$second_paragraph}</p>
我想在whatever.php中将一个模板加载到另一个模板中,即:
// ... including smarty and all other boiler plate things ...
$smarty -> assign('first_header', "Foo");
$smarty -> assign('first_paragraph', "This is a paragraph");
$smarty -> assign('second_header', "Bar");
$smarty -> assign('second_paragraph', "This is another paragraph");
$main_content = $smarty->load('main.tpl');
$smarty -> display('base.tpl');
我知道smarty里面有“模板继承”,但是不熟悉。它能给我类似的功能吗?
注意:我认为 heredoc 最大的问题是我无法为 html 获得语法高亮(如果我在 heredoc 字符串中指定 html)。如果没有突出显示,我想通过 smarty 传递的 html 会更难阅读,这有悖于 smarty 的目的。
【问题讨论】:
-
您的模板看起来无效....似乎缺少
{和}??? -
对不起,我只是通过一个例子,我是smarty的新手。不过我会修复它
-
我的回答在模板中包含模板是否有意义?
-
是的,这或多或少是我想要的。谢谢
标签: php smarty multilinestring