Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i pass variables to a layout in symfony?

Tags:

symfony1

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

like image 827
never_had_a_name Avatar asked Jun 01 '10 16:06

never_had_a_name


People also ask

How do I add a global variable in Twig?

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.

What is macro Twig?

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."

What is Twig in Symfony?

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.


1 Answers

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.

like image 103
richsage Avatar answered Oct 17 '22 09:10

richsage