diff --git a/README.md b/README.md index cb98b60..763cf4c 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,14 @@ str = String.build do |__kilt_io__| Kilt.embed "path/to/template.slang" end +# or `Kilt.render "path/to/template.slang"` + puts str # => ``` ## Registering your own template engine -Use `Kilt.register_template(extension, embed_command)` macro: +Use `Kilt.register_engine(extension, embed_command)` macro: ```crystal require "kilt" @@ -70,7 +72,7 @@ module MyEngine 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 @@ -90,3 +92,4 @@ Please contribute your own "adapter" if you create a template language for Cryst ## Contributors - [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer +- [waterlink](https://github.com/waterlink) Oleksii Fedorov \ No newline at end of file diff --git a/spec/kilt_spec.cr b/spec/kilt_spec.cr index 98f4d73..bce3d6f 100644 --- a/spec/kilt_spec.cr +++ b/spec/kilt_spec.cr @@ -19,7 +19,7 @@ describe Kilt do end 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") } end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 78f6f85..1c6ca4e 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -2,7 +2,7 @@ require "spec" require "../src/kilt" require "slang" -Kilt.register_template("slang", embed_slang) +Kilt.register_engine("slang", embed_slang) macro render_file(filename) String.build do |__io__| diff --git a/src/kilt.cr b/src/kilt.cr index b33c225..6c05e45 100644 --- a/src/kilt.cr +++ b/src/kilt.cr @@ -3,27 +3,33 @@ require "./kilt/*" module Kilt # macro only constant - TEMPLATES = {} of String => Int32 + ENGINES = {} of String => Int32 - macro register_template(ext, embed_macro) - {% ::Kilt::TEMPLATES[ext] = embed_macro %} + macro register_engine(ext, embed_macro) + {% Kilt::ENGINES[ext] = embed_macro.id %} end - macro embed(filename, io_name = "__kilt_io__") + macro embed(filename, io_name = "__kilt_io__", *args) {% ext = filename.split(".").last %} - {% if ::Kilt::TEMPLATES[ext] %} - {{::Kilt::TEMPLATES[ext]}}({{filename}}, {{io_name}}) + {% if Kilt::ENGINES[ext] %} + {{Kilt::ENGINES[ext]}}({{filename}}, {{io_name}}, {{*args}}) {% else %} - raise Kilt::Exception.new("Unsupported template type \"" + {{ext}} + "\"") + raise Kilt::Exception.new("Unsupported template engine for extension: \"" + {{ext}} + "\"") {% 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}}) - Kilt.embed({{filename}}, {{io_name}}) + Kilt.embed({{filename}}, {{io_name}}, {{*args}}) end end end -::Kilt.register_template("ecr", embed_ecr) +Kilt.register_engine("ecr", embed_ecr) diff --git a/src/kilt/version.cr b/src/kilt/version.cr index 6857872..b4d3aec 100644 --- a/src/kilt/version.cr +++ b/src/kilt/version.cr @@ -1,3 +1,3 @@ module Kilt - VERSION = "0.1.1" + VERSION = "0.2.0" end