我知道這是主觀的第一部分,但我想聽聽人們使用的一些不同的技術。這是一個兩部分問題:你在PHP中對複雜的多行字符串使用什麼?而且,我可以使用與smarty合成類型的關係嗎?鏈接smarty模板和乾淨的多行字符串 - PHP
問題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>
我想在任何.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');
我知道有「模板inheritanc e「,但我並不熟悉它。它可以給我類似的功能嗎?
注:我認爲我與heredoc最大的問題是,我無法得到HTML的語法高亮(如果我在heredoc字符串中指定html)。如果沒有突出顯示,我想通過smarty閱讀的html更難以閱讀,這種打破smarty的目的。
你的模板看起來不合法....似乎缺少''''和'}'? – Baba
對不起,我剛剛通過一個例子,我是新來的聰明。我會修復它,雖然 – clementine
我的答案在模板中包含模板方面有意義嗎? –