Added slang adapter + specs, reorganized specs and adapters

This commit is contained in:
Jerome Gravel-Niquet 2016-02-15 11:50:18 -05:00
parent ae9e097221
commit b152bf1153
14 changed files with 62 additions and 27 deletions

View file

@ -12,6 +12,7 @@ Simplify developers' lives by abstracting template rendering for multiple templa
| -------- | --------------- | ------------------ | ---------- | | -------- | --------------- | ------------------ | ---------- |
| ECR | .ecr | none (part of the stdlib) | | | ECR | .ecr | none (part of the stdlib) | |
| Mustache | .mustache | [crustache](https://github.com/MakeNowJust/crustache) | [@MakeNowJust](https://github.com/MakeNowJust) | | Mustache | .mustache | [crustache](https://github.com/MakeNowJust/crustache) | [@MakeNowJust](https://github.com/MakeNowJust) |
| Slang | .slang | [slang](https://github.com/jeromegn/slang) | [@jeromegn](https://github.com/jeromegn) |
See also: See also:
[Registering your own template engine](#registering-your-own-template-engine). [Registering your own template engine](#registering-your-own-template-engine).
@ -39,7 +40,10 @@ Both macros take a `filename` and a `io_name` (the latter defaults to `"__kilt_i
```crystal ```crystal
require "kilt" require "kilt"
require "slang" # if you want to use Slang templates, for instance
# For slang, try:
require "slang"
require "kilt/slang"
# With a Class # With a Class
@ -51,12 +55,14 @@ puts YourView.new.to_s # => <compiled template>
# Embedded # Embedded
str = Kilt.render "path/to/template.slang"
# or
str = String.build do |__kilt_io__| str = String.build do |__kilt_io__|
Kilt.embed "path/to/template.slang" Kilt.embed "path/to/template.slang"
end end
# or `Kilt.render "path/to/template.slang"`
puts str # => <compiled template> puts str # => <compiled template>
``` ```
@ -73,7 +79,7 @@ module MyEngine
end end
end end
Kilt.register_engine("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 This can be part of your own `my-engine` library: in this case it should depend
@ -94,3 +100,4 @@ Please contribute your own "adapter" if you create a template language for Cryst
- [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer - [jeromegn](https://github.com/jeromegn) Jerome Gravel-Niquet - creator, maintainer
- [waterlink](https://github.com/waterlink) Oleksii Fedorov - [waterlink](https://github.com/waterlink) Oleksii Fedorov
- [MakeNowJust](https://github.com/MakeNowJust) TSUYUSATO Kitsune

View file

@ -7,5 +7,7 @@ authors:
license: MIT license: MIT
development_dependencies: development_dependencies:
slang:
github: jeromegn/slang
crustache: crustache:
github: MakeNowJust/crustache github: MakeNowJust/crustache

1
spec/fixtures/test.slang vendored Normal file
View file

@ -0,0 +1 @@
span = Process.pid

View file

@ -1,5 +1,5 @@
require "./spec_helper" require "../spec_helper"
require "../src/mustache" require "../../src/kilt/crustache"
class MustacheView class MustacheView
def has_key?(name) def has_key?(name)
@ -13,9 +13,9 @@ class MustacheView
Kilt.file "spec/fixtures/test.mustache", "__kilt_io__", self Kilt.file "spec/fixtures/test.mustache", "__kilt_io__", self
end end
describe Kilt do describe "kilt/crustache" do
it "renders ecr" do it "renders crustache" do
Kilt.render("spec/fixtures/test.mustache", { "pid" => Process.pid }).should eq("<span>#{Process.pid}</span>") Kilt.render("spec/fixtures/test.mustache", { "pid" => Process.pid }).should eq("<span>#{Process.pid}</span>")
end end

18
spec/kilt/slang_spec.cr Normal file
View file

@ -0,0 +1,18 @@
require "../spec_helper"
require "../../src/kilt/slang"
class SlangView
Kilt.file "spec/fixtures/test.slang"
end
describe "kilt/slang" do
it "renders slang" do
Kilt.render("spec/fixtures/test.slang").should eq("<span>#{Process.pid}</span>")
end
it "works with classes" do
SlangView.new.to_s.should eq("<span>#{Process.pid}</span>")
end
end

View file

@ -10,11 +10,6 @@ describe Kilt do
Kilt.render("spec/fixtures/test.ecr").should eq("<span>#{Process.pid}</span>") Kilt.render("spec/fixtures/test.ecr").should eq("<span>#{Process.pid}</span>")
end end
it "renders registered engine" do
Kilt.register_engine "raw", Raw.embed
Kilt.render("spec/fixtures/test.raw").should eq("Hello World!")
end
it "works with classes" do it "works with classes" do
View.new.to_s.should eq("<span>#{Process.pid}</span>") View.new.to_s.should eq("<span>#{Process.pid}</span>")
end end
@ -25,4 +20,9 @@ describe Kilt do
} }
end end
it "renders registered engine" do
Kilt.register_engine "raw", Raw.embed
Kilt.render("spec/fixtures/test.raw").should eq("Hello World!")
end
end end

View file

@ -1,8 +1,3 @@
require "spec" require "spec"
require "../src/kilt" require "../src/kilt"
require "./support/raw_engine"
module Raw
macro embed(filename, io)
{{ io.id }} << {{`cat #{filename}`.stringify}}
end
end

View file

@ -0,0 +1,5 @@
module Raw
macro embed(filename, io)
{{ io.id }} << {{`cat #{filename}`.stringify}}
end
end

View file

@ -1,5 +1,5 @@
require "ecr/macros" require "./kilt/version"
require "./kilt/*" require "./kilt/exception"
module Kilt module Kilt
# macro only constant # macro only constant
@ -32,4 +32,4 @@ module Kilt
end end
end end
Kilt.register_engine("ecr", embed_ecr) require "./kilt/ecr"

4
src/kilt/crustache.cr Normal file
View file

@ -0,0 +1,4 @@
require "../kilt"
require "crustache"
Kilt.register_engine "mustache", embed_mustache

3
src/kilt/ecr.cr Normal file
View file

@ -0,0 +1,3 @@
require "ecr/macros"
Kilt.register_engine("ecr", embed_ecr)

4
src/kilt/slang.cr Normal file
View file

@ -0,0 +1,4 @@
require "../kilt"
require "slang"
Kilt.register_engine "slang", Slang.embed

View file

@ -1,3 +1,3 @@
module Kilt module Kilt
VERSION = "0.2.1" VERSION = "0.3.0"
end end

View file

@ -1,4 +0,0 @@
require "./kilt"
require "crustache"
Kilt.register_engine "mustache", embed_mustache