Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you pass a parameter from an action to the layout in Symfony

Tags:

php

symfony1

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?

like image 301
Justin Avatar asked Feb 28 '23 18:02

Justin


2 Answers

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')))
?> 
like image 187
Pascal Avatar answered Apr 08 '23 10:04

Pascal


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' ?>">
like image 45
Justin Avatar answered Apr 08 '23 09:04

Justin