The metadata plugin simplifies the setting of HTML page meta data. Web site meta tags are often the same for every page, but a few could vary between requests. The purpose of the metadata plugin is to eliminate view or layout logic for setting meta tags data. It is also meant to encapsulate the static meta tag content from controller logic, while still allowing for tags to be overwritten conditionally in the controller.
There is a simple way to use this plugin without much maintenance for the duration of the project. After including the plugin, simply include the component.
public $components = array(‘Metadata.Metadata’);
The meta tags data can be completely encapsulated from the controller logic by including a metadata.php file in app/config/. This file will automatically get included and meta tags can be created from the data contained. In the metadata file, site global, controller global and action specific properties can be set (in the case of the PagesController, the page file can be set instead of actions).
// app/config/metadata.php
$metadata = array( ‘_all’ => array( ‘description’ => ‘Site wide description’ ), ‘pages’ => array( ‘_all’ => array( ‘description’ => ‘All pages_controller pages description’ ), ‘home’ => array( ‘description’ => ‘Home page description’ ) ), ‘controller_name’ => array( ‘_all’ => array( ‘description’ => ‘All controller_name actions description’ ), ‘action’ => array( ‘description’ => ‘controller_name/action description’ ) ) );
By having all of the meta data in the a config, the data can be semi dynamic for different pages without having to build controller setting logic or have any additional logic in the view. In some cases, though, it will be necessary to set a meta tag data from within the controller. The Metadata component exposes a method for setting meta tag data manually. Any data set this way will overwrite data that for the same key that might already exist.
$this->Metadata->metadata(‘description’, ‘Manually set description’);
or
$this->Metadata->metadata(‘description’, array(
‘content’ => ‘Manually set description’,
‘http-equiv’ => ‘content-type’
));
Note: Metadata can be set as an array like the second example above, even in the metadata.php, as long as a content key exists in the array. All additional keys will be passed as attributes in the helper.
So far, so good. We can now get rid of all the logic in the controllers regarding metadata. We can also get rid of any sets in the view that set data for the layout to render. All the metadata information is set in the metadata file, or overwritten in special cases in the controller. But how is all of this metadata rendered? Short answer:
<?php echo $this->MetaData->meta(); ?>
The Metadata plugin comes with a Helper also named Metadata. The helper is automatically added to the controllers $helpers property. While adding the helper, the component also passes along all of the metadata keys that have been added up to that point (this happens in the Metadata component beforeRender()).
By not specifying any parameters, the helper’s metadata() method will loop through all data passed in from the controller, and render the data using Html->meta(). The metadata method can also take parameters and pass them along to the Html->meta() method if you’d like to stick to a single method for handling meta tags data.
Another method exposed by the metadata helper is the title() method. If at any point, metadata is set with the key “title”, that data is set aside in the helper. To render the title, use the title() method. The first two parameters are a default title, and a boolean merge option. If a default title is passed and merge is set to true, the return will be the combination of the title set in metadata and the passed in default title (an example would be a site wide title and a post, newsletter, etc title contained in the data layer).
The Metadata plugin is meant to be an alternative to having to manage metadata database tables. In many sites, a full model/behavior solution may be too much. This plugin was the solution that best fit one of our projects, and we hope it comes in handy for some of you.
Then code can be downloaded or cloned from here: https://github.com/joeytrapp/metadata
hey guys, great work. one note though, you might want to consider a different name for the plugin to prevent confusion:
https://github.com/jmcneese/metadata