Friday, December 3, 2010

Parameterized templates

How about this?

define formatting my-snippet "text"
   parameters (bold)
   nodes
      text "$text"
      text (italic) "$text"
      text "$text"

This would define a parameterized snippet that types its input text three times, in bold, with the middle also italicized. You'd invoke it like this:

document
   para "Some initial text"
   para
      my-snippet "Repeated text here"

By default, parameters would be passed through to the defined object.

You could define-and-implement in place with this:

document
   var text "Here is my variable"
   para "Some initial text"
   <= formatting (bold)
      nodes
         text "$text"

Here, we don't need a separate "parameters" tag because we're just going to use the variables in our event context at runtime.

The reason there's a "nodes" tag in this is that I might want to include other tags as well:

define formatting my-snippet "text"
   parameters (bold)
   do {
      # Set some things up
   }
   nodes
      text ...

We'd want a whole range of tags to denote the parts of a full node: parameters, options, label, parser, code, body, and nodes.

Note that instantiation of a named macro happens at build time, while instantiation of an anonymous macro happens at runtime. To run a named macro at runtime, we'd want to do:

express my-snippet "text here"

I should implement this stuff now, then see whether it handles everything I want. Add some control structures and it could be just about as powerful as you could want.

No comments:

Post a Comment