douwen3965 2012-03-29 19:07 采纳率: 0%
浏览 47

是否可以在ob_start缓冲区中使用php标签?

I am trying to make complex template library. The thing is that I am holding my own syntax and php inside the same file as mixed.

  1. First my syntax is parsed and reformatted. Output is usual html + php as string. I don't want to make cache file but use string containing the "middle" template instead.

  2. On this template (string containing php tags and html) the php operations should be executed.

MyComplexTemplate(my syntax + php) =parse=> Template(HTML+PHP) =exec php=> Result(HTML)

For 2. I am trying to use ob_start() with echo, but everything inside php tags gets vanished.

For example:

<?php
  $asd = "123";
  ob_start();
  echo "<div><?php echo $asd; ?></div>"; // Middle template
  $result = ob_get_contents();
  ob_end_clean();

  echo $result;
?>

The result should be "<div>123</div>". At the moment I get only div tags.

This is just a simple example. My template will contain all kind of php operations inside php tags.

Is this kind of operation possible somehow?

UPDATE

Template i.e.: (template.php)

.mySyntax (...) and so on
  <?php foreach($asd as $data): ?>
  <div><?php echo $data ?></div>
  <?php endforeach; ?>
  .mySyntax
    some text

Parsing template with MySyntaxParser and executing php after that, which is left untouched:

<?php
  $t = new MySyntaxParser("template.php"); // My parser. Converts mySyntax to html.
  $asd = array("test1", "test2");

  ob_start();
  echo $t->parse(); // Should execute php code inside the string;
  $result = ob_get_contents();
  ob_end_clean();

  echo $result;
?>

UPDATE

I want to achieve allmost exact thing like here but require_once should somehow come from string:

SOLUTION

Actually the solution is in this topic PHP: Equivalent of include using eval . eval must be used with pre and postpadded with php tags.

Also you should check out THIS blogpost.

Like here:

<?php
  $asd = "123";
  ob_start();
  eval ('?>
    <?php foreach(array("asd", "asx123") as $data): ?>
    <div><?php echo $data ?></div>
    <?php endforeach; ?>
  <?php;');
  ob_flush();
?>
  • 写回答

3条回答 默认 最新

  • dongmeixi5311 2012-03-29 19:09
    关注

    You don't need PHP tags. Just:

    echo "<div>$asd</div>"; // Middle template
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么