MENU

Smarty scope examples

  Previous article Next article  

CMS Made Simple 1.12 introduces the concept of 'scopes' in Smarty templates. This version of Smarty does not treat each and every variable as a global. This will solve problems with Smarty variables being inadvertently overwritten, but will cause problems with websites that rely on some global variables.

About scope

Formerly, you could create a variable in a module template (for example the News detail template could set a canonical variable) and then use that variable in another template, such as in the portion of your page template. Though this is still possible, an additional step is required.

In a CMSMS Core::Page template there are 3 different scope areas

<!-- Area 1 -->

<head> <!-- Area 2 --> </head>

<!-- Area 3 -->

They are rendered in the order: 1, 3 and 2!
You need to add scope=global to your assigned parameter when you want to re-use the value in another scope...


CMS Made Simple 2.2 introduces a fourth scope!! All "mact" module action will be processed first. Therefor you need to process your content blocks containing the module tags (in the template head) first.
You can see here a base CMS Made Simple page template with automated metatags »

You can change this behavior with the "startup_mact_processing" config parameter!
More info at the CMSMS docs website

  How to use

Templates

Current

{content assign=content}

New

{content assign=content}
{$content=$content scope=global}

Or a bit shorter:

{$content="{content}" scope=global}


Current

{content block=second assign=content2}

New

{$content2="{content block=second}" scope=global}


Current

{description assign=description}
{title assign=page_title}

New

{$description="{description}" scope=global}
{$page_title="{title}" scope=global}


Current

{capture assign=foo}
  ...
  ...
{/capture}

New

{capture assign=foo}
  ...
  ...
{/capture}
{$foo=$foo scope=global}


Current

{assign var=canonical value=$entry->canonical}

New

{assign var=canonical value=$entry->canonical scope=global}

Or a bit shorter:

{$canonical=$entry->canonical scope=global}


Current

{$canonical = $entry->canonical}
{$page_title = $entry->title}

New

{$canonical = $entry->canonical}
{$page_title = $entry->title}

{share_data data='page_title,canonical' scope=global}

Stylesheets

Current

[[capture assign=theme_url]][[uploads_url]]/template[[/capture]]

New

[[$theme_url="[[uploads_url]]/template" scope=global]]


Current

[[$my_red='#EF4A46']]

New

[[$my_red='#EF4A46' scope=global]]


Buy Me A Coffee


  Comment Form

ReviewManager

Click here to open the form

ReviewManager

  3 Comments

Buy Me A Coffee

CMS Made Simple - Tutorials, Tips and Tricks - CMSMS

Smarty scope examples

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.19
  Last updated: 23-06-2018
  Comments: 3
  http://cms.ms/Hupy


Buy Me A Coffee




Advertisement


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!