Getting the Template Name

I sometimes need more control over the styling than conventional ID’s and classes can provide; which will also protect against user input and be dynamic.

I like to use a combination of the root alias, parent alias (if not the same as root) and the template name. The reason for this is so I can have full control over what gets styled and how even if the user doesn’t include that crucial ID to the specific p tag.

This tutorial will just touch upon this by showing how you can get the template name, format it and add it onto the page.

To follow this tutorial you will need:

  1. A basic understanding of UDT's
  2. A basic understanding of Smarty

 

Getting the Template's Name

So first things first, let's create the UDT:

$page_id = !empty($params['page_id']) ? $params['page_id'] : null;
 $assign = !empty($params['assign']) ? $params['assign'] : null;
 
 if (!empty($page_id)) {
  $contentOps = cmsms()->getContentOperations();
  $templateOps = cmsms()->getTemplateOperations();
 if (!empty($contentOps) && !empty($templateOps)) {
   $content = $contentOps->LoadContentFromId($page_id);
      if (!empty($content)) {
    $templateID = $content->templateId();
    if (!empty($templateID)) {
     $template = $templateOps->LoadTemplateByID($templateID);
     if (!empty($template)) {
      if (!empty($assign)) $smarty->assign($assign, $template->name);
      else return $template->name;
     }
    } 
   }
  }
 }

 return false;

The UDT accepts two values, the content ID and the variable name to save the information too. The UDT attempts to get the template ID from the full content object. It then loads the template using this ID to retrieve the name and if something goes wrong the UDT will just return false.

For the sake of this tutorial I have saved this UDT as "get_template_name".

 

Adding it to Your Template

I prefer to have separate header and footer GCB's. This is so then I don't have to change every template to alter them and it also slims down the size of each template. So let's take a look at how I am deploying this UDT in my header GCB:

{get_template_name page_id=$content_id assign="templateName"}
<body{if !empty($templateName)} class="template-{$templateName|replace:' ':'-'|lower}"{/if}>

As you can see I am calling the UDT using the following tag:

{get_template_name page_id=$content_id assign="templateName"}

If you use {get_template_vars} you can see where I got $content_id from and the assign parameter is declaring the name of the variable I want the returned information stored in.

 

Formatting the result

Since I am adding this to my body, I test to see if there was anything returned (I hate seeing empty tag parameters!). If something is returned I display it with a "template-" prefix, this is just in case I need to add a class with the same name as the template.

Finally the template name itself is formatted by replacing all spaces with dashes and making it lowercase. Alternatively you could use the following code to camel case:

{$templateName|capitalize|replace:" ":""}

And that's it! A nice and simple step to making styling of specific templates that much easier and not a module in sight.

Comments