Git Product home page Git Product logo

liquid.cr's Introduction

liquid - Liquid template engine for Crystal

Build Status Version License

Liquid templating language: http://shopify.github.io/liquid/

Installation

Add this to your application's shard.yml:

dependencies:
  liquid:
    github: amberframework/liquid.cr

Usage

require "liquid"

txt = "
    {% if kenny.sick %}
      Kenny is sick.
    {% elsif kenny.dead %}
      You killed Kenny!  You ***!!!
    {% else %}
      Kenny looks okay --- so far
    {% endif %}
    "
ctx = Liquid::Context.new
ctx.set "kenny", Any{ "sick" => false, "dead" => true}

tpl = Liquid::Template.parse txt  # tpl can be cached and reused

result = tpl.render ctx

# result = "
#      You killed Kenny!  You ***!!!
#
#    "

Tags can be escaped:

\{% assign myvar = 15 %}

# or

{% raw %}
{% assign myvar = 15 %}
{% endraw %}

will both render {% assign myvar = 15 %}.

Blocks

Cache block (only supports caching using Redis): https://github.com/TechMagister/liquid-cache.cr

Filters

  • abs
  • append
  • at_least
  • at_most
  • capitalize
  • ceil
  • compact
  • concat
  • date
  • default
  • divided_by
  • downcase
  • escape
  • escape_once
  • first
  • floor
  • join
  • last
  • lstrip
  • map
  • minus
  • modulo
  • newline_to_br
  • plus
  • prepend
  • remove
  • remove_first
  • replace
  • replace_first
  • reverse
  • round
  • rstrip
  • size
  • slice
  • sort
  • sort_natural
  • split
  • strip
  • strip_html
  • strip_newlines
  • times
  • truncate
  • truncatewords
  • uniq
  • upcase
  • url_decode
  • url_encode
  • where

Helper Methods

  • size (for Array and String)
  • present / blank (added)
  • first / last

Development

TODO:

  • Basic For loops
  • if/elsif/else/endif
  • unless/endunless
  • Raw and comment blocks ({% raw %} and {% comment %})
  • Add variable assignment ({% assign var = "Hello World" %})
  • Add support for multiple operator (no operator precedence support (for now))
  • Add support for Array into for loop
  • Add support for Hash into for loop ({% for val in myhash %}, access as val[0] and val[1])
  • Add support for Hash into for loop ({% for key, val in myhash %}) (new)
  • Add support for Float
  • Add iteration over Arrays
  • Improve data interface
  • Add Filter support
  • Add capture block
  • Add increment block
  • Add decrement block
  • Add support for Array in expressions
  • Add support for Hash in expressions
  • Add "secret" empty Array ([]) for use in comparisons (equivalent to #blank helper method)
  • Add contains operator
  • Add cycle keyword
  • Add starts_with keyword (new)
  • Add ends_with keyword (new)
  • Add continue keyword
  • Add break keyword
  • Add case/when
  • Add syntax checking
  • Improve expression parsing
  • Add optional strict mode on Context (see below)
  • Add Everything that's missing [https://shopify.github.io/liquid/]

Context Strict Mode

NOTE: Will eventually use this to implement a strict_variables rendering flag (planning to implement strict_filters as well).

Enable at initialization:

ctx = Liquid::Context.new(:strict)

Or on an existing Context:

ctx.error_mode = :strict

Raises Liquid::InvalidExpression on missing keys or IndexError on array out of bounds errors instead of silently emitting nil.

Append ? to emit nil in strict mode (very simplistic, just checks for ? at the end of the identifier)

ctx = Liquid::Context.new(strict: true)
ctx["obj"] = { something: "something" }
{{ missing }}          -> KeyError
{{ missing? }}         -> nil
{{ obj.missing }}      -> KeyError
{{ obj.missing? }}     -> nil
{{ missing.missing? }} -> nil

Contributing

  1. Fork it ( https://github.com/amberframework/liquid.cr/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

liquid.cr's People

Contributors

anamba avatar asterite avatar bcardiff avatar damianham avatar docelic avatar exts avatar hclarsen avatar hugopl avatar icyleaf avatar luckschulze avatar setanta avatar shootingfly avatar

Forkers

setanta

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.