Use {module_action_link} to create canonical URLS
Posted: Thu Jun 27, 2013 5:25 am
Some modules create smarty variables for canonical urls from thier various detail views. News is one of them. Some however do not. (including I am sorry to say, some of mine).
We all know that the <link rel="canonical" href="some url"/> tag is the way to tell the search engines the one true URL for a page. So, making sure its filled in is important.
CMSMS's built in templates have a tag like this in their head section:
This logic says if the smarty variable 'canonical' exists, then use it to build the canonical link. otherwise, use the current content object. Also, remember CMSMS processes the body portion of the page template through smarty before the head portion of the page template is parsed through smarty. So therefore, we can set variables in any template that is processed in the body, and those variables will be available in the head portion of the page template.
News has an {$entry->canonical} variable in its detail view. And the default detail template has a line like: {assign var='canonical' value=$entry->canonical} to ensure that our 'canonical' value is set properly for use when the page template's head section is processed.
If a variable like this isn't available, you can easily create one, just by re-creating the URL. Sometimes you can use the {get_current_url} plugin provided by the CGExtensions module. However, if there is alot of ajax going on this may work the way you want it to. That's where the {module_action_link} plugin comes in. It is provided in the CGSimpleSmarty module.
Adding a line like:
{module_action_link module=CompanyDirectory action=details compid=$entry->id urlonly=1 assign=canonical} into your detail template will generate the pretty url necessary for the canonical view.
I encourage you to look at the help of the CGSimpleSmarty module to see all of the parameters for this, and the other smarty plugins.
Hint: some js for ajax:
var url = '{module_action_link module=CompanyDirectory action=details compid=5 urlonly=1 jsfriendly=1}&showtemplate=false';
$('#somediv').load(url);
We all know that the <link rel="canonical" href="some url"/> tag is the way to tell the search engines the one true URL for a page. So, making sure its filled in is important.
CMSMS's built in templates have a tag like this in their head section:
Code: Select all
{if isset($canonical)}<link rel="canonical" href="{$canonical}" />{elseif isset($content_obj)}<link rel="canonical" href="{$content_obj->GetURL()}" />{/if}
News has an {$entry->canonical} variable in its detail view. And the default detail template has a line like: {assign var='canonical' value=$entry->canonical} to ensure that our 'canonical' value is set properly for use when the page template's head section is processed.
If a variable like this isn't available, you can easily create one, just by re-creating the URL. Sometimes you can use the {get_current_url} plugin provided by the CGExtensions module. However, if there is alot of ajax going on this may work the way you want it to. That's where the {module_action_link} plugin comes in. It is provided in the CGSimpleSmarty module.
Adding a line like:
{module_action_link module=CompanyDirectory action=details compid=$entry->id urlonly=1 assign=canonical} into your detail template will generate the pretty url necessary for the canonical view.
I encourage you to look at the help of the CGSimpleSmarty module to see all of the parameters for this, and the other smarty plugins.
Hint: some js for ajax:
var url = '{module_action_link module=CompanyDirectory action=details compid=5 urlonly=1 jsfriendly=1}&showtemplate=false';
$('#somediv').load(url);