Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Kostache-before()方法_Php_Kohana_Kostache - Fatal编程技术网

Php Kostache-before()方法

Php Kostache-before()方法,php,kohana,kostache,Php,Kohana,Kostache,在kostache模块中是否有类似于before()的方法?例如,如果我在视图文件中有两行PHP代码,我希望在视图类中分别执行它们,而不回显模板本身中的任何内容。如何处理该问题?您可以将此类代码放入视图类的构造函数中。当视图被实例化时,代码将运行 下面是一个工作应用程序的(稍加修改)示例。此示例演示了一个ViewModel,可用于更改用作站点主布局的胡子文件。在构造函数中,它选择一个默认布局,如果需要,可以覆盖该布局 控制器: class Controller_Pages extends Con

在kostache模块中是否有类似于before()的方法?例如,如果我在视图文件中有两行PHP代码,我希望在视图类中分别执行它们,而不回显模板本身中的任何内容。如何处理该问题?

您可以将此类代码放入视图类的构造函数中。当视图被实例化时,代码将运行

下面是一个工作应用程序的(稍加修改)示例。此示例演示了一个ViewModel,可用于更改用作站点主布局的胡子文件。在构造函数中,它选择一个默认布局,如果需要,可以覆盖该布局

控制器

class Controller_Pages extends Controller
{
    public function action_show()
    {
        $current_page = Model_Page::factory($this->request->param('name'));

        if ($current_page == NULL) {
            throw new HTTP_Exception_404('Page not found: :page',
                array(':page' => $this->request->param('name')));
        }

        $view = new View_Page;
        $view->page_content = $current_page->Content;
        $view->title = $current_page->Title;

        if (isset($current_page->Layout) && $current_page->Layout !== 'default') {
            $view->setLayout($current_page->Layout);
        }

        $this->response->body($view->render());
    }
}
class View_Page
{
    public $title;

    public $page_content;

    public static $default_layout = 'mytemplate';
    private $_layout;

    public function __construct()
    {
        $this->_layout = self::$default_layout;
    }

    public function setLayout($layout)
    {
        $this->_layout = $layout;
    }

    public function render($template = null)
    {
        if ($this->_layout != null)
        {
            $renderer = Kostache_Layout::factory($this->_layout);
            $this->template_init();
        }
        else
        {
            $renderer = Kostache::factory();
        }

        return $renderer->render($this, $template);
    }
}
视图模型

class Controller_Pages extends Controller
{
    public function action_show()
    {
        $current_page = Model_Page::factory($this->request->param('name'));

        if ($current_page == NULL) {
            throw new HTTP_Exception_404('Page not found: :page',
                array(':page' => $this->request->param('name')));
        }

        $view = new View_Page;
        $view->page_content = $current_page->Content;
        $view->title = $current_page->Title;

        if (isset($current_page->Layout) && $current_page->Layout !== 'default') {
            $view->setLayout($current_page->Layout);
        }

        $this->response->body($view->render());
    }
}
class View_Page
{
    public $title;

    public $page_content;

    public static $default_layout = 'mytemplate';
    private $_layout;

    public function __construct()
    {
        $this->_layout = self::$default_layout;
    }

    public function setLayout($layout)
    {
        $this->_layout = $layout;
    }

    public function render($template = null)
    {
        if ($this->_layout != null)
        {
            $renderer = Kostache_Layout::factory($this->_layout);
            $this->template_init();
        }
        else
        {
            $renderer = Kostache::factory();
        }

        return $renderer->render($this, $template);
    }
}