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.
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.
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();
?>