Tilt Is Loader of Template. It is generalized template engine interface.
Add this to your application's shard.yml
:
dependencies:
tilt:
github: MakeNowJust/tilt
require "tilt"
# Load template engines which are used in your app
require "ecr/macros"
require "crustache" # MakeNowJust/crustache
require "slang" # jeromegn/slang
# Render some templates
puts Tilt.render "hello.ecr" # render as "ecr"
puts Tilt.render "hello.mustache" # render as "mustache" (using crustache)
puts Tilt.render "hello.slang" # render as "slang"
# Set the default template engine
Tilt.default_engine "ecr"
puts Tilt.render "hello" # render as "ecr" although without extension
# Other APIs
# It is like `Tilt.render`, but it requires IO object then renders to this.
puts String.build { |io| Tilt.embed "hello", io }
# Load specified template file, and define `#to_s` method to render this file.
class HelloView
def initialize(@name); end
getter name
Tilt.file "hello"
end
puts HelloView.new("Tilt").to_s
# Passing additional arguments to the template if the engine supported
puts Tilt.render("hello.mustache", { "name" => "Tilt" })
# Add your template engine
Tilt.register "html", ECR.embed
# Add alias
Tilt.alias "html", "ecr"
This is the process of Tilt.embed
.
<FilenameExtension>.embed
macro in global space. (<FilenameExtension>
is your template engine's filename extension. When it is less then 4 character, it should be upper case. Otherwise, when it is more than or equal 4 character,it should be camel-case.)Tilt.embed(filename)
is called, Tilt
calls <FilenameExtension>.embed
or registered engine if filename
has extension. Otherwise, Tilt
calls default engine.require "tilt"
module FMT
# Render `filename` by `sprintf`
def self.embed(filename, io, arg = nil)
io << sprintf(File.read(filename), arg)
end
end
# Render with `FMT` template engine
puts Tilt.render "hello.fmt", { "name" => "Tilt" }
If you create new template engine supported Tilt, I welcome your Pull Request to add it to supported engine list. (And, I wanna you add specs for this engine.)
$ crystal spec
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)