Parijat Kalia
Parijat Kalia

Reputation: 5095

Dynamic data in header in a symfony project

I have a Symfony 1.4 project. As you know the Template layout is defined independently, in the apps' templates' folder and then it is universally applied to all other templates. My layout is very simple, something like this:

<div id = "header">
</div>
<div id = "content">
<?php echo $sf_content ; ?>
</div>
<div id = "footer">
</div>

$sf_content, as most symfonians would know, essentially spits out the template for whatever web page is being viewed at the moment. If I needed some specific data for my header, such as logout, logo etc, I would simply include it within my header. THis works great because it is static in nature. The challenge I am facing is how I can include data that is dynamic in nature and specific to a page within the header tag because the UI demands that I include it there.

For instance, one of my webpages requires user specific data to be loaded in a dropdown/select menu. This is dynamic and could range from 0 to 100 and is specific to each user. To create this dropdown menu is not an issue, and I already have that part done. The challenge is, how do I load it in the header, given that my data becomes part of $sf_content and that is spit out in my content div.

Is there a way for me to move a specific part of my $sf_content into the header div ?

Upvotes: 0

Views: 774

Answers (2)

Simon Cast
Simon Cast

Reputation: 255

Slots work for this. They can either be set in the action as in the first answer above or you can define them in the templates themselves. This is what I've done where I have dynamic data to define for the layout.

In your example:

<div id="header">
  <?php include_slot('some slot name')?>
</div>
<div id="content">
<?php echo $sf_content() ?>
</div>
<div id="footer">
</div>

In the templates you would define the following:

<?php slot('some slot name')?>
  //your code goes here
<?php end_slot() ?>

When the layout is then rendered Symfony will place the code between the slot() and end_slot() into the point at which you defined by using include_slot().

For ease I created a global partial that is included in all templates that defines the various common slots used through out the application. There is more info on slots and their usage here

Upvotes: 0

Wojciech Zylinski
Wojciech Zylinski

Reputation: 2035

In your actions.php:

$this->getResponse()->setSlot('someData', 'and its value');

In layout.php:

<div id="header">
<?php echo get_slot('someData'); ?>
</div>
<div id="content">
<?php echo $sf_content ; ?>
</div>
<div id="footer">
</div>

Upvotes: 1

Related Questions