2016-02-14 18:30:01 +00:00
|
|
|
|
# Kilt [![Build Status](https://travis-ci.org/jeromegn/kilt.svg?branch=master)](https://travis-ci.org/jeromegn/kilt) [![Dependency Status](https://shards.rocks/badge/github/jeromegn/kilt/status.svg)](https://shards.rocks/github/jeromegn/kilt) [![devDependency Status](https://shards.rocks/badge/github/jeromegn/kilt/dev_status.svg)](https://shards.rocks/github/jeromegn/kilt)
|
2016-02-14 18:05:54 +00:00
|
|
|
|
|
|
|
|
|
Generic templating interface for Crystal.
|
|
|
|
|
|
|
|
|
|
## Goal
|
|
|
|
|
|
|
|
|
|
Simplify developers' lives by abstracting template rendering for multiple template languages.
|
|
|
|
|
|
2016-02-14 21:17:31 +00:00
|
|
|
|
## Supported out of the box
|
2016-02-14 18:05:54 +00:00
|
|
|
|
|
2016-02-15 04:18:42 +00:00
|
|
|
|
| Language | File extensions | Required libraries | Maintainer |
|
|
|
|
|
| -------- | --------------- | ------------------ | ---------- |
|
|
|
|
|
| ECR | .ecr | none (part of the stdlib) | |
|
|
|
|
|
| Mustache | .mustache | [crustache](https://github.com/MakeNowJust/crustache) | [@MakeNowJust](https://github.com/MakeNowJust) |
|
2016-02-15 16:50:18 +00:00
|
|
|
|
| Slang | .slang | [slang](https://github.com/jeromegn/slang) | [@jeromegn](https://github.com/jeromegn) |
|
2016-02-16 08:55:13 +00:00
|
|
|
|
| Temel | .temel | [temel](https://github.com/f/temel) | [@f](https://github.com/f) |
|
2017-04-04 19:34:28 +00:00
|
|
|
|
| Crikey | .crikey | [crikey](https://github.com/domgetter/crikey) | [@domgetter](https://github.com/domgetter) |
|
2019-04-23 13:14:54 +00:00
|
|
|
|
| Liquid | .liquid | [liquid](https://github.com/TechMagister/liquid.cr) | [@docelic](https://github.com/docelic) |
|
2016-02-14 21:17:31 +00:00
|
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
[Registering your own template engine](#registering-your-own-template-engine).
|
2016-02-14 18:05:54 +00:00
|
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
|
|
Add this to your application's `shard.yml`:
|
|
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
|
dependencies:
|
|
|
|
|
kilt:
|
|
|
|
|
github: jeromegn/kilt
|
2016-02-16 08:55:13 +00:00
|
|
|
|
|
2016-02-14 18:05:54 +00:00
|
|
|
|
# Any other template languages Crystal shard
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
2016-02-14 18:26:50 +00:00
|
|
|
|
- Kilt essentially adds two macros `Kilt.embed` and `Kilt.file`, the code is really simple.
|
|
|
|
|
- Add template language dependencies, as listed in the support table above.
|
|
|
|
|
|
|
|
|
|
Both macros take a `filename` and a `io_name` (the latter defaults to `"__kilt_io__"`)
|
|
|
|
|
|
|
|
|
|
### Example
|
2016-02-14 18:05:54 +00:00
|
|
|
|
|
|
|
|
|
```crystal
|
|
|
|
|
require "kilt"
|
2016-02-15 16:50:18 +00:00
|
|
|
|
|
2016-02-15 19:35:04 +00:00
|
|
|
|
# For slang, add:
|
2016-02-15 16:50:18 +00:00
|
|
|
|
require "kilt/slang"
|
2016-02-14 18:05:54 +00:00
|
|
|
|
|
|
|
|
|
# With a Class
|
|
|
|
|
|
|
|
|
|
class YourView
|
|
|
|
|
Kilt.file("path/to/template.ecr") # Adds a to_s method
|
|
|
|
|
end
|
|
|
|
|
puts YourView.new.to_s # => <compiled template>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Embedded
|
|
|
|
|
|
2016-02-15 16:50:18 +00:00
|
|
|
|
str = Kilt.render "path/to/template.slang"
|
|
|
|
|
|
|
|
|
|
# or
|
|
|
|
|
|
2016-02-14 18:26:50 +00:00
|
|
|
|
str = String.build do |__kilt_io__|
|
|
|
|
|
Kilt.embed "path/to/template.slang"
|
2016-02-14 18:05:54 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
puts str # => <compiled template>
|
|
|
|
|
```
|
|
|
|
|
|
2016-02-14 21:17:31 +00:00
|
|
|
|
## Registering your own template engine
|
|
|
|
|
|
2016-02-15 03:22:35 +00:00
|
|
|
|
Use `Kilt.register_engine(extension, embed_command)` macro:
|
2016-02-14 21:17:31 +00:00
|
|
|
|
|
|
|
|
|
```crystal
|
|
|
|
|
require "kilt"
|
|
|
|
|
|
|
|
|
|
module MyEngine
|
|
|
|
|
macro embed(filename, io_name)
|
|
|
|
|
# ....
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-15 16:50:18 +00:00
|
|
|
|
Kilt.register_engine("myeng", MyEngine.embed)
|
2016-02-14 21:17:31 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
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`.
|
|
|
|
|
|
2016-02-14 18:05:54 +00:00
|
|
|
|
## Contributing
|
|
|
|
|
|
|
|
|
|
Please contribute your own "adapter" if you create a template language for Crystal that's not yet supported here!
|
|
|
|
|
|
|
|
|
|
1. Fork it ( https://github.com/jeromegn/kilt/fork )
|
|
|
|
|
2. Create your feature branch (git checkout -b my-awesome-template-language)
|
|
|
|
|
3. Commit your changes (git commit -am 'Add my-awesome-template-language')
|
|
|
|
|
4. Push to the branch (git push origin my-awesome-template-language)
|
|
|
|
|
5. Create a new Pull Request
|
|
|
|
|
|
|
|
|
|
## Contributors
|
|
|
|
|
|
|
|
|
|
- [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer
|
2016-02-15 04:18:42 +00:00
|
|
|
|
- [waterlink](https://github.com/waterlink) Oleksii Fedorov
|
2016-02-15 19:35:04 +00:00
|
|
|
|
- [MakeNowJust](https://github.com/MakeNowJust) TSUYUSATO Kitsune
|
2016-02-16 08:55:13 +00:00
|
|
|
|
- [f](https://github.com/f) Fatih Kadir Akın
|