htp on-line reference : global and expand

Table of Contents
    Predefined macros
  > global and expand
  HTP Tags
  Wish list

The global attribute

Normally when you define a macro it is only defined in the current scope. A scope is for example the definition of a metatag. Once the definition is expanded and the metatag has finished the macro is automatically removed. In some cases you want to define a macro inside a metatag and use it later. You can do this with the global attribute that you can add to [ref]set, [ref]inc, [ref]block, [ref]def, and [ref]blockdef tags.

The expand attribute

The expand attribute is not a tag of its own but is an attribute of all block tags even metatags defined with the blockdef macro. Normally htp uses a lazy evaluation strategy, i.e. it expands htp tags, metatags and macros when the HTML document is written not when they're stored into block macros. When you use a macro defined with the blockdef tag the html code between the tags is just copied literally into the macro named block, without any expansion going on.

However, if the block tag is marked with the expand modifier it is expanded immediately. This is useful if you want to store something in a block macro, that depends on the current value of another macro. This other macro may no longer have the correct value when the macro is expanded. Here is some code that demonstrate when the htp tags are expanded:

 1.   <[ref]set time="0">
 2.   <[ref]block name=a>
 3.     a is expanded at time <[ref]use time>.
 4.   <[ref]/block>
 5.   <[ref]block name=b expand>
 6.     b is expanded at time <[ref]use time>.
 7.   <[ref]/block>
 8.   <[ref]set time="1">
 9.   <[ref]use a>
10.   <[ref]use b>
11.   contents of a: <[ref]use a noexpand>
12.   contents of b: <[ref]use b noexpand>
13.   <[ref]block name=c expand>
14.     c is expanded at time <[ref]use time>; <[ref]use a>
15.   <[ref]/block>
16.   <[ref]block name=d expand>
17.     d is expanded at time <[ref]use time>; <[ref]use a noexpand>
18.   <[ref]/block>
19.   <[ref]block name=e>
20.     e is expanded at time <[ref]use time>; <[ref]use a noexpand>
21.   <[ref]/block>
22.   <[ref]set time="2">
23.   <[ref]block name=a>
24.     a is defined for the 2nd time <[ref]use time>.
25.   <[ref]/block>
26.   <[ref]use c>
27.   <[ref]use d>
28.   <[ref]use e>

This produces the following:

    a is expanded at time 1.
    b is expanded at time 0.
  contents of a:   a is expanded at time <[ref]use time>.
  contents of b:   b is expanded at time 0.
    c is expanded at time 1;   a is expanded at time 1.
    d is expanded at time 1;   a is expanded at time 2.
    e is expanded at time 2;   a is defined for the 2nd time <[ref]use time>.

When block a and block b are defined in lines 2-7 the contents of a are taken literally, however, the contents for b are already expanded so the macro time is evaluated. So a is expanded when it is used, while the contents of b are already expanded.

In line 10 and 12 the [ref]use tag is used with the noexpand attribute which prevents the expansion of the macro value. Here it is used to show you what the value of the block macros really is.

A more important reason to use the noexpand attribute is to prevent double expansion and to restore the original lazy expansion policy when you have to use the expand attribute as for block d in the example above. See the section example in the [ref]metatag tutorial. Rule of thumb: The use tags in an expanded block should be tagged with noexpand.

  page source
  htp project
  htp homepage
hosted by
HTML coding Powered by htp
htp on-line reference /
Authors: Jim Nelson, Jochen Hoenicke, Michael Möller.
Maintainers: Jochen Hoenicke.

Copyright © 1995-96 Jim Nelson.
Copyright © 2001-2003 Jochen Hoenicke.
Permission to reproduce and distribute this hypertext document granted according to terms described in the License section.

last updated Tue Feb 22, 2011