php8.5
Home/ Manual/ yaf / yaf_view_interface/ Yaf_View_Interface::render

Yaf_View_Interface::render

PHP function Edit on GitHub ✎

(Yaf >=1.0.0)

Render a template

Description

Yaf_View_Interface::render(string $tpl, array $tpl_vars = NULL): string

Renders a template and returns the result as a string instead of emitting it.

Parameters

tpl

The path to the template, relative to the template directory.

tpl_vars

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

Return Values

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

Examples

Yaf_View_Interface::render example

php
<?php
class MyView implements Yaf_View_Interface
{
    private $tpl_dir;
    private $vars = array();

    public function __construct($template_dir, $options = null)
    {
        $this->tpl_dir = $template_dir;
    }

    public function assign($name, $value = null)
    {
        $this->vars[$name] = $value;
        return true;
    }

    public function display($tpl, $tpl_vars = null)
    {
        echo $this->render($tpl, $tpl_vars);
        return true;
    }

    public function render($tpl, $tpl_vars = null)
    {
        extract($this->vars);
        if ($tpl_vars !== null) {
            extract($tpl_vars);
        }
        ob_start();
        include $this->tpl_dir . "/" . $tpl;
        return ob_get_clean();
    }

    public function setScriptPath($template_dir)
    {
        $this->tpl_dir = $template_dir;
        return $this;
    }

    public function getScriptPath()
    {
        return $this->tpl_dir;
    }
}

/* plug the custom view engine into the dispatcher in the bootstrap */
class Bootstrap extends Yaf_Bootstrap_Abstract
{
    public function _initView(Yaf_Dispatcher $dispatcher)
    {
        $dispatcher->setView(new MyView(APPLICATION_PATH . "/views"));
    }
}
?>

See Also

  • Yaf_View_Interface::display
  • Yaf_View_Interface::assign
  • Yaf_View_Interface::setScriptPath
  • Yaf_View_Interface::getScriptPath

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