我正在使用(或至少绑定到) PHP函数作为模板引擎。我已经实现了外部调用字符串,它可以在HEREDOC中直接处理外部函数,并且工作成功。
我现在面临的问题是,某些函数的顺序似乎优先并首先执行,而不考虑特定HEREDOC中的其他函数和/或代码。
怎么解决这个问题?
(请注意,我是个PHP初学者。我做了作业,但找不到解决办法。谢谢,)
职能方案:
function heredoc($input)
{
return $input;
}
$heredoc = "heredoc";HEREDOC模板:
function splicemaster_return_full_page()
{
global $heredoc;
$title ="This is document title";
echo <<<HEREDOC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{$heredoc(splice_html_title($title))}
</head>
<body>
{$heredoc(splicemaster_return_message())}
{$heredoc(splice_quick_add_article_form())}
{$heredoc(display_all_articles_in_a_html_table())}
</body>
</html>
HEREDOC;
}当前的问题是"{$heredoc(display_all_articles_in_a_html_table())}“调用,它比其他任何东西都要先输出,从而导致HTML崩溃。
如果你对我的帮助表示感谢,我已经用这个敲了很长一段时间。
更新:
使用评论中的内容,我试图做其他的事情,但这是丑陋的地狱,我可能会有问题,编辑这一天以后。
function testout()
{
$title = "This is document title";
echo "<!DOCTYPE html>";
echo '<html lang="en">';
echo "<head>";
echo '<meta charset="utf-8">';
echo "<title>". $title . "</title>";
echo "</head>";
echo "<body>";
echo splicemaster_return_message();
echo splice_quick_add_article_form();
echo display_all_articles_in_a_html_table();
echo "</body>";
echo "</html>";
}(它的外观并不重要--我有一个HTML处理器函数。)
更新2
好的,所以我发现了“脏”的修复,这并不能解释为什么引擎会像它一样工作。(我还在另一台机器上进行了测试,用的是diff。( php):
function splicemaster_return_full_page()
{
global $heredoc;
$title ="This is document title";
echo <<<HEREDOC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{$heredoc(splice_html_title($title))}
</head>
<body>
{$heredoc(splicemaster_return_message())}
{$heredoc(splice_quick_add_article_form())}
HEREDOC;
echo <<<HEREDOC
{$heredoc(display_all_articles_in_a_html_table())}
</body>
</html>
HEREDOC;
}发布于 2014-10-03 19:39:36
我在其他网站上问了这个(类似的)问题,同时想知道为什么会发生这种情况,并找到了罪魁祸首。
问题在于调用的函数回显(或打印)输出,而不是返回它。当我切换到返回时,代码会适当地输出。
发布于 2014-10-02 17:07:25
你不应该在这里使用。或者真正尝试在函数中呈现整个html文档。这就是如何用php来呈现html。注意:我也非常肯定,您不能在heredoc语句中调用函数。
<?php $title = "This is document title"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<?php echo splice_html_title($title); ?>
</head>
<body>
<?php
echo splicemaster_return_message()
. splice_quick_add_article_form()
. display_all_articles_in_a_html_table();
?>
</body>
</html>您可以看到这是多么清洁,这使它更容易编辑,当需要。例如,您只需将其放入文件“page.php”中即可。
include_once('page.php');并将其包含在任何您将其称为splicemaster_return_full_page函数的地方。
https://stackoverflow.com/questions/26165640
复制相似问题