• News
  • Developer information
  • Projects & contributions
  • Forum
  • Issue tracker
  • Open Funding
  • Security
  • User groups
  • Support
  • Store
  • Company

  • Path

    ez.no / exponential / documentation / development / libraries / ez template / basics / namespaces


    Namespaces

    These documentation pages are no longer maintained. Please visit the new documentation site.

    Namespaces allow separation of variables with the same name. This is useful for functions that create variables on the fly (e.g. section).

    When you are editing a template there are always three namespace locators defined which you can utilize to address your variables. These three are:

    Name

    Addressed using

    Description

    Global namespace

    $#

    The global namespace does not have a name. It is created when a new template object is created from PHP code. Refering to a variable using the global namespace is refered to as absolute addressing.

    Root namespace

    $

    The root namespace is the toplevel namespace relative to the template file you are in. Notice that the root namespace can be different from the global namespace if you use the include function.

    Current namespace

    $:

    The current namespace is the topmost namespace defined at the place where you are editing.

    When a new template is created all three namespace variables are set to the global namespace. New namespaces can then be opened using the name attribute present in many template functions. These are:

    Template functions with the name attribute

    section

    switch

    include

    append block

    default

    let

    sequence

    set-block

    If the name attribute is omitted when using any of these functions no new namespace is created. The namespace is closed together with the closing tag of the function. Each time a namespace is created it is created within the current namespace. This means that you can have nested namespaces.

    Example

    This is an initial template file which starts out in the global namespace.

    {let varglobal='Hello world'}
    
    {* We did not open a namespace in the let and we are still in the global namespace *}
    
    {* This means we can print var in the following way using all of the namespace accessing methods *} 
    
    {$#varglobal}
    
    {$varglobal}
    
    {$:varglobal}
    
     
    
     {* Let's open the namespace foo and create the var varlocal using let *}
    
     {* This will change the current namespace but not the global and root namespace *}
    
     {let varlocal='Hello world in foo namespace' name='foo'}
    
       {* To print varlocal using the three accessing methods *}
    
       {$#foo:varlocal}
    
       {$foo:varlocal}
    
       {$:varlocal}
    
     
    
       {* If we want to print varglobal inside this namespace *}
    
       {$#varglobal}
    
       {$varglobal}
    
       {* Not accessible from the local scope *}
    
     
    
       {* Let's include another template here and open the scope bar *}
    
       {include uri='include.tpl' name='bar'}
    
     {/let}
    
     
    
    {/let}

    The contents of include.tpl:

    {* Let's create another varlocal *}
    
    {let varlocal='Hello world in include.tpl'}
    
    {* Let's access the new varlocal we just defined *}
    
     {$#foo:bar:varlocal}
    
     {$varlocal}
    
     {$:varlocal}
    
     
    
    {* To access the varlocal variable in the foo scope *}
    
     {$#foo:varlocal}
    
     {* Not possible to access using root and current scope *}
    
    {/let}

    Note that if you do not create any namespaces using the "name" attribute, the global, root and current namespaces always stay the same.

    Comments

    Doc page rewritten

    21. sept. 2004

    no more set

    If you want to change the value of the variable a inside another section, you shouldn't name the section:

    Here is a simple sample:
    this is wrong:
    {let a=1}
    {$a}<br>
    {section name=Loop1 loop=3}

    {section name=Loop2 loop=3}

    {section name=Loop3 loop=3}
    {set a=3}
    {$a}<br>
    {/section}

    {set a=2}
    {$a}<br>
    {/section}


    {$a}<br>
    {/section}
    {/let}
    ----------------------------------
    Instead use the following, without naming the sections:

    {let a=1}
    {$a}<br>
    {section loop=3}

    {section loop=3}

    {section loop=3}
    {set a=3}
    {$a}<br>
    {/section}

    {set a=2}
    {$a}<br>
    {/section}


    {$a}<br>
    {/section}
    {/let}

    Needs more explanation

    Hi,
    I was stuck with variable namespaces earlier, too and am happy to see this documented.
    When I thought, i understood it, i saw the "table", which is no table, but some literal pseudo-code-stuff. THIS confused me more, than is does explain something.
    Should it be a table and just suffers some missing formatting?
    This part of the docu needs a better example, instead of or based of the "table". My 2 cents.

    Re: Absolute addressing

    Sorry for the earlier post! Sometimes it is possible to call the root namespace variable spelling it only with $, but other-times it has to be spelled with $#, I have to investigate it further...
    TIA, MD.

    Absolute addressing

    Is the spelling $# to address a variable in the root namespace still in use?
    I mean if you spell it only $, it works!
    So, which one is the correct/best practice?
    TIA, MD.

    Contents

    Development

    Extensions
    Exponential datamodel
    Exponential tuning and stability
    Importing attribute data
    Kernel
    Libraries
        eZ xml
        eZ db
        eZ i18n
        eZ soap
        eZ template
           Basics
              Functions
              Operators
              Attributes
              Comments
              Namespaces
           Functions
           Operators
        eZ webdav
    Scripting
    Standards
    System overview
    Test Suite
    Using Doxygen to create API documenta...


    Created

    07/07/2003
    3:27:44 pm
    by Bård Farstad

    Last updated

    21/09/2004
    7:18:36 pm
    by Frederik Holljen

    Authors

    Bård Farstad
    Frederik Holljen



    This page is part of the Exponential documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.