views:

32

answers:

1

I am trying to render templates in Symfony with a CSS class to denote which part of the site they belong to. For example: I am in /games I'd like the page to look something like:

<div id="wrapper" class="games">
<!-- content -->
</div>

Or if we are displaying /home/profile the page would look like this:

<div id="wrapper" class="home">
<!-- content -->
</div>

Basically I am looking for a similar functionality to CodeIgniter's url segment methods.

+2  A: 

Is the class simply the name of the module? If it is, do this:

<div class="<?php echo $sf_context->getModuleName() ?>">

You could also set it as a parameter on the request by defining it in your routes:

page:
  url: /page
  param: { module: default, action: page, section: games }
  ...

Then get it off the request in your template:

<div class="<?php echo $sf_request['section'] ?>">

Finally, if it's the same for each module but not equivalent to the module name, you could set it in preExecute:

public function preExecute()
{
  $this->getRequest()->setParameter('section', 'workouts');
}
jeremy
The module name solution is pretty much perfect. It would be even more perfect if I could put it in the layout instead of template but I don't think '$sf_context' is available to the layout. I just don't want to have to repeat this everywhere when it really seems better to put it just once in the layout.
Simon
$sf_context is available in layouts. Also, it should be "getModuleName" not "getModule"
jeremy
Ah! My mistake, all works perfectly now - thanks for the insight.
Simon