module
Armature::Template
Overview
Armature::Template is very similar ECR (and is, in fact, derived from it),
but whereas ECR is designed to be flexible enough for any kind of output,
Armature::Template is specific to HTML. It supports automatic sanitization
for HTML output within <%= ... %> blocks, but allows raw output using
<%== ... %> blocks — note the = vs == distinction.
Extended Modules
Defined in:
template.crConstant Summary
-
DefaultBufferName =
"__str__"
Macro Summary
-
def_to_s(filename)
Defines a
to_s(io)method whose body is the ECR contained in filename, translated to Crystal code. -
embed(filename, io_name)
Embeds an ECR file filename into the program and appends the content to an IO in the variable io_name.
-
render(filename)
Embeds an ECR file filename into the program and renders it to a string.
Macro Detail
Defines a to_s(io) method whose body is the ECR contained
in filename, translated to Crystal code.
# greeting.ecr
Hello <%= @name %>!
require "armature/template"
class Greeting
def initialize(@name : String)
end
Armature::Template.def_to_s "greeting.ecr"
end
Greeting.new("World").to_s # => "Hello World!"
The macro basically translates the text inside the given file to Crystal code that appends to the IO:
class Greeting
def to_s(io)
io << "Hello "
io << @name
io << '!'
end
end
Embeds an ECR file filename into the program and appends the content to an IO in the variable io_name.
The generated code is the result of translating the contents of the ECR file to Crystal, a program that appends to an IO.
# greeting.ecr
Hello <%= name %>!
require "ecr/macros"
name = "World"
io = IO::Memory.new
Armature::Template.embed "greeting.ecr", io
io.to_s # => "Hello World!"
The Armature::Template.embed line basically generates this Crystal code:
io << "Hello "
HTML.escape name, io
io << '!'
Embeds an ECR file filename into the program and renders it to a string.
The generated code is the result of translating the contents of the ECR file to Crystal, a program that appends to an IO and returns a string.
# greeting.ecr
Hello <%= name %>!
require "ecr/macros"
name = "World"
rendered = Armature::Template.render "greeting.ecr"
rendered # => "Hello World!"
The Armature::Template.render basically generates this Crystal code:
String.build do |io|
io << "Hello "
HTML.escape name, io
io << '!'
end