7 min read

Working with Smarty Variables

Smarty variables are much simpler than complex Smarty plugins. They are placeholders that contain plain information about the actual page ID, page alias, or position of the page in the hierarchy. Some Smarty variables that you are not aware of, are already defined in your template. You do not need to know or remember all of them if you know how you can figure out their names and values.

Time for action – getting Smarty variables

We are going to get the number of the page in the page hierarchy to integrate this information into the design of the page title. How do we figure out the name of the Smarty variable that contains this information? We can get it from the template as follows:

  1. In the admin console, click on Layout | Templates.
  2. Open the Business World template for edit and add the plugin {get_template_vars} just before the last tag, as shown in the following code snippet:



      {title} - {sitename}
      {stylesheet}
      {metadata}



      ...........
      {get_template_vars}



  • Click on Apply and then click on the magnifying glass icon on the top-right corner of the admin console to see the result. It should now look like the following screenshot:
  • CMS Made Simple 1.6: Beginner's Guide

    What just happened?

    With the Smarty {get_template_vars} plugin, you displayed all Smarty variables available in your template. In the list of variables on each line, one variable is displayed with its name and its value separated by an equals sign. These values change from page to page. For example, the variable with the name friendly_position contains the position of the page in the page hierarchy. If you navigate to other pages, you will see that the value of this variable is different on every page.

    How do you add a variable in your template? Smarty variables are enclosed in curly brackets as well, but unlike the Smarty plugins, they have a dollar sign at the beginning. To use the variable friendly_position, you just need to add the following Smarty tag to your template:

    {$friendly_position}

    You can delete the {get_template_vars} plugin now. It is helpful for you to see which Smarty variables exist and what values are stored there. You can add this plugin again, when you need to look for another variable.

    Let us use the information we have learned about Smarty plugins and Smarty variables by combining them both to create a title of the page. Open the template Business World (Layout | Templates)for editing and change the title of the page between the body tags and before the tag {content} shown as follows:

    {$friendly_position} {title}


    Then open Business World Style Sheet for editing (Layout | Stylesheets), and add a CSS style to format the title of the page:

    h1 span 
    {
    color: #ffffff;
    background: #cccccc;
    padding: 0 5px;
    }

    The result of the above formating should look as shown in the following screenshot:

    CMS Made Simple 1.6: Beginner's Guide

    You  can use any Smarty variable from the template, except for variables with the value Array(). We will look at these special variables in the following section.

    Controlling output with the IF function

    You can create numerous templates for your website and assign different templates to different pages. This is useful if you use layouts with a different number of columns. However, sometimes there is only a tiny difference between the templates, and it is not efficient to create a new template each time you need only slight changes.

    For example, imagine you would like to display the last editor of the page, as we did with the {last_modified_by}tag. It is a useful piece of information on most pages but we would like to hide it on the contact page. You do not need to create a new template where this tag is not added. For such slight changes, it is better to know how to control the output in the same template with an IF structure.

    Time for action – displaying tags in dependence of the page

    We  are going to hide the {last_modified_by} tag on the page Contact Us. However, it has to be still displayed on all other pages.

    1. Open the template Business World for editing (Layout | Templates).
    2. Add the Smarty IF code around the {last_modified_by…} tag, as shown in the following code snippet:



        {title} - {sitename}
        {stylesheet}
        {metadata}






        {menu number_of_levels="1" template="minimal_menu.tpl"}


        {title}


        {content}
        {if $page_alias neq "contact-us"}

        Last modified by {last_modified_by format=


        "fullname"}

        {/if}






    3. Click on Apply and then click on the magnifying glass icon in the top-right corner of the admin console to see the result.

    What just happened?

    The IF code that you have added around the paragraph containing the last modification causes CMS to check the page alias of the displayed page. If the page alias is equal to “contact-us”, then everything between the IF structure is not shown, otherwise the information about the last modification is displayed.

    You have seen from the previous section that CMS knows what page of our website is currently being displayed. This information is stored in the Smarty variable {$page_alias}. With the built-in IF function, you can compare the page alias of the actual page with the page alias of the page Contact Us. If the value of the variable {$page_alias} is NOT equal to contact-us, then everything between the IF tags is displayed. If the page alias is equal to contact-us, then nothing is displayed. In this way, you can control the output of the template depending on the page alias.

     

    The abbreviation neq (meaning not equal) between the variable {$page_alias} and the value contact-us is called a Qualifier. Qualifiers are used to build a logical condition in the IF code. The result of the logical condition can be true or false. If the result of the IF condition is true (and it is true if the page alias IS NOT EQUAL to contact-us), then everything placed in between the IF tags is displayed. If the result of the IF condition is false (and it is only false if the page alias IS EQUAL to contact-us), then everything between the IF tags is suppressed.

    There are more qualifiers that can be used to build logical conditions in Smarty. Some of them are listed in the following table:

    The IF structure is a useful tool for handling slight changes in one template depending on the page name or the position in the hierarchy. In the preceding example, you saw that you can use every variable from the template to build a logical condition.

    Creating navigation template with Smarty loop

    You can also change the HTML markup of the navigation. Before you can learn this principle, you have to understand some Smarty basics.

    When we added the top navigation to the website, we used a standard template for the navigation. It displays the navigation as an unordered HTML list. Imagine that you need a kind of footer navigation where all the links from the top navigation are shown. You do not need an unordered HTML list in this case. You just would like to show all links in one line separated by a pipe (|) shown as follows:

    Our Company | Announcements | History | Team | Photo gallery ……

    This means that you need a completely different HTML markup for this kind of navigation. The great advantage of CMS Made Simple is the ability to display a template in template. While you can use the main template to define the whole layout for the page, the  HTML markup of the navigation is saved in its own template. This navigation template is just a piece of the HTML code that is added to the main template at the place where the tag {menu} is placed.


    Subscribe to the weekly Packt Hub newsletter

    * indicates required

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here