Since Symfony 1.1, a bit of change has gone on with passing variables from an action to a layout file. You used to be able to do it like this:
$this->foo = 'bar';
And then you could call it in either your action’s template file or the application’s layout file like this:
<?php echo $bar ?>
But that’s no longer the case… for good reason, but I won’t get into that here. Now you’re forced to use slots. So If you want to use the same logic above, you must pass the variable via a slot in your action like this:
$this->getResponse()->setSlot('foo', 'bar');
Then you can call that variable value from within your layout template like this:
<?php echo get_slot('foo') ?>
You can also make the assignment from within your action’s template file like this:
<?php slot('foo', 'bar') ?>
And you’ll still call the variable the same way in your layout template.
April 3rd, 2009 at 7:35 am
Perfect tip and perfect timing!
Thanks
Would be nice to know the “good reason” too
April 3rd, 2009 at 2:05 pm
Does that work going the other way? Can I set in my templete and get in my Action?
May 5th, 2009 at 3:35 am
$this->foo = ‘bar’;
still works in symfony 1.2, don’t know what you mean.
May 5th, 2009 at 8:10 am
@Ark – It does work when you are assigning the variable from an action to that action’s corresponding template. However, it does not work when you are trying to make an assignment from an action to a layout template.
June 28th, 2009 at 11:22 am
Thanks man, this stuff really works! =)
July 17th, 2009 at 8:09 pm
thanks man!
this safe a lot of time..!
August 1st, 2009 at 4:40 pm
omg, why is this not in the book !?!?!
$this->getResponse()->setSlot(‘foo’, ‘bar’);
searched for loads of time.
checked now, and it is not even nowhere in Jobeet tutorial. This is bad, i think it is as intuitive to set slot contents (actually trans-view variables
in controllers.
Dammit, lost hours for this.
March 21st, 2010 at 7:24 pm
Joining to container. Lost hours for this. It should be in the Jobeet tutorial.
July 28th, 2010 at 4:56 am
This is not necessary to use slots, you can always do $this->getRequest()->setAttribute() in an action and then do $sf_request->getAttribute() in layout template.