mirror of
https://gitea.invidious.io/iv-org/shard-kilt.git
synced 2024-08-15 00:43:15 +00:00
renamed a few template -> engine, credited waterlink, added render method, bump version
This commit is contained in:
parent
f4965bf66d
commit
8fb4bd73f8
5 changed files with 24 additions and 15 deletions
|
@ -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 # => <compiled template>
|
||||
```
|
||||
|
||||
## 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
|
|
@ -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
|
||||
|
|
|
@ -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__|
|
||||
|
|
26
src/kilt.cr
26
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)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module Kilt
|
||||
VERSION = "0.1.1"
|
||||
VERSION = "0.2.0"
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue