(source) C. Scott Ananian: Ideas/A Dozen Visions for Wikitext/Visual Templates

Visual Templates

We’ve made some proposals which facilitate creating and using wikitext-based templates.  But wikitext is not the only transclusion mechanism on our platform. In A Plan For Scribunto I briefly mentioned adding a "wikitext" slot to Scribunto modules to allow using VisualEditor to edit the presentation layer of Scribunto modules. The idea is to move the presentation layer out of Scribunto and use Scribunto only for computation and logic.  Instead, each Scribunto module has an associated wikitext slot to handle presentation. Wikitext in that slot is rendered in a “data context” provided by the Scribunto module, using parser functions similar to those introduced by the ArrayFunctions extension to access the data context. The data computed by Scribunto fills in the blanks in the wikitext, using a "logic-less" template system similar to Spacebars, Handlebars, or Mustache. This results in a simple block+placeholder structure compatible with creating/editing the wikitext slot using VisualEditor.

Here is an example of "logic-less" templates, in this particular case Mustache, with the Mustache syntax highlighted in red:

Mustache template
Hello {{name}}

You have just won {{value}} dollars!
{{#if in_ca}}
Well, {{taxed_value}} dollars, after taxes.

{{/if}}

There are only two constructs: named placeholders, like {{name}} and {{value}}, and blocks, like the section between the {{#if}} and the {{/if}}.  Blocks have a main body and an optional “else” body (not shown in this example), and insert the content of their bodies one or more times into the output.

This template is evaluated in a data context such as the following, presented here in JSON format:

{
  "name": "Chris",
  "value": 10000,
  "taxed_value": 6000,
  "in_ca": true
}

And the Mustache output for this example would be:

Mustache template output
Hello Chris

You have just won 10000 dollars!

Well, 6000 dollars, after taxes.

Here’s that same example translated into wikitext syntax.

Wikitext logic-less template
Hello {{{name}}}

You have just won {{{value}}} dollars!
{{#if:{{{in_ca}}}|<<<
Well, {{{taxed_value}}} dollars, after taxes.

>>>}}

It’s very similar!  The placeholders use triple-brace syntax in wikitext, but are still referring to the data context for the module.  I used long argument syntax to pass block content.

In visual editor, the placeholders appear as editable elements, and the "if" and "else" arguments to blocks (aka the arguments to certain parser functions) are editable inline.

VisualEditor editing a "logic-less" template with placeholders and blocks.

This is a constrained version of the normal wikitext template system, with the "data context" replacing template arguments.[1]

This mechanism is also how HTML-only wikis could work with templates. In that case the "wikitext" slot would actually be stored as "HTML", but it could use a similar logic-less template mechanism integrated with Scribunto and editable (as MediaWiki DOM Spec HTML) with VisualEditor.

Next section: Page Description Language

  1. And if the Scribunto code slot is empty, perhaps the template arguments (packaged as a structured data key/value array) are the default "data context". This provides for a migration path for existing templates.
C. Scott Ananian [[User:cscott]]