I'm messing around with templating and I've run into a situation where I need to echo to the browser a template that contains html & php. How do I evaluate the PHP and send it to the browser?
So here's an example (main.php):
    <div id = "container">
        <div id="head">
            <?php if ($id > 10): ?>
                <H3>Greater than 10!</H3>
            <?php else: ?>
                <H3>Less than 10!</H3>
            <?php endif ?>
        </div>
            </div>
And then in the template.php:
   <?php
           $contents; // Contains main.php in string format
           echo eval($contents); // Doesn't work... How do I do this line??
   ?>
EDIT: My template also allows you to inject data from the controller Smarty-style. Would an output buffer allow me to do this and then evaluate my php. The ideal is that it does a first-pass through the code and evaluates all the tags in first, then runs the php. This way I can create loops and stuff from using data sent from my controller.
So maybe a more complete example: 
    <div id = "container">
            <div id = "title">{$title}</div> <!-- This adds data sent from a controller -->
            <div id="head">
                <?php if ($id > 10): ?>
                    <H3>Greater than 10!</H3>
                <?php else: ?>
                    <H3>Less than 10!</H3>
                <?php endif ?>
            </div>
    </div>
Thanks!
Definition and Usage. The eval() function evaluates a string as PHP code. The string must be valid PHP code and must end with semicolon. Note: A return statement will terminate the evaluation of the string immediately. Tip: This function can be useful for storing PHP code in a database.
In case you are trying to do this with a string of mixed HTML/PHP (like from a database, as I was), you can do it this way:
eval(' ?>'.$htmlandphp.'<?php ');
More info: http://blog.5ubliminal.com/posts/eval-for-inline-php-inside-html/ (note this is a dead link as of 2014-3-3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With