Global layouts were introduced in Joomla 3 so that they are no longer limited to a particular view or extension.
In this example I am going to create a new view for the tags component which will output a plain comma separated list so that I can use it in a facebook opengraph tag.
To start with, let's look at how the tags layout is called in the articles view file.
<?php if ($params->get('show_tags', 1) && !empty($this->item->tags)) : ?>
<?php $this->item->tagLayout = new JLayoutFile('joomla.content.tags'); ?>
<?php echo $this->item->tagLayout->render($this->item->tags->itemTags); ?>
<?php endif; ?>
So if we ignore the check to see if tags are enabled we have 2 basic lines. The first is setting the layout file and the second is feeding the layout data to render.
The JLayoutFile('joomla.content.tags'); relates to the file /layouts/joomla/content/tags.php
We could just add another layout to this folder but I think it is best to keep all our overrides and layouts in the same place. So, as with standard template overrides, we can add them to the templates html folder in a directory named 'layouts'. I have mimicked the folder structure and created a new file here
In this file I copied the contents of tags.php and then reduced it to this:
* @package Joomla.CMS
* @subpackage Layout
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
defined('JPATH_BASE') or die;
JLoader::register('TagsHelperRoute', JPATH_BASE . '/components/com_tags/helpers/route.php');
<?php if (!empty($displayData)) : ?><?php foreach ($displayData as $i => $tag) : ?><?php if (in_array($tag->access, JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id')))) : ?><?php $tagParams = new JRegistry($tag->params); ?><?php echo $this->escape($tag->title); ?>,<?php endif; ?><?php endforeach; ?><?php endif; ?>
This basically loops through the tags and outputs them as a comma separated list.
To call the new layout from anywhere I just need to change the original code to reflect my new layout:
$this->item->rawtagLayout = new JLayoutFile('joomla.content.rawtags');
You can see a working example in my article on adding facebook opengraph tags to Joomla.