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
expand attribute is not a tag of its own
but is an attribute of all block tags even metatags defined with
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
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:
This produces the following:
1. <set time="0">
2. <block name=a>
3. a is expanded at time <use time>.
5. <block name=b expand>
6. b is expanded at time <use time>.
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>
16. <block name=d expand>
17. d is expanded at time <use time>; <use a noexpand>
19. <block name=e>
20. e is expanded at time <use time>; <use a noexpand>
22. <set time="2">
23. <block name=a>
24. a is defined for the 2nd time <use time>.
26. <use c>
27. <use d>
28. <use e>
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
is to prevent double expansion and to restore the original lazy
expansion policy when you have to use the
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
expanded block should be