无法让Twig与Object中的Method一起使用

时间:2013-12-16 03:10:29

标签: php templates twig

我无法让它工作,我需要通过Session对象内的对象检索CSRF令牌,但无论我尝试什么,它都不会显示。

我开发了一个系统,它将根据对象的属性生成模板,如下所示:

public function render(){
    if(method_exists($this->controller, $this->action)){
        //We need to check if the method is used in the controller and not in the parents AppController and/or Controller
        $f = new ReflectionClass($this->controller);
        $methods = array();
        foreach($f->getMethods() as $m){
            if($m->name == $this->action){
                if($m->class == $this->controller){
                    $this->controller->{$this->action}();
                }
            }
        }
        $this->view_body = $this->render_view();
    }else{
        $this->error->show('Method ' . $this->action . ' not found in  ' . $this->controller, $this->action, $this->controller);
    }
    $this->_set_render_view();
    $this->_set_controller_layout();
    if(!file_exists($this->config->paths->root . $this->layouts_path."/".$this->controller->layout)){
        $this->error->show('Layout not found', $this->action, $this->controller);
    }
    if(!file_exists($this->config->paths->root . $this->views_path.'/'. $this->view_root . "/" . $this->controller->view)){
        $this->error->show('View not found', $this->action, $this->controller);
    }
    $layout_content = file_get_contents($this->config->paths->root . $this->layouts_path."/".$this->controller->layout);
    $view_content = file_get_contents($this->config->paths->root . $this->views_path.'/'. $this->view_root . "/" . $this->controller->view);
    $bind_content = "{% extends \"layout.tmpl\" %} ";
    $templates = array();
    $bind_content .= '{% block view %}' . $view_content . " {% endblock %}";
    $templates['layout.tmpl'] = $layout_content;

    foreach($this->controller->snippets as $snippet_name => $snippet){
        if(!file_exists($this->config->paths->root.$this->snippets_path ."/" . $snippet)){
            $this->error->show('Snippet not found', $this->action, $this->controller);
        }
        $snippet_content = file_get_contents($this->config->paths->root.$this->snippets_path ."/" . $snippet);
        $bind_content .= "{% block ". $snippet_name . " %}" . $snippet_content . " {% endblock %}";
    }   
    $templates[$this->controller->view] = $bind_content;
    $loader = new Twig_Loader_Array($templates);
    $processwire_vars = array('user', 'pages', 'page', 'sanitizer', 'files', 'input', 'permissions', 'roles', 'templates', 'session', 'config', 'controller', 'wire');

    foreach($this->controller->vars as $key=>$val){
        $vars[$key] = $val;
    }

    $twig = new Twig_Environment($loader);
    $twig->addGlobal('this', $this);
    foreach($processwire_vars as $key => $pw_v){
        if(!isset($vars[$pw_v])){
            //$vars[$pw_v] = $this->$pw_v;
            $twig->addGlobal($pw_v, $pw_v);
        }
    }
    echo $twig->render($this->controller->view, $vars);
}

现在我认为我可以通过以下方式访问会话的属性:

{{ session.CSRF.getTokenValue() }}

{{ session['CSRF'].getTokenValue() }}

但没有任何东西在显示。

当我使用<?php echo $session->CSRF->getTokenValue(); ?>它有效时,所以它不是失败的对象/方法,

我在这里做错了什么?

编辑:

值得注意的是,访问user(user.title,user.name)等属性是可能的

0 个答案:

没有答案
相关问题