php8.5
Home/ Manual/ yaf / yaf_view_simple/ Yaf_View_Simple::eval

Yaf_View_Simple::eval

PHP function Edit on GitHub ✎

(Yaf >=2.2.0)

Render a template string

Description

Yaf_View_Simple::eval(string $tpl_content, array $vars = NULL): string

Renders a template given as a string and returns the result.

Note

Since no template file is involved, the template directory is not consulted.

Parameters

tpl_content

The template content as a string.

vars

Optional variables to assign for this render; they override assigned variables with the same name.

Return Values

The rendered result as a string, or false on failure.

Examples

Yaf_View_Simple::eval example

php
<?php
class IndexController extends Yaf_Controller_Abstract
{
    public function indexAction()
    {
        $template = "<h1><?php echo htmlspecialchars($title); ?></h1>";

        /* render the template string without touching the filesystem */
        $html = $this->getView()->eval($template, array("title" => "Breaking News"));

        $this->getResponse()->setBody($html);
        return false; // skip auto-rendering
    }
}
?>

The above example will output something similar to:

output
<h1>Breaking News</h1>

See Also

  • Yaf_View_Simple::render
  • Yaf_View_Simple::display

Source: reference/yaf/yaf_view_simple/eval.xml · from the official PHP manual (php/doc-en)