Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Feb 14, 2016
0 parents commit f31dad1
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
/doc/
/libs/
/.crystal/
/.shards/


# Libraries don't need dependency lock
# Dependencies will be locked in application that uses them
/shard.lock

1 change: 1 addition & 0 deletions .travis.yml
@@ -0,0 +1 @@
language: crystal
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Jerome Gravel-Niquet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
65 changes: 65 additions & 0 deletions README.md
@@ -0,0 +1,65 @@
# Kilt

Generic templating interface for Crystal.

## Goal

Simplify developers' lives by abstracting template rendering for multiple template languages.

## Supported

| Language | File extensions | Required libraries |
| -------- | --------------- | ------------------ |
| ECR | .ecr | none |
| Slang | .slang | [slang](https://github.com/jeromegn/slang) |

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
kilt:
github: jeromegn/kilt

# Any other template languages Crystal shard
```

## Usage

Add template language dependencies, as listed in the support table above.

```crystal
require "kilt"
require "slang" # if you want to use Slang templates, for instance
# 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
str = String.build do |str|
Kilt.embed "path/to/template.slang", "str"
end
puts str # => <compiled template>
```

## 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
11 changes: 11 additions & 0 deletions shard.yml
@@ -0,0 +1,11 @@
name: kilt
version: 0.1.0

authors:
- Jerome Gravel-Niquet <jeromegn@gmail.com>

license: MIT

development_dependencies:
slang:
github: jeromegn/slang
1 change: 1 addition & 0 deletions spec/fixtures/test.ecr
@@ -0,0 +1 @@
<span><%= Process.pid %></span>
1 change: 1 addition & 0 deletions spec/fixtures/test.slang
@@ -0,0 +1 @@
span = Process.pid
27 changes: 27 additions & 0 deletions spec/kilt_spec.cr
@@ -0,0 +1,27 @@
require "./spec_helper"

class View
Kilt.file "spec/fixtures/test.ecr"
end

describe Kilt do

it "renders ecr" do
render_file("spec/fixtures/test.ecr").should eq("<span>#{Process.pid}</span>")
end

it "renders slang" do
render_file("spec/fixtures/test.slang").should eq("<span>#{Process.pid}</span>")
end

it "works with classes" do
View.new.to_s.should eq("<span>#{Process.pid}</span>")
end

it "raises with unsupported filetype" do
expect_raises(Kilt::Exception, "Unsupported template type \"abc\"") {
render_file("test.abc")
}
end

end
9 changes: 9 additions & 0 deletions spec/spec_helper.cr
@@ -0,0 +1,9 @@
require "spec"
require "slang"
require "../src/kilt"

macro render_file(filename)
String.build do |__io__|
Kilt.embed({{filename}}, "__io__")
end
end
22 changes: 22 additions & 0 deletions src/kilt.cr
@@ -0,0 +1,22 @@
require "ecr/macros"
require "./kilt/*"

module Kilt

macro embed(filename, io_name = "__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 %}
end

macro file(filename)
def to_s(__io__)
Kilt.embed({{filename}}, "__io__")
end
end

end
5 changes: 5 additions & 0 deletions src/kilt/exception.cr
@@ -0,0 +1,5 @@
module Kilt
class Exception < ::Exception
# Nothing special
end
end
3 changes: 3 additions & 0 deletions src/kilt/version.cr
@@ -0,0 +1,3 @@
module Kilt
VERSION = "0.1.0"
end

0 comments on commit f31dad1

Please sign in to comment.