评估HTML&的PHP Eval;的PHP [英] PHP Eval that evaluates HTML & PHP
问题描述
我搞砸了模板,遇到了一种情况,我需要向浏览器回显包含html&的模板.的PHP.如何评估PHP并将其发送到浏览器?
I'm messing around with templating and I've run into a situation where I need to echo to the browser a template that contains html & php. How do I evaluate the PHP and send it to the browser?
所以这是一个例子(main.php):
So here's an example (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>
然后在template.php中:
And then in the template.php:
<?php
$contents; // Contains main.php in string format
echo eval($contents); // Doesn't work... How do I do this line??
?>
我的模板还允许您从控制器Smarty样式注入数据.将输出缓冲区允许我执行此操作,然后评估我的php.理想的情况是,它首先执行代码,然后首先评估所有标记,然后运行php.这样,我就可以使用控制器发送的数据来创建循环和内容.
So maybe a more complete example:
<div id = "container">
<div id = "title">{$title}</div> <!-- This adds data sent from a controller -->
<div id="head">
<?php if ($id > 10): ?>
<H3>Greater than 10!</H3>
<?php else: ?>
<H3>Less than 10!</H3>
<?php endif ?>
</div>
</div>
谢谢!
推荐答案
如果您尝试使用混合HTML/PHP字符串(例如从数据库访问),可以这样做:
In case you are trying to do this with a string of mixed HTML/PHP (like from a database, as I was), you can do it this way:
eval(' ?>'.$htmlandphp.'<?php ');
更多信息: http://blog.5ubliminal. com/posts/eval-for-inline-php-inside-html/(请注意,这是自2014年3月3日起的无效链接)
More info: http://blog.5ubliminal.com/posts/eval-for-inline-php-inside-html/ (note this is a dead link as of 2014-3-3)
这篇关于评估HTML&的PHP Eval;的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!