简体   繁体   中英

Undefined variable in actionblock template

This is my first SymfonyCMF application, so please bear with me, I might be fundamentally misunderstanding some concepts.

I have an ActionBlock ( /cms/content/most-recent-psa ) with an action of FooCmsBundle:Psa:embedPsa .

My embed function looks like this:

class PsaController extends Controller {

    /**
     * @Template()
     */
    public function embedPsaAction() {
        //... figure out which PSA needs to be displayed and set it to $psa

        return array('page' => $psa);
    }
}

I embed it in a Twig template with this: {{ sonata_block_render({"name": "/cms/content/most-recent-psa"}) }}

When I embed that template on a page, I get this exception: Variable "psa" does not exist in /var/www/peacefund-cms/src/Foo/CmsBundle/Resources/views/Psa/embedPsa.html.twig at line 6 , which is just a simple output: <h2>{{ psa.title | raw }}</h2> <h2>{{ psa.title | raw }}</h2> .

So it looks like it's doing everything correctly. I've made sure that $psa is a valid value when it gets returned from the embedPsaAction function, and it seems like it loads the template exactly as it should, but the variable isn't being exposed.

If you want to use a variable named "psa", you will have to give this exact name in the controller. Here you have the "page" variable for you twig page. And the page variable for twig equals to $psa for the controller.

In your controller, you have to do that :

array("variableNameForTwig1" => $variableController1, "variableNameForTwig2" => $variableController2 )

And then in your view, you can do that :

{{variableNameFortwig1.attribute}}
{{variableNameFortwig2.attribute}}

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