include 引用了一个文件(模板文件)。然后想把这个文件的结果存为字符串,这个能实现吗?比如<?php
include template('forumthree:block/index');
$str="<div> </div>";
?>include template('forumthree:block/index');输出的结果为
<ul>
<li>XXXX</li>
<li>XXXX</li>
</ul>

现在想把红色的输出转为字符串形式。
然后想实现这种需求:
$str="<div>".这里需要连上include引用的模板的字符串."</div>";
不知如何写呢?
假如这么写:
$str="<div>".include template('forumthree:block/index')."</div>";
显然是不行的

解决方案 »

  1.   

    $s = file_get_contents('forumthree:block/index');
    $str="<div>$s</div>";
      

  2.   

    试了一下,好象不行
    Array ( [2] => [array index_forum_extra/'.$titems[fid].']';if(!empty($_G['setting']['pluginhooks']['index_forum_extra'][$titems[fid]])) echo $_G['setting']['pluginhooks']['index_forum_extra'][$titems[fid]];?>
    直接输出了原形来了,我要的是这个原形出来的html结果。
      

  3.   

    引用的模板中是这么写的
    <!--{hook/index_forum_extra $titems[fid]}-->
    如果用$s = file_get_contents('forumthree:block/index');
    势必输出其原形来,而它输出的html结构是
    [array index_forum_extra/2]这种形式,这才是我要的。
      

  4.   

    forumthree:block/index
    你这里面是身内容?试试$s = file_get_contents(template('forumthree:block/index'));
    $str="<div>$s</div>";
      

  5.   

    不知道你用的什么模板,template()应该是一个封装函数,用来获取模板路径,include再引入该路径.
    既然能include引入,那么用file_get_contents()也是能获取到的.请仔细检查并自行分析,可单独打印template()结果查看得到的是什么.我认为@fdipzone 方法没错.
      

  6.   

    ob_start();
    include template('forumthree:block/index');
    $tp = ob_get_clean();
    $str="<div>$tp</div>";
      

  7.   

    template('forumthree:block/index')
    你这里面是用php处理的吧,不是静态html改成这样就可以了。ob_start();// 开启缓冲区,放在文件最顶
    ob_end_flush(); // 把缓冲区现有的数据先强制输出
    ob_flush();
    include template('forumthree:block/index'); // include 后会把内容输出到缓冲区
    $data = ob_get_contents();  // 获取缓冲区内容,因为之前强制输出过,所以缓冲区内只有include template('forumthree:block/index'); 的数据
    ob_clean(); // 清空缓冲区
    $str = '<div>'.$data.'</div>';
    echo $str;