Under the Hood - How CMSMS Processes Page Templates

Most people know that CMS Made Simple processes the BODY of a template before the HEAD, but did you know that there is a third section of the template that is processed before anything else?

CMS Made Simple processes page templates in four zones:

  • Zone 1: From the beginning of the file (the very top) to the end of the HTML tag.
  • Zone 2: From the BODY tag to End of File.
  • Zone 3: From HEAD to the /HEAD

There are a few things that you can do to make practical application of this.

Put Your Content Blocks First

I put my content blocks right after the {process_pagedata} tag at the beginning of the template.  For each content block I assign it to a variable.

Example:

{process_pagedata}
{content label='Main Content' assign='main_content'}
{content block='sidebar' label='Sidebar' assign='sidebar'}

Then in the page template, I insert the variable tag for the relevant content block where it belongs, like this: {$main_content}. The advantage of declaring your content blocks at the beginning of the template is that you can easily find them if you need to change a parameter, and you can also add this line right after the main content block:

{if !isset($page_title)}{title assign='page_title'}{/if}

This assigns the page title to the variable {$page_title}, but only if this has not already been done in the content block. This allows you to change the page title in a module such as News, Products, or CGBlog by assigning the entry name to the variable $page_title.

In the TITLE tag in the HEAD, as well as for your H1 page title in the BODY, use the {$page_title} variable in place of {title} to give the correct title based on the content.

Another possible reason for declaring your content blocks before the !DOCTYPE tag is that you may want to apply a class to your HTML or BODY tag based on the page content. This allows you to assign the class to a variable such as {$page_class} and then write your BODY tag like this:

<body class="{$page_class}">

Correct Placement of {strip} Tags

If you are a page speed fanatic, then you probably want to remove as much of the white space as possible from your fully rendered page.

Each Smarty tag, such as {assign} or {capture} will create a line break in your page code, even if it is assigning the output to a variable.

To clean this up, you can use {strip} {/strip} tags around your template. However, if you just put {strip} at the first line of your template, and {/strip} at the end of your template, it doesn't do anything. The reason is that each of the four zones on the page are evaluated separately.

So to get perfect whitespace stripping, you need to put {strip} tags at the beginning and end of each of the zones.  

Conclusion

Understanding the page zones and the order in which CMS Made Simple processes them makes it possible to apply content-specific classes to your HTML or BODY tags, and also enables "perfect" whitespace stripping with the correct placement of {strip} tags in each zone.

ALL-INKL.COM - Webhosting Server Hosting Domain Provider

Comments