This is an old revision of the document!


Macro Command

The Macro command allows a template section, containing data and/or PTPScript code, to be defined as a macro. In this context, a macro is essentially a sub-template, and behaves very much like a function. It has its own Variable "scope" - it cannot access Variables that are available in the main template. Any Variables that are created or modified within the macro will only affect the data inside that macro.

When a Macro command is first encountered, the macro data is not displayed, but is instead stored for later use. Parameters can be defined, so that values can be passed to the macro. When required, the macro can be displayed by calling it in the same way as a function. This means that the output of a macro can be used in an expression in the same way as that of a function.

There are two commands in the Macro family - Macro and EndMacro. Each Macro command has to have a corresponding EndMacro command.

The Macro command consists of the command, followed by the macro name, and then the names of any parameters, contained within parentheses and separated by commas. The EndMacro command does not use an expression, and so will ignore any expression given.

For example:

{ macro Greeting ( Name, MealOfTheDay ) }…
Hi there, {Name}! Would you like {MealOfTheDay}?
{ endmacro }{ Greeting ("Jim", "fish and chips") }

Hi there, Jim! Would you like fish and chips?

Macro names are case-insensitive, just like functions.

Macro definitions and calls can be nested indefinitely within other Macro definitions, and within other command structures, however each macro is fully enclosed and cannot detect or affect anything else. All Macro definitions are global in scope - that is, they can be called from anywhere, no matter where they were defined. However, if defined within a macro, the containing macro has to be called before the definition will be processed.

Macros can be re-declared without error. The latest definition will apply for all code following it.

Checking for macros

Sometimes it may be necessary to check for a macro, and in such a situation, type checking should be used to determine whether the macro has been defined.

For example:

{ if "Greeting" is a macro }{   Greeting ("Jim", "fish and chips") }
{ endif }

Resolving ambiguity

Because macros are called in the same way as functions, a macro could be declared with the same name as a function. In this case, the function will always be called, not the macro.

ptpscript/commands/macro.1169633753.txt.gz · Last modified: 2007/01/24 19:39
 
 
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki