Lasso Soft Inc. > Home

  • Articles

Capture and Display Page Elements

This article shows how to capture portions of a page and display them in a different order. Specifically, a number of HTML elements can be re-displayed in random order.

Background

I was working on the Lasso Developer Conference 2008 Topics and Speakers Web page and wanted to display the speakers in random order for different site visitors. This tip shows how it was easy to do that without completely changing the structure of the page.

<h2>Topic One</h2>
<p>Description</p>

<h2>Topic Two</h2>
<p>Description</p>

 

Note: An earlier version of this tip defined the [Capture] ... [/Capture] tags rather than [Collect] ... [/Collect] tags, but "capture" is already used as a tag name in some solutions so "collect" is a better name for this functionality moving forward.

Solution

The solution was to create a simple container tag [Collect] ... [/Collect] which can capture a portion of text on the page and store it into a variable or an array. This allows the page elements to be placed in an array temporarily and then output from the array in random order.

The following code shows the elements are inserted into an array called $speakers.

[Var('speakers') = Array]

[Collect(-Insert=$speakers)]
<h2>Topic One</h2>
<p>Description</p>
[/Collect]

[Collect(-Insert=$speakers)]
<h2>Topic Two</h2>
<p>Description</p>
[/Collect]

 

Now the $speakers array contains the HTML elements as separate array elements. Now, we display those in random order by getting and removing them from the array one by one.

[While($speakers->Size > 0)]
  [Var('index') = Math_Random(-Lower=1, -Upper=$speakers->size)]
  [$speakers->Get($index)]
  [$speakers->Remove($index)]
  <hr />
[/While]

Each time the code is run the elements will be output in random order.

<h2>Topic Two</h2>
<p>Description</p>

<h2>Topic One</h2>
<p>Description</p>

 

Since, the random display can be a little jarring to site visitors, the [Cache] ... [/Cache] tags were used to prevent the elements from re-ordering more than once an hour. This code is placed around the entire portion of the page from the definition of the $speakers variable to the end of the [While] ... [/While] loop.

[Cache('LDC2008_Speakers', -Expires=(60*60))]
... Captures and Random Display ...
[/Cache]

 

Implementation

The implementation of the [Collect] ... [/Collect] tag is simple. The tag uses [Run_Children] to get the contents of the container. It stores these results in a private variable whose contents can be retrieved using [Collect_Result]. For example, the following code sets a variable to the contents of the collect tags and inserts the contents into an array.

[Collect] ... [/Collect]

[Var('myvariable') = Collect_Result]

[$myarray->Insert(Collect_Result)]

 

If a parameter -Var is specified then the contents is placed into that variable. [Collect(-Var='myelement')] ... [/Collect] will define a variable $myelement which contains the contents of the tags.

If a parameter -Insert is specified then the contents is inserted into that variable. The variable should be an array or other compound type. Either the variable itself can be specified as [Collect(-Insert=$myarray)] ... [/Collect] or the name of the variable can be specified as [Collect(-Insert='myarray')] ... [/Collect]. The variable should already exist before the tag is called.

Normally, the [Collect] ... [/Collect] tag produces no output itself. However if -Output is specified then the collect tag will both capture and output the contents of the tags.

Here is the implementation of the [Collect] ... [/Collect] tag. This tag can be defined on the page where it is used, defined in LassoStartup, or placed into a file Collect.Lasso in the LassoLibraries folder for dynamic loading.

[
  Define_Tag('collect', 
      -Optional='var', 
      -Optional='insert', 
      -Optional='output', 
      -Container);
    Local('collect') = Run_Children;
    Local('types') = Array('array', 'list', 'queue', 'set', 'stack');
    Var('__collect_result__') = #collect;
    Local_Defined('var') && #var->IsA('string') ? Var(#var) = #collect;
    If(Local_Defined('insert'));
      If(#types >> #insert->type);
        #insert->Insert(#collect);
      Else(#insert->IsA('string') && Var_Defined(#insert) 
          && (#types >> Var(#insert)->Type));
        Var(#insert)->Insert(#collect);
      /If;
    /If;
    If(Local_Defined('output') && #output !== False);
      Return(#collect);
    /If;
  /Define_Tag;
  Define_Tag('collect_result');
    Return(Var('__collect_result__'));
  /Define_Tag;
]

Author: Fletcher Sandbeck
Created: 7 Mar 2008
Last Modified: 2 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