ez.no / exponential / documentation / development / libraries / ez template / basics / 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.
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
Frederik Holljen
Tuesday 21 September 2004 1:03:16 pm
no more set
andreas spahr
Thursday 29 January 2004 1:20:01 pm
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
Marco Zinn
Monday 24 November 2003 4:11:25 pm
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
M Desdin
Friday 07 November 2003 3:21:00 pm
TIA, MD.
Absolute addressing
M Desdin
Friday 07 November 2003 3:10:19 pm
I mean if you spell it only $, it works!
So, which one is the correct/best practice?
TIA, MD.