when i pass variables from a controller they are only passed to a template, not a layout surrounding that template.
how do i pass variables to a template?
thanks
If you are using Twig in another project, you can set your globals directly in the environment: $twig = new Twig_Environment($loader); $twig->addGlobal('myStuff', $someVariable); And then use {{ myStuff }} anywhere in your application.
From the official Twig documentation: "Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself."
Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher.
Use slots.
In your action method:
$this->getResponse()->setSlot("foo", "12345");
In your layout template:
<?php echo get_slot("foo", "default value if slot doesn't exist"); ?>
which will output the slot contents. In this example, you'll see 12345
appear in your layout. If you don't set a slot's value in the action, you can supply a default value to be shown instead in the layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With