Posted on | July 17, 2012 | 2 Comments
Meta keywords and description used to be used by search engines to help work out what your website is about. Nowadays they are used to web applications to work out how to describe a specific page (for example, paste a link into Google+ and the meta description of the page will be fetched and shown). If you are writing a CMS that allows the user to populate the meta keywords and description of a page, but the user does not fill these fields, then it is nice to have a default value.
This post will share a custom View Helper that will check for your application adding empty meta details, and if there are, populating the values with default values from the application config.ini file.
First, we need to do add a folder structure for our view helpers. Create the following tree structure:
Now, we need to tell our application where to find our view helper. In the application bootstrap.php file add the following function:
$view = $this->getResource(‘view’);
This allows the application to access our view helper.
Here is the code for the view helper. We extend the already existing Zend view helper ‘HeadMeta’. Feel free to download this file
type == ‘name’ AND ($value->content == null OR strlen($value->content) === 0) AND ($value->name == ‘keywords’ OR $value->name == ‘description’))
$config = Zend_Controller_Front::getInstance()->getParam(‘bootstrap’)->getOptions();
$setting = $config[‘default’][‘meta’][‘keywords’];
$setting = $config[‘default’][‘meta’][‘description’];
//set the value
$value->content = $setting;
$e = new Zend_View_Exception(‘Invalid value passed to append; please use appendMeta()’);
page = page object passed to view script from controller
As you can see there is no special instantiation code needed.
Posted By:Richard Parnaby-King