Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Crustache::Engine class for typical using
- Loading branch information
1 parent
763a8ae
commit d813bd2
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Crustache | ||
class Engine | ||
def initialize(@basedir : String, @cache = false) | ||
self.initialize ViewLoader.new @basedir, @cache | ||
end | ||
|
||
def initialize(@fs : FileSystem); end | ||
|
||
# It renders a template loaded from `filename` with `model` | ||
# and it returns rendered string. | ||
# If `filename` is not found, it returns `nil`, but it dosen't raise an error. | ||
def render(filename : String, model) | ||
@fs.load(filename).try{|tmpl| self.render tmpl, model} | ||
end | ||
|
||
def render(filename : String, model, output : IO) | ||
@fs.load(filename).try{|tmpl| self.render tmpl, model, output} | ||
end | ||
|
||
# It is a strict version `Engine#render`. | ||
# If `filename` is not found, it raise an error. | ||
def render!(filename : String, model) | ||
@fs.load!(filename).try{|tmpl| self.render tmpl, model} | ||
end | ||
|
||
def render!(filename : String, model, output : IO) | ||
@fs.load!(filename).try{|tmpl| self.render tmpl, model, output} | ||
end | ||
|
||
def render(tmpl : Template, model) | ||
Crustache.render tmpl, model, @fs | ||
end | ||
|
||
def render(tmpl : Template, model, output : IO) | ||
Crustache.render tmpl, model, @fs, io | ||
end | ||
end | ||
end |