Smarty scope examples

  Previous article Next article  

Smarty scope examples

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.2
  Last updated: 04-07-2017
  Comments: 0

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}

A2 Webhosting CMS Made Simple

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]]


A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

Your IP address will be stored on submit. I reserve the right to change or delete your response without notice!
In case of abuse your IP address will be blocked!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  0 Comments

No comments yet...

CMS Made Simple 2.2.2 - Tutorials, tips and tricks - CMSMS 2.2.2

Smarty scope examples

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.2
  Last updated: 04-07-2017
  Comments: 0

A2 Webhosting CMS Made Simple