tags:

views:

721

answers:

4

The global layout.php file contains the tags for each page:

<body> 
    <?php echo $sf_content ?>
</body>

But for all my inner HTML pages of the site, a class is applied to the body tag:

 <body class="inner-page"> 
    <?php echo $sf_content ?>
 </body>

How can I pass in a class to the layout from different templates?

A: 

In most MVC frameworks, you access variables in the layout the same way you access them in the view files.

So, if you did something like $this->view->my_data=10; in the control. You can access it in the layout by: echo $this->my_data.

This was pseudo code, adjust it to the symphony way.

Itay Moav
+1  A: 

in your layout.php

<body <?php if (!include_slot('body_id')): ?>id="default"<?php endif; ?>>

in your templates :

<?php slot('body_id') ?>id="bla"<?php end_slot(); ?>

or

<?php slot(
  'body_id',
  sprintf('id="%s"', $sf_params->get('module')))
?>
Pascal
+1  A: 

Here is the solution I used with Symfony 1.2+

Use setSlot() in the action:

$this->getResponse()->setSlot('home_page', 'yes');

Then use get_slot() in the Layout:

<body class="<?php echo has_slot('home_page') ? 'home-page' : 'inner-page' ?>">
Failpunk
A: 

I'm 90% sure that you do this the same way you'd do it from a regular view file. So as long as you set the variable in the action, it should be accessible from the layout.php file.

In your action:

$this->body_class = "xxx";

In layout.php

<body class="<?php echo $body_class?>">

Be sure to run some checks to make sure $body_class is set.

sjobe
This is what I managed to find about this: http://www.symfonydeveloper.com/2009/03/05/passing-variables-from-an-action-or-module-template-to-a-layout-template/
Failpunk
I tried this in Symfony 1.2.7 and it did not work.
Failpunk
Does not work in 1.3.6 either ...
morpheous