7

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

never_had_a_name
  • 90,630
  • 105
  • 267
  • 383

1 Answers1

16

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.

richsage
  • 26,912
  • 8
  • 58
  • 65
  • Thanks. Ran across this today. Seems like variables set in the action would be available in the layout. In my case, the variables $module and $action are available in the layout. – yellottyellott Apr 23 '12 at 19:16
  • @yellottyellott I believe Symfony sets these itself, like it sets the `$sf_user` variable, although I may be wrong - it's been a while since I worked on a Symfony 1.x project. – richsage Apr 24 '12 at 07:37