Skip to content
FutureBlitz

    Install

    Import std.text.template

    std.text.template

    Mustache-style templates, filled from Values and other types.

    A Value fills one, or a Struct, a Class, or a Map, through the Value that Save makes of it. A template is text with tags in "{{ }}", each filled in when it's rendered: {{name}} gives a value, the blocks {{#each name}}, {{#if name}}, and {{#unless name}} go over or test one, with an {{else}} for when there's nothing, {{> name}} renders another template there, and {{! }} is a comment. A value can take a format spec, as {{price:.2}} does, as interpolation's "{price:.2}" would. A block's tag alone on its line takes the line with it. A template for HTML escapes its values.

    std/text/template.fb

    Types

    Template class

    Public Class Template

    A template, read once, which Render fills from what it's given each time.

    Examples

    Local greeting = Try Template(r"Hello, {{name}}! {{#each items}}[{{this}}]{{/each}}")
    Local data = Value.Object()
    data["name"] = Value("Ada")
    data["items"] = Value([Value("a"), Value("b")])
    Expect((Try greeting.Render(data)) == "Hello, Ada! [a][b]")

    std/text/template.fb, line 30

    • Public New(text: String, html: Bool = False) Throws FormatError constructor #

      The template that text is, which escapes its values for HTML with html. Throws a FormatError of BAD_TEMPLATE where the text isn't one, which says the line and column.

    • Public Method Render(data: Value): String Throws FormatError method #

      The template, filled from a Value, whose Object's keys are the names the tags give. Throws a FormatError of MISSING_VALUE at a partial, which only Templates have.

    • @PanicsAtCaller Public Method Render<T>(data: T): String Throws FormatError method #

      The template, filled from the Value that Save makes of data: a Struct's or Class's Fields by name, a Map's keys, and the rest as Save has them. Panics, as Save does, for data that nests more than 256 levels deep.

      Examples

      Struct Page
          Field Title: String
      End Struct
      
      Local page = Try Template(r"<h1>{{Title}}</h1>", True)
      Expect((Try page.Render(Page("Fish & chips"))) == "<h1>Fish &amp; chips</h1>")

    Templates class

    Public Class Templates

    Templates by name, which render each other as partials: {{> header}} is the template named "header". One whose name ends with ".html" or ".htm" escapes its values for HTML, as a partial too, and so do the partials it renders, whatever their names.

    Examples

    Local site = Templates()
    Try site.Add("header", r"<h1>{{title}}</h1>")
    Try site.Add("page", r"{{> header}}<p>{{body}}</p>")
    Local data = Value.Object()
    data["title"] = Value("Hi")
    data["body"] = Value("there")
    Expect((Try site.Render("page", data)) == "<h1>Hi</h1><p>there</p>")

    std/text/template.fb, line 80

    • Public New() constructor #

      A new Templates, with none added.

    • Public Method Add(name: String, text: String) Throws FormatError method #

      Adds the template that text is as name, in place of one that has the name already. Throws a FormatError of BAD_TEMPLATE where the text isn't one, which says the template's name, the line, and the column.

    • Public Method Contains(name: String): Bool method #

      Whether it has a template of the name.

    • Public Method Render(name: String, data: Value): String Throws FormatError method #

      The template of the name, filled from a Value, whose Object's keys are the names the tags give. Throws a FormatError of MISSING_VALUE where it has no template of the name, or of a partial's, and of OTHER for partials more than 1000 deep inside each other.

    • @PanicsAtCaller Public Method Render<T>(name: String, data: T): String Throws FormatError method #

      The template of the name, filled from the Value that Save makes of data. Panics, as Save does, for data that nests more than 256 levels deep.

    Report an issue with this page or with FutureBlitz on GitHub.