Merge pull request #1 from waterlink/add-ability-to-register-template-outside-of-the-library

Add ability to register template engine outside the library
This commit is contained in:
Jerome Gravel-Niquet 2016-02-14 22:06:17 -05:00
commit f4965bf66d
3 changed files with 40 additions and 9 deletions

View file

@ -6,12 +6,14 @@ Generic templating interface for Crystal.
Simplify developers' lives by abstracting template rendering for multiple template languages. Simplify developers' lives by abstracting template rendering for multiple template languages.
## Supported ## Supported out of the box
| Language | File extensions | Required libraries | | Language | File extensions | Required libraries |
| -------- | --------------- | ------------------ | | -------- | --------------- | ------------------ |
| ECR | .ecr | none (part of the stdlib) | | ECR | .ecr | none (part of the stdlib) |
| Slang | .slang | [slang](https://github.com/jeromegn/slang) |
See also:
[Registering your own template engine](#registering-your-own-template-engine).
## Installation ## Installation
@ -55,6 +57,26 @@ end
puts str # => <compiled template> puts str # => <compiled template>
``` ```
## Registering your own template engine
Use `Kilt.register_template(extension, embed_command)` macro:
```crystal
require "kilt"
module MyEngine
macro embed(filename, io_name)
# ....
end
end
Kilt.register_template("myeng", ::MyEngine.embed)
```
This can be part of your own `my-engine` library: in this case it should depend
on `kilt` directly, or this could be a part of adapter library, like:
`kilt-my-engine`, which will depend on both `kilt` and `my-engine`.
## Contributing ## Contributing
Please contribute your own "adapter" if you create a template language for Crystal that's not yet supported here! Please contribute your own "adapter" if you create a template language for Crystal that's not yet supported here!

View file

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

View file

@ -2,14 +2,20 @@ require "ecr/macros"
require "./kilt/*" require "./kilt/*"
module Kilt module Kilt
# macro only constant
TEMPLATES = {} of String => Int32
macro register_template(ext, embed_macro)
{% ::Kilt::TEMPLATES[ext] = embed_macro %}
end
macro embed(filename, io_name = "__kilt_io__") macro embed(filename, io_name = "__kilt_io__")
{% if filename.ends_with?(".ecr") %} {% ext = filename.split(".").last %}
embed_ecr({{filename}}, {{io_name}})
{% elsif filename.ends_with?(".slang") %} {% if ::Kilt::TEMPLATES[ext] %}
embed_slang({{filename}}, {{io_name}}) {{::Kilt::TEMPLATES[ext]}}({{filename}}, {{io_name}})
{% else %} {% else %}
raise Kilt::Exception.new("Unsupported template type \"" + {{filename.split(".").last}} + "\"") raise Kilt::Exception.new("Unsupported template type \"" + {{ext}} + "\"")
{% end %} {% end %}
end end
@ -18,5 +24,6 @@ module Kilt
Kilt.embed({{filename}}, {{io_name}}) Kilt.embed({{filename}}, {{io_name}})
end end
end end
end end
::Kilt.register_template("ecr", embed_ecr)