在PHP,定界符串声明是真正有用的输出一块html。你可以拥有这一分析中的变量只是通过他们的前缀美元,但对于更复杂的语法(如$var[2][3]),你必须把你的表情里面{}括号。

在PHP5, 可能实际上使功能的电话内{}括号内的定界符串的,但你必须要经过一点点的工作。该职能名称本身已被储存在一个可变的,你有把它像是一个动态命名的功能。例如:

$fn = 'testfunction';
function testfunction() { return 'ok'; }
$string = <<< heredoc
plain text and now a function: {$fn()}
heredoc;

正如你可以看到,这是一个比较凌乱的不仅仅是:

$string = <<< heredoc
plain text and now a function: {testfunction()}
heredoc;

还有其他的方法除了第一码的例子,例如打破了定界符叫功能,或逆转的问题和做喜欢的东西:

?>
<!-- directly output html and only breaking into php for the function -->
plain text and now a function: <?PHP print testfunction(); ?>

后者具有的缺点是产出的直接投入流输出(除非我用output buffering),这可能不是我想要什么。

因此,我的问题是:是否有一个更优雅的方式来处理这个?

编辑基础上的对策: 它肯定不会看起来像某种模板发动机将会使我的生活变得更加容易,但是它将需要我基本上反向我通常PHP的风格。不,那是坏事,但它解释了我的惯性..我起来找出方法来让生活更容易的,所以我要找到模板。

有帮助吗?

解决方案

我不会使用定界符,在所有用于这一点,个人。它只是没有一个良好的"模板建筑物"系统。所有你HTML是锁定在一串,其中有几个缺点

  • 没有选择对于所见即所得
  • 没有代码的完成HTML从IDEs
  • 输出(HTML)锁定逻辑的文件
  • 你最终不必使用黑客喜欢什么你想现在要做,以实现更复杂的模板,如循环

得到一个基本的模板发动机,或只是使用PHP与包括-这就是为什么语言都有的 <?php?> 符。

template_file.php

<html>
<head>
  <title><?php echo $page_title; ?></title>
</head>
<body>
  <?php echo getPageContent(); ?>
</body>

index.php

<?php

$page_title = "This is a simple demo";

function getPageContent() {
    return '<p>Hello World!</p>';
}

include('template_file.php');

其他提示

如果你真的想这样做,但比使用类更简单:

function fn($data) {
  return $data;
}
$fn = 'fn';

$my_string = <<<EOT
Number of seconds since the Unix Epoch: {$fn(time())}
EOT;

我会做以下事情:

$string = <<< heredoc
plain text and now a function: %s
heredoc;
$string = sprintf($string, testfunction());

不确定你是否认为这更优雅......

尝试此操作(作为全局变量,或在需要时实例化):

<?php
  class Fn {
    public function __call($name, $args) {
      if (function_exists($name)) {
        return call_user_func_array($name, $args);
      }
    }
  }

  $fn = new Fn();
?>

现在任何函数调用都通过$fn实例。因此可以使用testfunction()

在heredoc中调用现有函数{$fn->testfunction()}

基本上我们将所有函数包装到一个类实例中,并使用PHP的__call magic方法将类方法映射到需要调用的实际函数。

我有点晚了,但我随机发现了它。对于任何未来的读者,这是我可能会做的:

我只想使用输出缓冲区。所以基本上,你使用ob_start()开始缓冲,然后包含<!>“模板文件<!>”;使用其中的任何函数,变量等,获取缓冲区的内容并将它们写入字符串,然后关闭缓冲区。然后,您已经使用了所需的任何变量,可以运行任何函数,并且您仍然可以在IDE中使用HTML语法高亮显示。

这就是我的意思:

模板文件:

<?php echo "plain text and now a function: " . testfunction(); ?>

<强>脚本:

<?php
ob_start();
include "template_file.php";
$output_string = ob_get_contents();
ob_end_clean();
echo $output_string;
?>

因此脚本将template_file.php包含到其缓冲区中,运行任何函数/方法并在此过程中分配任何变量。然后,您只需将缓冲区的内容记录到变量中,然后使用它执行所需的操作。

这样,如果您不想在第二天将其回显到页面上,那么您不必这样做。您可以在输出之前循环并继续添加到字符串中。

如果您不想使用模板引擎,我认为这是最好的方法。

为完整起见,您还可以使用!${''} 黑魔法 解析器破解

echo <<<EOT
One month ago was ${!${''} = date('Y-m-d H:i:s', strtotime('-1 month'))}.
EOT;

此代码段将在userscope中定义具有已定义函数名称的变量,并将它们绑定到包含相同名称的字符串。让我演示一下。

function add ($int) { return $int + 1; }
$f=get_defined_functions();foreach($f[user]as$v){$$v=$v;}

$string = <<< heredoc
plain text and now a function: {$add(1)}
heredoc;

现在可以工作了。

我认为使用heredoc非常适合生成HTML代码。例如,我发现以下几乎完全不可读。

<html>
<head>
  <title><?php echo $page_title; ?></title>
</head>
<body>
  <?php echo getPageContent(); ?>
</body>

但是,为了实现简单性,您必须在开始之前评估功能。我不相信这是一个如此严重的约束,因为这样做,你最终将你的计算与显示分开,这通常是一个好主意。

我认为以下内容非常易读:

$page_content = getPageContent();

print <<<END
<html>
<head>
  <title>$page_title</title>
</head>
<body>
$page_content
</body>
END;

不幸的是,即使你在提问中将函数绑定到变量是一个很好的建议,但最终会增加代码的复杂程度,这是不值得的,并且使代码的可读性降低,这是heredoc的主要优势。

在这里找到了包装功能很好的解决方案: http://blog.nazdrave.net/?p= 626个

function heredoc($param) {
    // just return whatever has been passed to us
    return $param;
}

$heredoc = 'heredoc';

$string = <<<HEREDOC
\$heredoc is now a generic function that can be used in all sorts of ways:
Output the result of a function: {$heredoc(date('r'))}
Output the value of a constant: {$heredoc(__FILE__)}
Static methods work just as well: {$heredoc(MyClass::getSomething())}
2 + 2 equals {$heredoc(2+2)}
HEREDOC;

// The same works not only with HEREDOC strings,
// but with double-quoted strings as well:
$string = "{$heredoc(2+2)}";

我会看一下 Smarty 作为模板引擎 - 我还没试过任何其他的我自己,但它做得很好。

如果您想坚持使用当前的方法 sans 模板,那么输出缓冲有什么不好?它比你必须声明变量更具灵活性,这些变量是你想要调用的函数的刺激名称。

有点晚但仍然。 这在heredoc中是可能的!

浏览一下php手册,部分<!> quot;复杂(卷曲)语法<!> quot;

这是使用@CJDennis提案的一个很好的例子:

function double($i)
{ return $i*2; }

function triple($i)
{ return $i*3;}

$tab = 'double';
echo "{$tab(5)} is $tab 5<br>";

$tab = 'triple';
echo "{$tab(5)} is $tab 5<br>";

例如,HEREDOC语法的一个很好的用途是在数据库中生成具有主 - 细节关系的巨大形式。可以在FOR控件中使用HEREDOC功能,在每个字段名称后添加后缀。这是典型的服务器端任务。

你忘记了lambda功能:

$or=function($c,$t,$f){return$c?$t:$f;};
echo <<<TRUEFALSE
    The best color ever is {$or(rand(0,1),'green','black')}
TRUEFALSE;

您也可以使用函数create_function

大家应该注意它也适用于双引号字符串。

http://www.php.net/manual/en /language.types.string.php

无论如何,

有趣的提示。

<div><?=<<<heredoc
Use heredoc and functions in ONE statement.
Show lower case ABC="
heredoc
. strtolower('ABC') . <<<heredoc
".  And that is it!
heredoc
?></div>
<?php
echo <<<ETO
<h1>Hellow ETO</h1>
ETO;
你应该试一试。在结束ETO之后;命令你应该输入。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top