Perhaps the most common things to do in a macro definition is accessing parameters and attributes. When doing so, it is important to consider whether we want to retrieve the raw value of and attribute or parameter or its expanded value. The difference between the two will become clearer in the following sections and also in the Interpreting Glyph Code section.
Accessing Expanded Values
Normally, you just want to get the value of an attribute or parameter and use it in the macro. This means, in other words, its expanded value, i.e. the value resulting from the expansion of the macros (if any) within the attribute or parameter.
To access expanded values, use the following methods:
parameter
(orparam
): Returns the expanded value of the parameter specified by number. Other parameters are not expanded.value
: Returns the expanded value of the first parameter (i.e. likeparameter(0)
).attribute
(orattr
): Returns the expanded value of the attribute specified by name. Other attributes are not expanded.parameters
(orparams
): Returns an array of expanded parameters.attributes
(orattrs
): Returns a hash of expanded attributes.
Accessing Raw Values
While accessing expanded values is simple and immediate, in some cases it may not produce the desired results. Consider the following macro definition:
And suppose to use it as follows:
It produces the following HTML code:
Everything is fine except for the header level: the heading "Inner Section" is of level 2, but it should be level 4!
This happens because the inner section is evaluated before the nest_section
macro: after
all, we ask for it ourselves when we call the value
method inside the macro definition. When the
value is expanded, there are no outer sections yet.
To avoid this unwanted behavior, we can use the raw_value
method instead, that returns the first
parameter converted back to a Glyph code string.
To access raw values, use the following methods:
raw_parameter
(orraw_param
): Returns the raw parameter value of the parameter specified by number.raw_value
: Returns the first raw parameter value (i.e. likeraw_parameter(0)
).raw_attribute
(orraw_attr
): Returns the attribute value of the attribute specified by name.