• Stars
    star
    232
  • Rank 166,833 (Top 4 %)
  • Language
    Crystal
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Slim-inspired templating language for Crystal

Slang Build Status

Lightweight, terse, templating language for Crystal.

Installation

Add this to your application's shard.yml:

dependencies:
  slang:
    github: jeromegn/slang

Usage

Preferred: use Kilt

Kilt is included as a dependency for this project. It should help integrating non-ECR template engines.

Add this to your application's shard.yml:

dependencies:
  kilt:
    github: jeromegn/kilt
require "kilt/slang"

Kilt.render("path/to/file.slang") #=> <compiled template>

Example with Kemal (includes Kilt):

require "kilt/slang"

get "/" do
  Kilt.render "path/to/file.slang"
end

Without Kilt

String.build do |str|
  Slang.embed("path/to/file.slang", "str")
end

Syntax

doctype html
html
  head
    meta name="viewport" content="width=device-width,initial-scale=1.0"
    title This is a title
    css:
      h1 {color: red;}
      p {color: green;}
    style h2 {color: blue;}
  body
    /! Visible multi-line comment
      span this is wrapped in a comment
    /[if IE]
      p Dat browser is old.
    / Invisible multi-line comment
      span this is wrapped in a comment
    h1 This is a slang file
    h2 This is blue
    input type="checkbox" checked=false
    input type="checkbox" checked=true
    input type="checkbox" checked="checked"
    span#some-id.classname
      #hello.world.world2
        - some_var = "hello world haha"
        span
          span data-some-var=some_var two-attr="fun" and a #{p("hello")}
          span
            span.deep_nested
              p
                | text inside of <p>
              = Process.pid
              | text node
              ' other text node
        span.alongside pid=Process.pid
          custom-tag#with-id pid="#{Process.pid}"
            - ["ah", "oh"].each do |s|
              span = s
    / This is an invisible comment
    - if true == true
      #amazing-div some-attr="hello"
    - else
      #not-so-amazing-div some-attr="goodbye"
    /! This is a visible comment
    script var num1 = 8*4;

    javascript:
      var num2 = 8*3;
      alert("8 * 3 + 8 * 4 = " + (num1 + num2));

Given the context:

some_var = "hello"
strings = ["ah", "oh"]

Compiles to HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>This is a title</title>
    <style>
      h1 {color: red;}
      p {color: green;}
    </style>
    <style>h2 {color: blue;}</style>
  </head>
  <body>
    <!--Visible multi-line comment
      <span>this is wrapped in a comment</span>
    -->
    <!--[if IE]>
      <p>Dat browser is old.</p>
    <![endif]-->
    <h1>This is a slang file</h1>
    <h2>This is blue</h2>
    <input type="checkbox"/>
    <input type="checkbox" checked/>
    <input type="checkbox" checked="checked"/>
    <span id="some-id" class="classname">
      <div id="hello" class="world world2">
        <span>
          <span data-some-var="hello world haha" two-attr="fun">and a hello</span>
          <span>
            <span class="deep_nested">
              <p>
                text inside of &lt;p&gt;
              </p>
              #{Process.pid}
              text node
              other text node
            </span>
          </span>
        </span>
        <span class="alongside" pid="#{Process.pid}">
          <custom-tag id="with-id" pid="#{Process.pid}">
            <span>ah</span>
            <span>oh</span>
          </custom-tag>
        </span>
      </div>
    </span>
    <div id="amazing-div" some-attr="hello"></div>
    <!--This is a visible comment-->
    <script>var num1 = 8*4;</script>
    <script>
      var num2 = 8*3;
      alert("8 * 3 + 8 * 4 = " + (num1 + num2));
    </script>
  </body>
</html>

Difference between single and double equals in Slang

  • = inserts HTML with escaped characters
  • == inserts HTML without escaping. It is needed when you have already rendered HTML and you need to insert it to your layout directly.

TODO

  • Fix known limitations
  • More tests
  • Website
  • Documentation

Contributing

  1. Fork it ( https://github.com/jeromegn/slang/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

More Repositories

1

Backbone.localStorage

A localStorage adapter for Backbone.js
JavaScript
1,899
star
2

DocumentUp

Pretty documentation generator for Github projects with proper Readme.
Ruby
886
star
3

kilt

Generic template interface for Crystal
Crystal
148
star
4

localtodos

Simple Todos App built on Backbone.js
JavaScript
99
star
5

protobuf.cr

Protobuf generator, encoder and decoder.
Crystal
95
star
6

poutine

MongoDB ORM for Node.js
CoffeeScript
35
star
7

docker.cr

Docker API client in Crystal.
Crystal
26
star
8

influxdb.cr

InfluxDB driver for Crystal
Crystal
25
star
9

bson.cr

BSON implementation written in Crystal
Crystal
24
star
10

v8.cr

V8 bindings for Crystal
C++
16
star
11

dry-types-rails

Dry::Types railties
Ruby
11
star
12

beautifier

A safari extension which smooths fonts!
10
star
13

Warping-Shadow-CSS3

Warping Shadows can be done in CSS3
Ruby
8
star
14

mongol

Light modeling for MongoDB.
JavaScript
8
star
15

jeromegn.github.com

Personal site
HTML
5
star
16

rapid

Redis ORM-ish api for nodejs
JavaScript
3
star
17

backbone.pjax

Backbone utilities for web apps using PJAX.
JavaScript
3
star
18

rhai-tpl

Simple template engine using rhai for logic
Rust
2
star
19

qml.cr

QML library for Crystal
Crystal
2
star
20

quinn-plaintext

QUIC pluggable crypto to use the protocol as plaintext (for use when cryptography is already handled at another layer, e.g. Wireguard)
Rust
1
star
21

Radiophonique

Objective-C
1
star
22

containerd-rs

Rust
1
star
23

lg-ultrafine-brightness

Control LG Ultrafine brightness on linux
Rust
1
star
24

documentup.js

experimental DocumentUp running on fly.io
JavaScript
1
star
25

portfolio

My portfolio, built with StaticMatic
JavaScript
1
star
26

node-api-boilerplate

Simple Node.js API server boilerplate using CoffeeScript and Express
CoffeeScript
1
star