Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to use php tags inside ob_start buffer?
    text
    copied!<p>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.</p> <ol> <li><p>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.</p></li> <li><p>On this template (string containing php tags and html) the php operations should be executed.</p></li> </ol> <p>MyComplexTemplate(my syntax + php) =parse=> Template(HTML+PHP) =exec php=> Result(HTML)</p> <p>For 2. I am trying to use ob_start() with echo, but everything inside php tags gets vanished.</p> <p>For example:</p> <pre class="lang-php prettyprint-override"><code>&lt;?php $asd = "123"; ob_start(); echo "&lt;div&gt;&lt;?php echo $asd; ?&gt;&lt;/div&gt;"; // Middle template $result = ob_get_contents(); ob_end_clean(); echo $result; ?&gt; </code></pre> <p>The result should be "&lt;div&gt;123&lt;/div&gt;". At the moment I get only div tags.</p> <p>This is just a simple example. My template will contain all kind of php operations inside php tags.</p> <p>Is this kind of operation possible somehow?</p> <h3>UPDATE</h3> <p>Template i.e.: (template.php) </p> <pre><code>.mySyntax (...) and so on &lt;?php foreach($asd as $data): ?&gt; &lt;div&gt;&lt;?php echo $data ?&gt;&lt;/div&gt; &lt;?php endforeach; ?&gt; .mySyntax some text </code></pre> <p>Parsing template with MySyntaxParser and executing php after that, which is left untouched: </p> <pre><code>&lt;?php $t = new MySyntaxParser("template.php"); // My parser. Converts mySyntax to html. $asd = array("test1", "test2"); ob_start(); echo $t-&gt;parse(); // Should execute php code inside the string; $result = ob_get_contents(); ob_end_clean(); echo $result; ?&gt; </code></pre> <h3>UPDATE</h3> <p>I want to achieve allmost exact thing like <a href="https://stackoverflow.com/a/1309805/878435">here</a> but require_once should somehow come from string:</p> <h1><strong>SOLUTION</strong></h1> <p>Actually the solution is in this topic <a href="https://stackoverflow.com/questions/1184628/php-difference-include-eval">PHP: Equivalent of include using eval</a> . eval must be used with pre and postpadded with php tags.</p> <p>Also you should check out <a href="http://www.intechgrity.com/understanding-php-eval-function-and-usage-do-more-than-just-evaluating/" rel="nofollow noreferrer"><strong>THIS</strong></a> blogpost.</p> <p>Like here: </p> <pre><code>&lt;?php $asd = "123"; ob_start(); eval ('?&gt; &lt;?php foreach(array("asd", "asx123") as $data): ?&gt; &lt;div&gt;&lt;?php echo $data ?&gt;&lt;/div&gt; &lt;?php endforeach; ?&gt; &lt;?php;'); ob_flush(); ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload