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]")-
The template that
textis, which escapes its values for HTML withhtml. Throws a FormatError of BAD_TEMPLATE where thetextisn't one, which says the line and column. -
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.
-
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, fordatathat 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 & 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>")-
A new Templates, with none added.
-
Adds the template that
textis asname, in place of one that has thenamealready. Throws a FormatError of BAD_TEMPLATE where thetextisn't one, which says the template'sname, the line, and the column. -
Whether it has a template of the
name. -
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 thename, or of a partial's, and of OTHER for partials more than 1000 deep inside each other. -
The template of the
name, filled from the Value that Save makes ofdata. Panics, as Save does, fordatathat nests more than 256 levels deep.
Report an issue with this page or with FutureBlitz on GitHub.