Skip to content
John Gietzen edited this page May 29, 2017 · 6 revisions

Template

A Weave template consists of a text file with two sections, in order:

  1. The settings.
  2. The template.

If a file named _config.weave exists in the same directory, the settings section of this file will be used as defaults.

Settings

Settings are specified like this:

@setting value

The setting's value begins after the whitespace following the setting name and extends to the end of the line.

Supported settings

  • @namespace Specifies the namespace in which the parser class will be placed.
  • @accessibility Specifies the accessibility of the generated method.
  • @static Specifies whether or not the generated method will be static.
  • @model Specifies the type of the subject (the "model") of the template.
  • @classname Specifies the name of the generated class.
  • @methodname Specifies the name of the generated template method.
  • @using Adds a using directive to the generated class file. (Multiple Allowed)
  • @encode Specifies an encoding method. Enables encoded output using the encoded echo tag, {{: }}.

Combined Example

@namespace MyProject
@classname TextGenerator
@static false
@accessibility internal
@using System.Collections.Generic
@using Foo = System.String
@model List<string>

Template

Templates are basically just text. Any text (except tags, which are surrounded by double curly braces) is parsed as plain text.

So, this is a valid template:

@namespace MyProject

Hello World!

Tags

  • {{ code }} Code tag
  • {{if expr}} If tag
  • {{each a in expr}} Each tag
  • {{: expr }} Echo tag (encoded)
  • {{= expr }} Echo tag (unencoded)
  • {{@Method expr}} Render tag
  • {{wrapif expr}}...{{body}} Wrap-if tag

Code Tag

The Code tag allows inclusion of arbitrary code in the template.

Foo
{{ var bar = 10; }}
Bar

If Tag

The If tag does what it says on the tin.

{{if model == 1}}
    One!
{{elif model == 2}}
    Two!
{{elif model == 3}}
    Three!
{{else}}
    Many! or Zero! or something else entirely!
{{/if}}

Each tag

The each tag allows iteration over an enumerable collection.

<table>
    <tr><th>Name</th><th>Value</th></tr>
    {{each item in model.Items}}
        <tr><td>{{: item.Name }}</td><td>{{: item.Value }}</td></tr>
    {{none}}
        <tr><td colspan="2"> No items. :( </td></tr>
    {{/each}}
</table>

The {{delimit}} section is supported to allow output between elements.

{{each tag in model.Tags}}{{@RenderTagLink tag}}{{delimit}}, {{none}}(no tags){{/each}}

Echo tag

The echo tag renders the string value of an expression directly.

{{: "Bar" }}
{{= "Foo" }}

The encoded and unencoded versions are identical, except that the escaped version additionally encodes the output (according to your @encode setting) before rendering it.

Render tag

The render tag is generally used to call another template in the same class:

{{@RenderHeader model.HeaderInfo}}
Body goes here.
{{@RenderFooter model.FooterInfo}}

However, the render tag simply calls a method with the following arguments: the specified model, the current text writer, and the current indentation.

Recursive templates are supported.

Wrap-if tag

The wrap-if tag allows you to conditionally surround text.

{{wrapif showBold}}
    <b>
        {{body}}
            This is conditionally bold.
        {{/body}}
    </b>
{{/wrapif}}

This results in one of these outputs.

This is the result if showBold is true:

<b>
    This is conditionally bold.
</b>

Whereas this results otherwise:

This is conditionally bold.