Reputation: 1
I want to show a template file (ajax include) with variable from controller. I want to create a simple shoutbox.
development environment:
PHP Version 5.6.19 (xampp)
Phalcon 2.1.0r (php c-ext)
Windows 10
IDE Netbeans
This is the include part: (it works)
$("#shoutbox_messages").load("{{ static_url("shoutbox/getshouts") }}");
This is my controller function (app/controllers/ShoutboxController.php):
public function getshoutsAction() {
$shouts = $this->di->getModelsManager()
->createBuilder()
->columns(array('Shouts.*', 'Users.name'))
->from('Shouts')
->join('Users')
->orderBy('Shouts.created_at DESC')
->getQuery()
->execute()
->toArray();
$this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT);
$this->view->setVar("shouts", $shouts);
}
This is my view file (app/views/shoutbox/getshouts.twig):
{% for shout in shouts %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ shout.name }}</h3>
</div>
<div class="panel-body">
{{ shout.shouts.text }}
</div>
</div>
{% endfor %}
(The twig file extension is set as the volt engine file extension.)
This view is part of a layout file (included from the main layout):
{# Shoutbox #}
<div id="flash_sb"></div>
{% include "shoutbox/shoutform.twig" %}
<hr/>
<div id="shoutbox_messages">
{% include "shoutbox/getshouts.twig" %}
</div>
I don't know why i get an error when include this file:
Notice: Undefined variable: shouts (in app/views/shoutbox/getshouts.twig)
When i use only the controller/action (http://myurl/shoutbox/getshouts) it works. I have access to the variable "shouts".
I don't understand why this works when i use http://myurl/shoutbox/getshouts but in the layout there is no var "shouts".
If u need more information tell me please.
I hope someone can tell me whats wrong.
Upvotes: 0
Views: 317
Reputation: 10717
You may should pass the vars with using with
like that:
{% include "shoutbox/getshouts.twig" with ['shouts': shouts] %}
Upvotes: 0