Showing posts with label title. Show all posts
Showing posts with label title. Show all posts

Wednesday, January 25, 2012

[ZF] Translating a page's title

So it's been a while, eh? Happy New Year (and Chinese New Year, while we're at it)!

For a project, we're finally in testing phase and all of a sudden, I point something out that looks wrong: the translator works perfectly... except for the page titles. By that, I mean the following tag:
<title>Title</title>
 
What gives? Well, my understanding is that the translator is not yet set in the Bootstrap when the
$this->view->headTitle($label);
is called.

The first thing I tried? Adding the translate() method to it:1
this->view->headTitle($this->view->translate($label));

... that didn't work at all. Simply no change. Great.

Then I put my thinking cap on (it's still early in the morning), and thougt this through. First of all, here is how the main parts of my Bootstrap file are laid out:
- Register plugins
- Register routes
- Prepare navigation (menus, prepare breadcrumbs, and set page title)
- Prepare translation
- Configure e-mail settings for sending stuff
- Prepare the view

Now this little brain of mine decided to kick in this morning, and made me realize: what if we prepared the translation before setting some text on the output page?

So there I am, moving the _initTranslate() method up a little, right over the _initNavigation(). Just leave the line of code that includes the translate() method ([1] above) when setting the page's title, and as long as $label is defined in your language file(s) and the translator is set correctly, shazam, it'll work like a beauty!

Final trick: you can prepend (or append, I guess) anything to your head title! In your _initView() method, be sure to do the following:
$view = new Zend_View();
$view->doctype('XHTML1_STRICT'); // Or whatever
$view->headTitle('Client - ');

// Add more settings here if needed

$view_renderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$view_renderer->setView($view);

return $view;
 
By doing this, all your pages' titles will look like "Client - Translated page title". Awesome, isn't it?

Friday, August 5, 2011

[ZF] Adding section titles in a Zend_Form

Do you have a Zend_Form with many elements and need to add section titles for it to be more user-friendly?

Nothing is as simple as this!

// Add section title
$personal_section = new Zend_Form_Element_Hidden('personal_section');
$personal_section->setLabel('Personal information');
$personal_section->setDecorators(
    array(
        'ViewHelper',
        'Description',
        'Errors',
        array('Label', array('tag' => 'h2'))
    )
);
$this->addElement($personal_section);


This basically creates a hidden input with a label, and we're just modifying this to use an H2 tag instead of the regular one. Not very nice technically, but very effective!