Search code examples
phplayoutheadersymfony1symfony-1.4

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 ?


Solution

  • 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>