简体   繁体   中英

Phalcon Template “Undefined variable” (variable is set)

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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