views:

548

answers:

2

Hi,

I'm wondering if anyone's got any good advice/experience regarding setting dynamic meta titles in Symfony?

Currently, the solution I'm aware of would be to use the following code to set a title individidually in each action:

$this->getResponse()->setTitle('This is a title');

Because I also need translated titles, I could call the i18n helper in the action to get them included in the extracted XLIFFs. No special SEO stuff needed, just a clean title.

However, the above does require that I tweak every single action separately. View.yml is not suitable as I frequently have multiple actions/templates per module.

Is anyone aware of a better approach in Symfony or is this indeed the right/only way to go?

Thank you.

+2  A: 

I think writing separate titles in every action is OK. But if you want to add some global prefix you can use something like this in layout:

<title>SITE NAME — <?= $sf_response->getTitle() ?></title>

Also you can probably manipulate a title per module using preExecute() method in actions.

zergu
Using the preExecute method is not a nice way of doing it since it will tie the title logic to the controller. It should really be in the view because it is part of the presentation layer, not the controller.
phidah
+3  A: 

You should use slots.

In your layout <head> tag:

<title><?php echo get_slot('page_title', __('Default page title here')) ?></title>

And in an action template:

<?php slot('page_title', __('Action page title goes here')) ?>
NiKo