renamed a few template -> engine, credited waterlink, added render method, bump version

This commit is contained in:
Jerome Gravel-Niquet 2016-02-14 22:22:35 -05:00
parent f4965bf66d
commit 8fb4bd73f8
5 changed files with 24 additions and 15 deletions

View file

@ -54,12 +54,14 @@ str = String.build do |__kilt_io__|
Kilt.embed "path/to/template.slang" Kilt.embed "path/to/template.slang"
end end
# or `Kilt.render "path/to/template.slang"`
puts str # => <compiled template> puts str # => <compiled template>
``` ```
## Registering your own template engine ## Registering your own template engine
Use `Kilt.register_template(extension, embed_command)` macro: Use `Kilt.register_engine(extension, embed_command)` macro:
```crystal ```crystal
require "kilt" require "kilt"
@ -70,7 +72,7 @@ module MyEngine
end end
end end
Kilt.register_template("myeng", ::MyEngine.embed) Kilt.register_engine("myeng", ::MyEngine.embed)
``` ```
This can be part of your own `my-engine` library: in this case it should depend This can be part of your own `my-engine` library: in this case it should depend
@ -90,3 +92,4 @@ Please contribute your own "adapter" if you create a template language for Cryst
## Contributors ## Contributors
- [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer - [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer
- [waterlink](https://github.com/waterlink) Oleksii Fedorov

View file

@ -19,7 +19,7 @@ describe Kilt do
end end
it "raises with unsupported filetype" do it "raises with unsupported filetype" do
expect_raises(Kilt::Exception, "Unsupported template type \"abc\"") { expect_raises(Kilt::Exception, "Unsupported template engine for extension: \"abc\"") {
render_file("test.abc") render_file("test.abc")
} }
end end

View file

@ -2,7 +2,7 @@ require "spec"
require "../src/kilt" require "../src/kilt"
require "slang" require "slang"
Kilt.register_template("slang", embed_slang) Kilt.register_engine("slang", embed_slang)
macro render_file(filename) macro render_file(filename)
String.build do |__io__| String.build do |__io__|

View file

@ -3,27 +3,33 @@ require "./kilt/*"
module Kilt module Kilt
# macro only constant # macro only constant
TEMPLATES = {} of String => Int32 ENGINES = {} of String => Int32
macro register_template(ext, embed_macro) macro register_engine(ext, embed_macro)
{% ::Kilt::TEMPLATES[ext] = embed_macro %} {% Kilt::ENGINES[ext] = embed_macro.id %}
end end
macro embed(filename, io_name = "__kilt_io__") macro embed(filename, io_name = "__kilt_io__", *args)
{% ext = filename.split(".").last %} {% ext = filename.split(".").last %}
{% if ::Kilt::TEMPLATES[ext] %} {% if Kilt::ENGINES[ext] %}
{{::Kilt::TEMPLATES[ext]}}({{filename}}, {{io_name}}) {{Kilt::ENGINES[ext]}}({{filename}}, {{io_name}}, {{*args}})
{% else %} {% else %}
raise Kilt::Exception.new("Unsupported template type \"" + {{ext}} + "\"") raise Kilt::Exception.new("Unsupported template engine for extension: \"" + {{ext}} + "\"")
{% end %} {% end %}
end end
macro file(filename, io_name = "__kilt_io__") macro render(filename, *args)
String.build do |__kilt_io__|
Kilt.embed({{filename}}, "__kilt_io__", {{*args}})
end
end
macro file(filename, io_name = "__kilt_io__", *args)
def to_s({{io_name.id}}) def to_s({{io_name.id}})
Kilt.embed({{filename}}, {{io_name}}) Kilt.embed({{filename}}, {{io_name}}, {{*args}})
end end
end end
end end
::Kilt.register_template("ecr", embed_ecr) Kilt.register_engine("ecr", embed_ecr)

View file

@ -1,3 +1,3 @@
module Kilt module Kilt
VERSION = "0.1.1" VERSION = "0.2.0"
end end