替换定界符语法
我是 php 新手,我有一个模板系统,最初我使用了heredoc语法,但后来由于多种原因决定放弃它。
我有一些看起来像这样的东西:
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" method="post">
<label for="title">Title:</label><br />
<input name="title" id="title" type="text" maxlength="220" />
<div class="clear"></div>
<label for="bodytext">Body Text:</label><br />
<textarea name="bodytext" id="bodytext"></textarea>
<input type="submit" value="Create This Entry!" />
</form>
ADMIN_FORM; }
我使用以下方式在页面上调用它:
$obj = new myCMS();
echo $obj->display_admin();
如何替换heredoc语法?是否有另一种有效的方法可以将大块 HTML 插入到 PHP 函数中?我知道一定有一个简单的解决方案,但每次我用谷歌搜索时,我都会得到似乎更适合插入单行 HTML 而不是整个块的答案。
Im new to php and I have a templating system in which I orignally used heredoc syntax but have since decided to get rid of it for a number of reasons.
I have something that looks like this:
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" method="post">
<label for="title">Title:</label><br />
<input name="title" id="title" type="text" maxlength="220" />
<div class="clear"></div>
<label for="bodytext">Body Text:</label><br />
<textarea name="bodytext" id="bodytext"></textarea>
<input type="submit" value="Create This Entry!" />
</form>
ADMIN_FORM; }
And I call it on a page using this:
$obj = new myCMS();
echo $obj->display_admin();
How can I replace the heredoc syntax? Is there another efficient method for inserting large blocks of HTML into a PHP function? I know there must be a simple solution, here but every time I google it, I get answers that seem better suited for inserting single lines of HTML, not whole blocks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,没有太多理由将大块 HTML 插入 PHP 函数。
调用
$obj->display_admin();
没有多大意义。而使
。
其次,heredoc绝对不是一个模板解决方案。
而 PHP 本身就是。
因此,创建一个文件
admin_form.tpl.php
并将其包含在适合的位置 - 最有可能包含在您的模板类中。
First of all, there are not much reasons for inserting large blocks of HTML into a PHP function.
calling
$obj->display_admin();
makes not much sense.while
makes.
Next, heredoc is absolutely not a templating solution.
While PHP itself is.
So, make a file
admin_form.tpl.php
and just include it where it fits - most likely in your templating class.
HEREDOCS 是此类多行文本构建的唯一实用选择。您唯一的其他选择是逐行连接,或“打破”PHP 模式并使用一些输出缓冲。
在这 3 个中,heredocs 更有用。您不必担心文本正文中的引号转义,并且使用变量函数,您现在甚至可以在文本中执行函数调用来执行诸如
htmlspecialchars()
调用之类的操作,而无需预先执行在 Heredoc 块之外定义任何此类文本。与
评论后续:
没有什么说你不能连接heredocs:
毕竟,heredoc只是生成一个字符串,其中恰好有多行文本。您可以将单个定界文档拆分为两个或多个部分,并将条件
if()
内容放在两个部分之间。另一方面,使用变量函数,您可以轻松地在单个定界文档中插入条件文本
:
HEREDOCS are the only practical choice of multi-line text building of this sort. Your only other options would be to line-by-line concatenation, or "break" out of PHP mode and use some output buffering.
Of the 3, heredocs are far more useable. You don't have to worry about escaping quotes within the text body, and with variable functions, you can even do function calls within the text now to do stuff like
htmlspecialchars()
calls without having to pre-define any such text outside the heredoc block.vs.
comment followup:
Nothing says you can't concatenate heredocs:
After all, a heredoc simply produces a string that happens to have multiple lines of text in it. You can split the monolithic single heredoc into two or more sections and put your conditional
if()
stuff in between the two.On the other hand, with the variable functions, you can trivially whip one up to insert the conditional text within a single heredoc:
v.s.