0

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 ?

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

awesome, so either a slot or a partial, I think partials are more for static text if I am not mistaken?
The difference between slots and partials is explained here: andreiabohner.files.wordpress.com/2007/08/…
I am a little confused, I got how we can render a slot here. But how do I construct the slot in itself? You know, it's composed of a dropdown . If it was a partial, I would define something like _partialName.php and then say include_partial. I could not figure out how to define the equivalent _slotName.php here :\
You do not use seperate files with slots. But you can set slot to contain an array. So for example, in your actions.class.php: $this->getResponse()->setSlot('slotName', array('1', '2', '3', 'five')); and then in your layout.php: <?php $slot = get_slot('slotName'); foreach ($slot as $number): ?><a href=""><?php echo $number; ?></a><?php endforeach; ?>
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.