# htp on-line reference : global and expand

## 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. <blockdef name="section" option="title">
2. <h1><use title></h1>
3. <p class="section">
4. <use block>
5. </p>
6. </blockdef>
7.
8. <section title="My Section">
9. This is a fancy section.
10. </section>
`

This produces the following:

`<def name="bigfont">
<font size="+4">
</def>
<def name="/bigfont">
</font>
</def>
<bigfont>Some large text</bigfont>
`

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`.

hosted by Sourceforge HTML coding

Copyright © 1995–96 Jim Nelson. Copyright © 2001–2014 Jochen Hoenicke. Permission to reproduce and distribute this hypertext document granted according to terms described in the License section. last updated Sat May 16, 2015