繁体   English   中英

Phalcon模板“未定义的变量”(已设置变量)

[英]Phalcon Template “Undefined variable” (variable is set)

我想显示一个带有控制器变量的模板文件(包括ajax)。 我想创建一个简单的喊话器。

开发环境:

PHP版本5.6.19(xampp)

Phalcon 2.1.0r(php c-ext)

Windows 10

IDE Netbeans

这是包括部分:(有效)

 $("#shoutbox_messages").load("{{ static_url("shoutbox/getshouts") }}"); 

这是我的控制器功能(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);

    }

这是我的视图文件(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%}

(树枝文件扩展名设置为伏特引擎文件扩展名。)

该视图是布局文件的一部分(包含在主布局中):

 {# Shoutbox #} <div id="flash_sb"></div> {% include "shoutbox/shoutform.twig" %} <hr/> <div id="shoutbox_messages"> {% include "shoutbox/getshouts.twig" %} </div> 

我不知道为什么在包含此文件时出现错误:

注意:未定义的变量:喊叫声(在app / views / shoutbox / getshouts.twig中)

当我仅使用控制器/操作( http:// myurl / shoutbox / getshouts )时,它可以工作。 我可以访问变量“ shouts”。

我不明白为什么在使用http:// myurl / shoutbox / getshouts时此方法有效,但是在布局中没有var“ shouts”。

如果您需要更多信息,请告诉我。

我希望有人能告诉我怎么了。

您可能应该像这样使用with传递变量:

{% include "shoutbox/getshouts.twig" with ['shouts': shouts] %}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM