Lasso Soft Inc. > Home

  • Articles

Tag Sets and Namespaces

The tip of the week for March 21, 2008 explains how Lasso's tag namespaces can be used to more easily call sets of custom tags which you have defined. For example, this allows you to use a different set of custom tags for several sites that otherwise share the same code.

Introduction

Lasso's tags are sorted into categories like [String_...], [Math_...], [Encode_...], etc. In Lasso 8.5 these categories are referred to as namespaces. For the tag [String_Uppercase] the namespace is String_ and the tag name is Uppercase.

If you are performing a series of string operations you can use the [Namespace_Using] ... [/Namespace_Using] tags to specify that you want to work in the String_ namespace. Then you can call all the [String_...] tags without specifying the namespace on each tag.
 

  Namespace_Using('String_');
    Uppercase('ThE qUiCk BrOwN ');
    Lowercase('FoX JuMpEd OvEr ');
    Uppercase('tHe lAzY dOg.');
  /Namespace_Using;

 THE QUICK BROWN fox jumped over THE LAZY DOG.

The remainder of this tip will show how this basic technique can be used to create your own namespace of tags which over-ride built in tags and how to create sets of custom tags which can be selected at runtime by modifying the surrounding [Namespace_Using] ... [/Namespace_Using] tags.

Custom Namespace

Ever since custom tags were introduced it has been recommended that you name your own custom tags with a unique prefix. If you worked for LassoSoft you might name your custom tags with the prefix LS_. This naming convention can help ease confusion since your [LS_Site_Title] tag will never be confused with the [OP_Site_Title] tag from another company.

In Lasso 8.5 the prefix should be specified as a namespace. When you define your tags take everything before the last underscore character and specify that as the -Namespace in the [Define_Tag]. The [LS_Site_Title] tag might be defined as follows.
 

  Define_Tag('Title', -Namespace='LS_Site_');
    Return('LassoTech');
  /Define_Tag;

It can get laborious specifying the LS_ prefix for every tag that you use in your site design so you an use the [Namespace_Using] ... [/Namespace_Using] tags around all of your site code to specify that you want all of your tags to be pulled from the LS_ namespace by default.

  Namespace_Using('LS_');
    ...
    Site_Title;
    ...
  /Namespace_Using;
]
 LassoTech

Replacing Built-In Tags

You can use this technique to redefine built-in tags. If you want to provide your own version of the [Redirect_URL] tag you can define a new tag in the LS_ namespace. The following code creates a new [LS_Redirect_URL] tag which redirects the user to the specified destination with an added name/value pair.

  Define_Tag('URL', -Namespace='LS_Redirect_', -Required='url');
    _global_Redirect_URL(#url + (#url>>'?'?'&'|'?') + 'myparam=myvalue');
  /Define_Tag;

 

Note that we call the built-in [Redirect_URL] tag by the name _global_Redirect_URL. The namespace _global_ (it should have a lowercase "g") bypasses searching other namespaces and fetches the built-in tag directly.

If we call our [Redirect_URL] tag within [Namespace_Using] ... [/Namespace_Using] tags as follows then the user will get redirected to the URL with the additional parameter as shown.

 Namespace_Using('LS_');
    ...
    Redirect_URL('http://www.lassosoft.com/');
    ...
  /Namespace_Using;
]
 http://www.lassosoft.com/?myparam=myvalue

If you are using a template it is often possible to surround the [Include] which pulls in the actual page code with [Namespace_Using] ... [/Namespace_Using] tags. The programmer of the included file does not necessarily even need to know that they are calling a custom version of [Redirect_URL]. From their point of view, the tag works exactly like the built-in tag, except for the added parameter.

Site Namespaces

Using multiple namespaces can help organize sets of tags for a single developer of multiple sites. Often a single developer will use a common code base for several different clients. The site title, colors, graphics, etc. will be different for different clients, but the basic functionality of the sites will be the same.

For example, one developer might develop sites for different musical artists The Grid and The Orb. The sites will share common design, but have different titles, graphics, artist biographies, discographies, etc. The individual content would be inserted using a series of tags [Artist_Title], [Artist_Header], [Artist_Biography], [Artist_Discography], etc.

We can use namespaces to make the design of this site easier. First, we define a series of tags for The Orb in an Orb_ namespace. Note that the [Namespace_Using] ... [/Namespace_Using] tags here surround the tag definitions. This is an alternate method of placing tags into a specific namespace. We can place these tags in an include file or even in LassoLibraries in a file named "orb.lasso" for on-demand loading.
 

 Namespace_Using('Orb_');
    Define_Tag('title', -Namespace='artist_');
      Return('The Orbs Ultraworld');
    /Define_Tag;
    Define_Tag('header', -Namespace='artist_', -EncodeNone);
      Return('<img src="battersea_and_fluffy.jpg" />');
    /Define_Tag;
    Define_Tag('biography', -Namespace='artist_');
      Return('Pioneers of the ambient techno movement of the nineties...');
    /Define_Tag;
    ...
  /Namespace_Using;

 

Then we define a series of tags for The Grid in the Grid_ namespace. Again, these could be in an include file (even the same include as the Orb tags) or in LassoLibraries in a file named "grid.lasso" for on-demand loading.

 
 Namespace_Using('Grid_');
    Define_Tag('title', -Namespace='artist_');
      Return('The Grids Doppelganger');
    /Define_Tag;
    Define_Tag('header', -Namespace='artist_', -EncodeNone);
      Return('<img src="baby_and_303.jpg" />');
    /Define_Tag;
    Define_Tag('biography', -Namespace='artist_');
      Return('Best known for their banjo heavy single Swamp Thing...');
    /Define_Tag;
    ...
  /Namespace_Using;

 

Now, we create our site template. At the top we define what namespace we want to use. We could determine this based on the [Server_Name] or other means so it is set automatically, or we could share files between the two domains, but set this variable manually.

 Var('namespace') = 'Orb_';
  
  Namespace_Using($namespace);
    '<html>rn';
    '  <head>rn';
    '    <title>'; Artist_Title; '</title>rn';
    '  </head>rn';
    '  <body>rn';
    '    <div id="header">'; Artist_Header; '</div>rn';
    '    <div id="body">'; Artist_Biography; '</div>rn';
    ...
    '  </body>rn';
    '</html>';
  /Namespace_Using;

All of the [Artist_...] tags which generate the page will automatically use the Orb variants. If we change the $namespace variable at the top to "grid_" then the Grid variants will be used instead.

If we get commissioned to do a site for The The we can clone the include file and create a new "The_" namespace. We can put the new Web site together making only minimal changes to the common template.

More Information

More information about all of the tags used in this tip of the week can be found in the Lasso 8.5 Language Guide or in the online Lasso Reference

More information about The Orb can be found at http://www.theorb.com, The Grid can be found at http://www.mit.edu/people/mattski/grid, and The The can be found at http://www.thethe.com. Although, clearly their sites aren't parameterized versions of one another.
 

Author: Fletcher Sandbeck
Created: 21 Mar 2010
Last Modified: 24 Mar 2011

Comments

No comments found
You must be logged in to comment.

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft