atodd
atodd

Reputation: 331

How do I render a Zend_View script from a string instead of a file

I currently have a CMS that writes some pages in a database table. In order to render them with Zend_View I have a method that writes them to the filesystem. I'd like to skip that process and render the templates directly from the database.

For example:

<?php
$template = '<html>
<head>
<title>Test</title>
</head>
<body>
<?php echo $this->test ?>
</body>
</html>';

$view = new Zend_View();
$view->test = 'This is a test';
echo $view->render($template);
?>

Upvotes: 2

Views: 548

Answers (1)

baconpiex
baconpiex

Reputation: 41

Zend_View extends Zend_View_Abstract and declares a concrete implementation of the _run() method (which is invoked by render()). sic:

protected function _run()
{
    include func_get_arg(0);
}

I guess what you want is basically:

class Zend_View_String extends Zend_View // or maybe // extends Zend_View_Abstract
{
    protected function _run()
    {
        $php = func_get_arg(0);

        eval(' ?>'. $php. '<?php ');
    }
}

But that might be slower than writing it to a file and calling include. You can put your file dumping code inside your own _run method instead. Doing so is left as an exercise for the reader.

Upvotes: 4

Related Questions