簡體   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