I can't get this to work, I need to retrieve an CSRF token via an object inside a Session object but it isn't displaying no matter what I try.
I have developed a system that will generate a template from properties from an object, it looks like this:
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);
}
Now I thought I could access the properties of the session by doing:
{{ session.CSRF.getTokenValue() }}
or
{{ session['CSRF'].getTokenValue() }}
But nothing is displaying.
When I used <?php echo $session->CSRF->getTokenValue(); ?>
It worked, so its not the object/method that is failing,
What am I doing wrong here?
EDIT:
It could also be noteworthy that accesing properties like user (user.title, user.name) is possible