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
set,
inc,
block,
def, and
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. <set time="0">
2. <block name=a>
3. a is expanded at time <use time>.
4. </block>
5. <block name=b expand>
6. b is expanded at time <use time>.
7. </block>
8. <set time="1">
9. <use a>
10. <use b>
11. contents of a: <use a noexpand>
12. contents of b: <use b noexpand>
13. <block name=c expand>
14. c is expanded at time <use time>; <use a>
15. </block>
16. <block name=d expand>
17. d is expanded at time <use time>; <use a noexpand>
18. </block>
19. <block name=e>
20. e is expanded at time <use time>; <use a noexpand>
21. </block>
22. <set time="2">
23. <block name=a>
24. a is defined for the 2nd time <use time>.
25. </block>
26. <use c>
27. <use d>
28. <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 <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 <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 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 metatag
tutorial. Rule of thumb: The
use tags in an expand ed block should be
tagged with noexpand .
|