简体   繁体   English

将新的上下文变量添加到锂\\模板\\视图\\适配器\\文件

[英]Add new context variables to lithium\template\view\adapter\File

I'd like to replicate the functionality of the $this->head() and $this->scripts() handlers to add $this->footer() . 我想复制$this->head()$this->scripts()处理程序的功能,以添加$this->footer() I understand that I can use Renderer::handlers() to register a new handler, but I'm not sure how I can extend the File::$_context property to add the new footer context. 我了解可以使用Renderer::handlers()注册新的处理程序,但是我不确定如何扩展File :: $ _ context属性以添加新的footer上下文。

So far, I assume I'd add the snippet below to an Html helper. 到目前为止,我假设我会将以下代码段添加到Html帮助程序中。

protected function _init() {
    parent::_init();

    if ($this->_context) {
        $ctx =& $this->_context;

        $this->_context->handlers(array(
            'footer' => function($footers) use (&$ctx) {
                    return "\n\t" . join("\n\t", $ctx['footer']) . "\n";
                }
        ));
    }
}

Perhaps I'm overthinking it, and should just extend my Html helper to replicate the functionality. 也许我想得太多了,应该扩展我的Html帮助程序来复制功能。

I haven't tested this, but with the way __call() works, you should be able to say $this->_context += ['footer' => []] inside _init() then call $this->footer("Foo!") to append. 我还没有测试过,但是使用__call()的方式,您应该可以在_init()说出$this->footer("Foo!") $this->_context += ['footer' => []]然后调用$this->footer("Foo!")附加$this->footer("Foo!")

When you're ready to render, $this->context("footer") should give you back an array of values. 准备渲染时, $this->context("footer")应该会给您返回一组值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM