We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

New way of dealing with view or is it issue?

Hi everyone,

I switched to Phalcon and I love it. Now, I was going through tutorials (btw, very good documented) and got confused a little bit. So, in the tutorial about Views it says that you can set View variables in the controller ( $this->view->setVar("myvar", $myvar);) and access them from the View with $this->myvar.

I installed Phalcon 1.3 on my local server running on WAMP (PHP 5.4.3). The problems that I have are:

  1. I can't set View variables this way ($this->view->setVar("myvar", $myvar);) it gives me an error. I still can set variable this way ($this->view->myvar=$myvar), which is good.

  2. I can't access the variable from the View this way ($this->myvar). But I can access it this way $this->view->myvar.

I'm not sure if this is ment to be this way or not.

Next week I'm planning to start coding on a new project and just would like to know, whether I should wait or go ahead with the project (hopefully don't need to change code if there are changes in the way it accesses variables).

Really cool and easy easy to learn MVC framework.

Thanks for clarification in advance.


@cjrb2009 Not sure about the tutorials, I will look closer at those in a bit, but to help with your questions - check out Transfer values from the controller to views section of the Views docs. Here, it shows a few different examples of how to perform what your inquiring about.

So, in your first example, you would do the following:


class IndexController extends ControllerBase

    public function indexAction()
        $myVar = "String of Text";
        $this->view->setVar('myVar', $myVar);


index.volt //partial view file

{% extends "layouts/main.volt" %}

{% block title %}<title> Unisys12 - Blog Home </title>{% endblock %}

{% block content %}
<main class="posts">
    {{ myVar }} //Echoing out our myVar variable we declared in the controller
{% endblock %}

To answer your second issue: Notice that when I need to echo out the variable declarded in the controller, I am using the double curly braces and not placing a '$' in front of the var. That dollar sign will cause you errors.

Hope this helps. If you have any other issues or questions, just give a yell.

edited Apr '14

@Phillip Thanks for your reply.

You are absolutelly right. I think I drank to much energy drinks and spent too much time in front of computer.

My fault. I was trying to use $this->view->setVars("myvar","MyVar"); and failed to notice that I need to use setVar instead of setVars.

Also, for some reason I thought to recall variables I need to use $this->myvar .... When I can just use $myvar (which is much nicer and better)

Thanks for clarification:) Love this framework!!!!