替换定界符语法

发布于 2024-10-27 22:59:07 字数 911 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倚栏听风 2024-11-03 22:59:07

首先,没有太多理由将大块 HTML 插入 PHP 函数。
调用 $obj->display_admin(); 没有多大意义。
而使

$data = array('bodytext' => $text);
$obj->set_data($data);
$obj->set_template('admin_form')
$obj->display();

其次,heredoc绝对不是一个模板解决方案。
而 PHP 本身就是。

因此,创建一个文件 admin_form.tpl.php

<form action="" 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"><?=$data['bodytext']?></textarea>
<? if ($data['id']): ?>
  <input name="id" id="id" type="hidden" value="<?=$data['id']?>" />
<? endif ?>
  <input type="submit" value="Create This Entry!" />
</form>

并将其包含在适合的位置 - 最有可能包含在您的模板类中。

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

$data = array('bodytext' => $text);
$obj->set_data($data);
$obj->set_template('admin_form')
$obj->display();

makes.

Next, heredoc is absolutely not a templating solution.
While PHP itself is.

So, make a file admin_form.tpl.php

<form action="" 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"><?=$data['bodytext']?></textarea>
<? if ($data['id']): ?>
  <input name="id" id="id" type="hidden" value="<?=$data['id']?>" />
<? endif ?>
  <input type="submit" value="Create This Entry!" />
</form>

and just include it where it fits - most likely in your templating class.

昨迟人 2024-11-03 22:59:07

HEREDOCS 是此类多行文本构建的唯一实用选择。您唯一的其他选择是逐行连接,或“打破”PHP 模式并使用一些输出缓冲。

$text = "blah blah blah";
$text .= "blah blah blah";
$text .= "blah blah blah";

ob_start(); ?>
blah blah blah
blah blah blah
blah blah blah
<? $text = ob_get_clean(); 

$text = <<<EOL
blah blah blah
blah blah blah
blah blah blah
EOL;

在这 3 个中,heredocs 更有用。您不必担心文本正文中的引号转义,并且使用变量函数,您现在甚至可以在文本中执行函数调用来执行诸如 htmlspecialchars() 调用之类的操作,而无需预先执行在 Heredoc 块之外定义任何此类文本。

$formfield = htmlspecialchars($_POST['somefield']);
$txt = <<<EOL
<input type="text" value="$formfield" />
EOL;

$hsc = function($x) { return htmlspecialchars($x); };

$txt = <<<EOL
<input type="text" value="{$hsc($_POST['somefield'])}" />
EOL;

评论后续:

没有什么说你不能连接heredocs:

$txt = <<<EOL
blah blah blah
EOL;

$txt .= <<<EOL
more blah
EOL;

毕竟,heredoc只是生成一个字符串,其中恰好有多行文本。您可以将单个定界文档拆分为两个或多个部分,并将条件 if() 内容放在两个部分之间。

另一方面,使用变量函数,您可以轻松地在单个定界文档中插入条件文本

$conditional = function($parameter) { return (($parameter == 1) ? 'Success' : 'Failure'); }

$txt = <<EOL
Your login attempt
resulted in {$conditional($somevalue)}.
How does that make you feel?
EOL;

$txt = <<<EOL
Your login attemp
resulted in
EOL;
$txt .= (($somevalue == 1) ? 'Success' : 'Failure');
$txt .= <<<EOL
.
How does that make you feel?
EOL;

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.

$text = "blah blah blah";
$text .= "blah blah blah";
$text .= "blah blah blah";

ob_start(); ?>
blah blah blah
blah blah blah
blah blah blah
<? $text = ob_get_clean(); 

$text = <<<EOL
blah blah blah
blah blah blah
blah blah blah
EOL;

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.

$formfield = htmlspecialchars($_POST['somefield']);
$txt = <<<EOL
<input type="text" value="$formfield" />
EOL;

vs.

$hsc = function($x) { return htmlspecialchars($x); };

$txt = <<<EOL
<input type="text" value="{$hsc($_POST['somefield'])}" />
EOL;

comment followup:

Nothing says you can't concatenate heredocs:

$txt = <<<EOL
blah blah blah
EOL;

$txt .= <<<EOL
more blah
EOL;

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:

$conditional = function($parameter) { return (($parameter == 1) ? 'Success' : 'Failure'); }

$txt = <<EOL
Your login attempt
resulted in {$conditional($somevalue)}.
How does that make you feel?
EOL;

v.s.

$txt = <<<EOL
Your login attemp
resulted in
EOL;
$txt .= (($somevalue == 1) ? 'Success' : 'Failure');
$txt .= <<<EOL
.
How does that make you feel?
EOL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文