Include with variables processing

GabrysGabrys wrote on 24 Mar 2010 13:53

This is to reply to James' question about includes with variables.

First of all what kind of syntaxes we want to support:

[[include page | variable1=value1]]

Simple include.

[[include page | %%content%%]]

We pass all the content as variables section. It needs to be in form of: "variable = value | variable = value | …"

[[include page | variable1=%%content%%]]

We pass all the content as ONE variable, so that in include, there is %%content%% available as ${variable1}.

One small note: I don't really believe supporting passing %%content%% as multiple variables makes any sense. This might be needed to overcome some limitations. I'm just guessing. Please tell me why we need to support this.

So let's get started.

First of all we need the following tags:

  • <include> — [[include]] recognized, page source waits to be injected there.
    • <variable> — variable passed to include
  • <include-variable> — this is what ${variable} is converted to in included page
  • <template-symbol> — a template symbol, for example %%content%%

Let the parser play :-)

<wiki>
[[include page | variable1=value1]]
</wiki>

Needs to be:

<include>
 <page-name>page</page-name>
 <variables>
  <variable>
   <name>variable1</name>
   <value>value1</value>
  </variable>
 </variables>
</include>

Second example. This time we will expose more passes of parsing:

<wiki>
[[include page | %%content%%]]
</wiki>

<include>
 <page-name>page</page-name>
 <variables>
  <wiki>%%content%%</wiki>
 </variables>
</include>
<include>
 <page-name>page</page-name>
 <variables>
  <template-symbol>content</template-symbol>
 </variables>
</include>

Suppose %%content%% is variable2=value2 | variable3=value3

<include>
 <page-name>page</page-name>
 <variables>
  <wiki>variable2=value2 | variable3=value3</wiki>
 </variables>
</include>
<include>
 <page-name>page</page-name>
 <variables>
  <variable>
   <name>variable2</name>
   <value><wiki>value2</wiki></value>
  </variable>
  <variable>
   <name>variable3</name>
   <value><wiki>value3</wiki></value>
  </variable>
 </variables>
</include>
<include>
 <page-name>page</page-name>
 <variables>
  <variable>
   <name>variable2</name>
   <value>value2</value>
  </variable>
  <variable>
   <name>variable3</name>
   <value>value3</value>
  </variable>
 </variables>
</include>

Nice. Let's go further:

<wiki>
[[include page | variable1=%%content%%]]
</wiki>

<include>
 <pagename>page</pagename>
 <variables>
  <wiki>variable1=%%content%%</wiki>
 </variables>
</include>
<include>
 <pagename>page</pagename>
 <variables>
  <variable>
   <name>variable1</name>
   <value><wiki>%%content%%</wiki></value>
  <variable>
 </variables>
</include>
<include>
 <pagename>page</pagename>
 <variables>
  <variable>
   <name>variable1</name>
   <value><template-symbol>content</template-symbol></value>
  <variable>
 </variables>
</include>
<include>
 <pagename>page</pagename>
 <variables>
  <variable>
   <name>variable1</name>
   <value><wiki>variable2=value2 | variable3=value3</wiki></value>
  <variable>
 </variables>
</include>

All content passed as one variable. This works nicely :)

Note:

If you look closely at what happens, we always defer symbol replacement, so we can match different rules that don't match after symbols are replaced (variable1=%%content%% — we want all %%content%% even if %%content%% has "|other-variable=something-else" in it!). But some rules may match only after replacement (like [[include page | %%content%%]]. So we need to apply wiki-syntax rules after symbol replacement as well. The trick is to make the applying rules safe on text that was already matched (so we can apply rules many many times). The XML-like notion is the answer to this.

(In this and previous examples, XML is only an example coding, that's easy to understand for most of Wikidot users. Actual coding as well as some internal names/tags may be totally different, but how it works will be the same.)


Start a new sub-thread

Comments: 4

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License