[英]Phalcon Template “Undefined variable” (variable is set)
I want to show a template file (ajax include) with variable from controller. 我想显示一个带有控制器变量的模板文件(包括ajax)。 I want to create a simple shoutbox.
我想创建一个简单的喊话器。
development environment: 开发环境:
PHP Version 5.6.19 (xampp) PHP版本5.6.19(xampp)
Phalcon 2.1.0r (php c-ext) Phalcon 2.1.0r(php c-ext)
Windows 10 Windows 10
IDE Netbeans 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): 这是我的控制器功能(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): 这是我的视图文件(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 %} {%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) 注意:未定义的变量:喊叫声(在app / views / shoutbox / getshouts.twig中)
When i use only the controller/action ( http://myurl/shoutbox/getshouts ) it works. 当我仅使用控制器/操作( http:// myurl / shoutbox / getshouts )时,它可以工作。 I have access to the variable "shouts".
我可以访问变量“ shouts”。
I don't understand why this works when i use http://myurl/shoutbox/getshouts but in the layout there is no var "shouts". 我不明白为什么在使用http:// myurl / shoutbox / getshouts时此方法有效,但是在布局中没有var“ shouts”。
If u need more information tell me please. 如果您需要更多信息,请告诉我。
I hope someone can tell me whats wrong. 我希望有人能告诉我怎么了。
您可能应该像这样使用with
传递变量:
{% include "shoutbox/getshouts.twig" with ['shouts': shouts] %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.