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.

1 Answer 1

3

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

3 Comments

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.
$sf_context is available in layouts. Also, it should be "getModuleName" not "getModule"
Ah! My mistake, all works perfectly now - thanks for the insight.

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.