PHP 8.6.0 Beta 2 available for testing

Yaf_View_Interface::assign

(Yaf >=1.0.0)

Yaf_View_Interface::assignAssign a variable to the view

Description

abstract public function Yaf_View_Interface::assign(mixed $name, mixed $value = NULL): bool

Assigns a variable to the view. Assigned variables are available to the template under their names.

Parameters

name

The name under which the variable becomes available to the template.

value

The value to assign.

Return Values

The return value is defined by the concrete implementation. Yaf_View_Simple, the default view engine, returns the view instance.

Examples

Example #1 Yaf_View_Interface::assign() example

<?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

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top