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

Yaf_View_Simple::render

PHP function Edit on GitHub ✎

(Yaf >=1.0.0)

Render a template

Description

Yaf_View_Simple::render(string $tpl, array $vars = NULL): string

Renders a template and returns the result as a string. The template is included as a plain PHP script, so any PHP code in it is executed, and assigned variables are available under their names.

Note

Unlike Yaf_View_Simple::display, the result is returned instead of being appended to the Yaf_Response_Abstract body.

Parameters

tpl

The path to the template, relative to the template directory set by Yaf_View_Simple::setScriptPath or application.view.directory.

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::render example

php
<?php
class ProductController extends Yaf_Controller_Abstract
{
    public function detailAction()
    {
        /* render the template and post-process the result */
        $html = $this->getView()->render("product/detail.phtml", array(
            "name"  => "Yaf in Action",
            "price" => "29.90",
        ));

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

Template example

php
<h1><?php echo $name; ?></h1>
<p>Price: <?php echo $price; ?> EUR</p>

The above example will output something similar to:

output
<h1>Yaf in Action</h1>
<p>Price: 29.90 EUR</p>

See Also

  • Yaf_View_Simple::display
  • Yaf_View_Simple::eval
  • Yaf_View_Simple::assign

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