Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
评估HTML的PHP评估&;PHP_Php_Templates - Fatal编程技术网

评估HTML的PHP评估&;PHP

评估HTML的PHP评估&;PHP,php,templates,Php,Templates,我正忙于模板制作,我遇到了一个需要向浏览器回显包含html和php的模板的情况。如何评估PHP并将其发送到浏览器 下面是一个示例(main.php): 大于10! 不到10个! 然后在template.php中: <?php $contents; // Contains main.php in string format echo eval($contents); // Doesn't work... How do I do this

我正忙于模板制作,我遇到了一个需要向浏览器回显包含html和php的模板的情况。如何评估PHP并将其发送到浏览器

下面是一个示例(main.php):


大于10!
不到10个!
然后在template.php中:

   <?php
           $contents; // Contains main.php in string format
           echo eval($contents); // Doesn't work... How do I do this line??
   ?>

改用输出缓冲
eval()
的速度是出了名的慢

main.php:

    <div id = "container">
        <div id="head">
            <?php if ($id > 10): ?>
                <H3>Greater than 10!</H3>
            <?php else: ?>
                <H3>Less than 10!</H3>
            <?php endif ?>
        </div>
            </div>
其输出将为:

同侧眼睑

大于10


不要读取该文件,而是将其包括在内,并使用output bufferig捕获结果

ob_start();
include 'main.php';
$content = ob_get_clean();

// process/modify/echo $content ...
编辑

使用函数生成新的变量范围

function render($script, array $vars = array())
{
    extract($vars);

    ob_start();
    include $script;
    return ob_get_clean();
}

$test = 'one';
echo render('foo.php', array('test' => 'two'));
echo $test; // is still 'one' ... render() has its own scope

如果您试图使用一个HTML/PHP混合字符串(就像我以前从数据库中看到的那样)来实现这一点,您可以这样做:

eval(' ?>'.$htmlandphp.'<?php ');

eval(“?>”.$htmlandphp.在您的情况下,最好的解决方案是将
eval
output buffer

// read template into $contents
// replace {title} etc. in $contents
$contents = str_replace("{title}", $title, $contents);
ob_start();
    eval(" ?>".$contents."<?php ");
$html .= ob_get_clean();
echo html;
//将模板读入$contents
//替换$contents中的{title}等
$contents=str_replace(“{title}”,$title,$contents);
ob_start();

评估(“?>”)$内容。"嗯..那么我该如何注入数据呢?有很多种方法。在$content上进行字符串替换,在包含它之前让main.php可以使用globals,这个列表会一直持续下去。我认为这样不行..Include会在你有机会进行字符串替换之前抛出错误。是的,你可以使用你已经在模板中声明的变量。你不能t、 但是,像您那样使用{$title}-这只会逐字显示文本。您必须告诉模板这是PHP。我已经更新了我的答案以演示。
ob_start()
…哇!每天都有新的东西!我喜欢StackOverlow!我热爱php!如此强大的语言…为了提高你答案的质量,请包括你的帖子将如何/为什么解决这个问题。
function render($script, array $vars = array())
{
    extract($vars);

    ob_start();
    include $script;
    return ob_get_clean();
}

$test = 'one';
echo render('foo.php', array('test' => 'two'));
echo $test; // is still 'one' ... render() has its own scope
eval(' ?>'.$htmlandphp.'<?php ');
$contents = htmlentities($contents);
echo html_entity_decode(eval($contents));
// read template into $contents
// replace {title} etc. in $contents
$contents = str_replace("{title}", $title, $contents);
ob_start();
    eval(" ?>".$contents."<?php ");
$html .= ob_get_clean();
echo html;