mirror of
https://gitea.invidious.io/iv-org/shard-kilt.git
synced 2024-08-15 00:43:15 +00:00
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:
commit
f4965bf66d
3 changed files with 40 additions and 9 deletions
26
README.md
26
README.md
|
@ -6,12 +6,14 @@ Generic templating interface for Crystal.
|
|||
|
||||
Simplify developers' lives by abstracting template rendering for multiple template languages.
|
||||
|
||||
## Supported
|
||||
## Supported out of the box
|
||||
|
||||
| Language | File extensions | Required libraries |
|
||||
| -------- | --------------- | ------------------ |
|
||||
| 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
|
||||
|
||||
|
@ -55,6 +57,26 @@ end
|
|||
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
|
||||
|
||||
Please contribute your own "adapter" if you create a template language for Crystal that's not yet supported here!
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
require "spec"
|
||||
require "../src/kilt"
|
||||
|
||||
require "slang"
|
||||
Kilt.register_template("slang", embed_slang)
|
||||
|
||||
macro render_file(filename)
|
||||
String.build do |__io__|
|
||||
Kilt.embed({{filename}}, "__io__")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
19
src/kilt.cr
19
src/kilt.cr
|
@ -2,14 +2,20 @@ require "ecr/macros"
|
|||
require "./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__")
|
||||
{% if filename.ends_with?(".ecr") %}
|
||||
embed_ecr({{filename}}, {{io_name}})
|
||||
{% elsif filename.ends_with?(".slang") %}
|
||||
embed_slang({{filename}}, {{io_name}})
|
||||
{% ext = filename.split(".").last %}
|
||||
|
||||
{% if ::Kilt::TEMPLATES[ext] %}
|
||||
{{::Kilt::TEMPLATES[ext]}}({{filename}}, {{io_name}})
|
||||
{% else %}
|
||||
raise Kilt::Exception.new("Unsupported template type \"" + {{filename.split(".").last}} + "\"")
|
||||
raise Kilt::Exception.new("Unsupported template type \"" + {{ext}} + "\"")
|
||||
{% end %}
|
||||
end
|
||||
|
||||
|
@ -18,5 +24,6 @@ module Kilt
|
|||
Kilt.embed({{filename}}, {{io_name}})
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
::Kilt.register_template("ecr", embed_ecr)
|
||||
|
|
Loading…
Reference in a new issue