We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How do I use a particular view?

How do I tell the controller which view to use?

IndexController

indexAction()

should use Index/index.volt

and in index.volt

it should

{% extends 'layout.volt' %}
{% block body %}
my content {{ some var }}
{% endblock %}
{% block css %}
<link rel="stylesheet/less" type="text/css" href="styles.less" />
{⅜ endblock %}

or for instance in BlogController createAction() it should use Blog/new.volt



98.9k

Mvc\View tries to locate a view with the same name as the lastest action executed inside a directory with the same name as lastest controller executed.

IndexController/indexAction -> views/index/index.volt
BlogController/createAction -> views/blog/create.volt

You can "pick" another view by using the pick method on the 'view' service:

$this->view->pick("blog/new");


3.8k

Thanks but when I create a skeleton app with the devtools bash phalcon project phal simple ./ It loads views/index.volt hmm weird So I moved views/index.volt to views/index/index.volt and it worked but when I create a new file views/index.volt while index/index.volt is present it picks views/index.volt instead of views/index/index.volt I don't know if it's supposed to be like this.

Anyhow thanks for the reply.



3.8k
Accepted
answer
edited Oct '14

I just tried it again. I deleted the cache files. Deleted views/index.volt according to your information it should use views/index/index.volt

the error I get is

Warning: file_get_contents(/home/darko/NetBeansProjects/phal/app/config/../../app/views/index.volt): failed to open stream: No such file or directory in /home/darko/NetBeansProjects/phal/public/index.php on line 27
Template file /home/darko/NetBeansProjects/phal/app/config/../../app/views/index.volt could not be opened

line 27:

echo $application->handle()->getContent();

default skeleton app

Well lol Most application errors happen between the PC and the keyboard I had

{% extends 'index.volt' %}

in index/index.volt D'oh moment