2016-01-04 31 views
4

我需要在运行时将PHP文件渲染或评估为PHP字符串变量。 file_get_contents()会读取内容,但不会评估PHP代码。将PHP文件渲染为字符串变量

我知道ob_start()解决方案(如此处所述:Get render HTML from local PHP file),但它感觉很脏。我希望有更直接和干净的事情。

什么我要完成

例子:

test.php的

<?php 

for ($i = 0; $ < 5; $i++) { 
    echo '<p>' . $i . '</p>\n'; 
} 

我的代码:

<?php 

$string = render_php('test.php'); 

/* 
    Content of $string: 
    <p>0</p> 
    <p>1</p> 
    <p>2</p> 
    <p>3</p> 
    <p>4</p> 
*/ 
+1

看看http://php.net/manual/en/function.eval.php。例如我想你想要'$ string = eval(file_get_contents('test.php'));'你需要取出'<?php'并更正'$ <5'。 – chris85

+9

“我知道ob_start()解决方案,但它感觉很脏”为什么?据我所见,这就是你想要的? – PeeHaa

+2

在这种情况下,@Peehaa是对的 - 你有你的解决方案,并且它有一个存在于php中的理由 - 供你使用它。 – somethinghere

回答

13

正如其他人所提到的输出缓冲可能是干净的解决方案在这里,因为它可以让你的HTML模板远离你的逻辑中分离出来。这样你最终会在模板文件中获得相当可读的html,而不是意大利面条代码混乱。通过添加第二个参数(数组或对象,即

render_php('test.php'); 

你甚至可以让这个更可重复使用:

function render_php($path) 
{ 
    ob_start(); 
    include($path); 
    $var=ob_get_contents(); 
    ob_end_clean(); 
    return $var; 
} 

然后创建模板文件

//test.php 
<?php for($i = 0; $i<5; $i++):?> 
    <p><?php echo $i;?></p> 
<?php endfor ?> 

然后调用你的函数

function render_php($path,array $args){ 
    ob_start(); 
    include($path); 
    $var=ob_get_contents(); 
    ob_end_clean(); 
    return $var; 
} 

现在让我们看看这是怎么有用

//create your template test.php 
<?php for($i = $args['start']; $i<$args['end']; $i++):?> 
    <p><?php echo $i;?></p> 
<?php endfor ?> 

现在创建你的论点,并通过他们关闭的渲染方法

$args = array('start' => 0, 'end' => 5); 
render_php('test.php', $args); 

为什么这是有用的

现在你有一个可重复使用的功能这是非常有用的,不管你需要传递多少个参数,并且你的逻辑可以在你的显示器的独立文件中,使你的代码更具可读性。我们可以使用它来构建仍然易于阅读的大块html。

$article = array( //imagine we have an article that we have pulled from our database 
'title' => 'Some Title', 
'subtitle' => 'Some Sub Title', 
'body' => 'lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris 
    eu nulla quis ligula ornare ultricies. Vivamus malesuada lectus a mi 
    auctor pellentesque. Maecenas eu ultricies sapien, ac porta augue. ', 
'image' => 'img/some_image.jpg' 
); 

echo render_php('article.php',array $article); 

,并创建一个模板

<!-- article.php --> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title><?php echo $article['title']; ?></title> 
    </head> 
    <body> 
     <img src='<?php echo $article['image']; ?>' alt='whatever' > 
     <h1><?php echo $article['title']; ?></h1> 
     <h2><?php echo $article['subtitle']; ?></h2> 
     <p><?php echo $article['body'];?></p> 

    </body> 
</html> 
-1

对于示例一个简单的实现你想要的结果可能是途径;

test.php的

<?php 

function render_html(){ 
    $string = ''; 
    for ($i = 0; $i < 5; $i++) { 
     $string .= '<p>' . $i . "</p>\n"; 
    } 
    return $string; 
} 

代码:

<?php 

include "test.php"; 
$string = render_html(); 

echo $string; 
+0

这不起作用,因为包含的输出没有被缓冲到一个字符串中,所以它不能完成它在罐子上所说的内容,并且不能解决问题。 – somethinghere

+0

原始问题只提到不想使用php缓冲区函数。虽然我同意评论说没有理由避免使用它们。我试图提供一个不使用它们的简单例子;正如问题所问。 (我从我的代码中的原始问题中复制了一个错字,现在已更正,并且还在双引号中添加了换行符。) – dading84

+0

_But它不能解决问题_。我还想补充说,这不是一个给出一些限制的答案,它给出了最好的答案来解决问题 - 在这种情况下,这就是'ob'。在给定的约束条件下,“ob”是最好的解决方案。 – somethinghere