mirror of
https://gitea.invidious.io/iv-org/shard-kilt.git
synced 2024-08-15 00:43:15 +00:00
Add ability to register template engine outside the library
This commit is contained in:
parent
5f23a69c15
commit
ee7059effd
3 changed files with 72 additions and 10 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
|
||||
|
|
52
src/kilt.cr
52
src/kilt.cr
|
@ -2,17 +2,55 @@ require "ecr/macros"
|
|||
require "./kilt/*"
|
||||
|
||||
module Kilt
|
||||
# macro only constants
|
||||
TEMPLATES = [] of Int32
|
||||
OVERRIDES = [] of Int32
|
||||
|
||||
macro register_template(extension, embed_macro)
|
||||
{% ::Kilt::TEMPLATES << {extension, embed_macro} %}
|
||||
::Kilt._override_embed
|
||||
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}})
|
||||
{% else %}
|
||||
raise Kilt::Exception.new("Unsupported template type \"" + {{filename.split(".").last}} + "\"")
|
||||
{% end %}
|
||||
{{::Kilt::OVERRIDES.last.id}}.embed({{filename}}, {{io_name}})
|
||||
end
|
||||
|
||||
macro _override_embed
|
||||
{% override_name = "::Kilt::Override#{OVERRIDES.size}" %}
|
||||
|
||||
module {{override_name.id}}
|
||||
macro embed(filename, io_name)
|
||||
{% start = true %}
|
||||
|
||||
{% for template in ::Kilt::TEMPLATES %}
|
||||
{% extension = template[0] %}
|
||||
{% embed_macro = template[1] %}
|
||||
|
||||
{% if start == true %}
|
||||
{% start = false %}
|
||||
|
||||
\{% if filename.ends_with?({{extension}}) %}
|
||||
{{embed_macro.id}}(\{{filename}}, \{{io_name}})
|
||||
|
||||
{% else %}
|
||||
|
||||
\{% elsif filename.ends_with?({{extension}}) %}
|
||||
{{embed_macro.id}}(\{{filename}}, \{{io_name}})
|
||||
|
||||
{% end %}
|
||||
{% end %}
|
||||
|
||||
\{% else %}
|
||||
raise Kilt::Exception.new("Unsupported template type \"" + \{{filename.split(".").last}} + "\"")
|
||||
\{% {{:end.id}} %}
|
||||
end
|
||||
end
|
||||
|
||||
{% ::Kilt::OVERRIDES << override_name %}
|
||||
end
|
||||
|
||||
register_template(".ecr", embed_ecr)
|
||||
|
||||
macro file(filename, io_name = "__kilt_io__")
|
||||
def to_s({{io_name.id}})
|
||||
Kilt.embed({{filename}}, {{io_name}})
|
||||
|
|
Loading…
Reference in a new issue