例如,我有一个具有非常特定格式的列表,我想一遍又一遍地放入不同的内容。
所以我可能有一个功能:
<?
function fancy_container (contents_array) {
<?
<ul>
<? for($contents_array as $content) { ?>
<li class='special'><? echo $content ?><div class='other-specials'></div></li>
<? } ?>
</ul>
?>
}
?>
这行得通,但我想这样称呼它:
<?
fancy_container(array(?>
<div class='whatever'>hi there</div>
<div class='whatever'>hi there</div>
<div class='whatever'>hi there</div>
<?), ?>
<div class='other-junk'>hiya</div>
<div class='other-junk'>hiya</div>
<div class='other-junk'>hiya</div>
<?))
?>
我想出了如何使用heredoc来做到这一点,但这似乎有点讨厌。我会以错误的方式解决这个问题吗?我不是 php 人,所以我不熟悉正常的做事方式或限制。我知道如何使用 ruby yield 来做到这一点,但在 php 中不知道。
我只想将 html 内容注入一个容器(或多个容器)中,并且我想让 html 成为 html,而不是 heredoc 文本。
谢谢