All Projects → evilmartians → Liquor

evilmartians / Liquor

Licence: other
Liquor is a safe sandboxing compiling template language for Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Liquor

Westwind.razorhosting
Hosting the Razor Runtime outside of ASP.NET/MVC for use in non-Web .NET applications.
Stars: ✭ 136 (+138.6%)
Mutual labels:  template, template-engine
Windowstemplatestudio
Windows Template Studio quickly builds a UWP app, using a wizard-based UI to turn your needs into a foundation of Windows 10 patterns and best practices.
Stars: ✭ 2,089 (+3564.91%)
Mutual labels:  template, template-engine
String template
A template engine for Rails, focusing on speed, using Ruby's String interpolation syntax
Stars: ✭ 122 (+114.04%)
Mutual labels:  template, template-engine
Tuxedo
Tuxedo is a template language for Swift.
Stars: ✭ 80 (+40.35%)
Mutual labels:  template, template-engine
Mikado
Mikado is the webs fastest template library for building user interfaces.
Stars: ✭ 323 (+466.67%)
Mutual labels:  template, template-engine
Sodajs
Light weight but powerful template engine for JavaScript
Stars: ✭ 240 (+321.05%)
Mutual labels:  template, template-engine
Pongo2
Django-syntax like template-engine for Go
Stars: ✭ 2,111 (+3603.51%)
Mutual labels:  template, template-engine
Inja
A Template Engine for Modern C++
Stars: ✭ 715 (+1154.39%)
Mutual labels:  template, template-engine
Email Templates
📫 Create, preview, and send custom email templates for Node.js. Highly configurable and supports automatic inline CSS, stylesheets, embedded images and fonts, and much more!
Stars: ✭ 3,291 (+5673.68%)
Mutual labels:  template, template-engine
Jade
Jade.go - pug template engine for Go (golang)
Stars: ✭ 251 (+340.35%)
Mutual labels:  template, template-engine
Pug
Pug template engine for PHP
Stars: ✭ 341 (+498.25%)
Mutual labels:  template, template-engine
Phug
Phug - The Pug Template Engine for PHP
Stars: ✭ 43 (-24.56%)
Mutual labels:  template, template-engine
Raygun Rails
Rails 6 application template for Raygun, the Carbon Five Rails application generator.
Stars: ✭ 48 (-15.79%)
Mutual labels:  template
Trust
Travis CI and AppVeyor template to test your Rust crate on 5 architectures and publish binary releases of it for Linux, macOS and Windows
Stars: ✭ 1,072 (+1780.7%)
Mutual labels:  template
Oa Apidoc Template
Live Demo
Stars: ✭ 47 (-17.54%)
Mutual labels:  template
Saker
The template engine for Node.js and browsers.
Stars: ✭ 46 (-19.3%)
Mutual labels:  template-engine
React Frontend Dev Portfolio
Easy to adapt and deploy React portfolio inspired with solutions found at GitHub.
Stars: ✭ 55 (-3.51%)
Mutual labels:  template
Razorlight
Template engine based on Microsoft's Razor parsing engine for .NET Core
Stars: ✭ 1,068 (+1773.68%)
Mutual labels:  template-engine
Jekyll Timeline
Timeline / Résumé Theme with Jekyll
Stars: ✭ 46 (-19.3%)
Mutual labels:  template
Bug Bounty Responses
A collection of response templates for invalid bug bounty reports.
Stars: ✭ 46 (-19.3%)
Mutual labels:  template

Liquor

Liquor is a safe and extensible templating language that compiles to Ruby. It can be thought of as a replacement of Liquid, though it does not provide a migration path.

Installation

Add this line to your application's Gemfile:

gem 'liquor'

And then execute:

$ bundle

Or install it yourself as:

$ gem install liquor

Usage

The language is described in the specification.

Compiling templates

The Liquor templates are rendered in two stages. First, you need to compile them using Liquor::Manager. Assuming templates is a hash mapping template names to contents and predefined variables, the following code demonstrates the usage of Liquor::Manager:

manager = Liquor::Manager.new

templates.each do |name, (body, predefined)|
  if manager.partial? name
    manager.register_partial name, body
  else
    manager.register_template name, body, predefined
  end
end

unless manager.compile
  manager.errors.each do |error|
    source, * = templates[error.location[:file]]
    puts error.decorate(source)
  end
end

The error.decorate call will extract the corresponding line from the source and highlight the character range that caused the error.

Rendering templates

The context is a hash that is shared between the layout and the inner template. The layout_environment and inner_environment contain the list of variables initially provided to corresponding template; it must match the value of predefined of the template at compilation time.

context ||= {}

# Production-mode code
diagnostics = Liquor::Runtime.capture_diagnostics do
  inner = manager.render(template_name, inner_environment, context)
  manager.render(layout_name, layout_environment, context.merge(_inner_template: inner))
end

# Development-mode code
Liquor::Runtime.with_fatal_deprecations do
  # idem
end

The difference between development-mode and production-mode code is that in development mode, all runtime errors are raised as Liquor::Diagnostic exceptions, and in production mode, they are returned from Liquor::Runtime.capture_diagnostics instead.

The code to perform decoration of the errors is the same as for compile-time errors.

Additionally, both development-mode and production-mode code can raise Liquor::HostError at render time, reserved for uncaught Ruby exceptions inside Liquor code.

Extending Liquor

Liquor functions are similar to Ruby functions. They should not have any side effects. To learn how to define your own Liquor functions, see lib/stdlib/builtin_functions.rb.

Liquor tags provide means for inserting arbitrary Ruby code in the compiled output. They can do almost anything, and can have side effects. To learn how to define your own Liquor tags, see lib/stdlib/builtin_tags.rb.

Both functions and tags are organized in libraries. A Liquor library looks like this:

module LiquorFoo
  include Liquor::Library

  function # ...

  tag # ...
end

The libraries should be provided to the Liquor::Manager constructor, e.g. Liquor::Manager.new(import: [LiquorFoo]).

Passing Ruby objects to Liquor code

Liquor represents primitive Liquor objects (null, booleans, integers, strings and tuples) using the corresponding primitive Ruby objects, so they can be passed as-is. Liquor does not extend core Ruby classes.

All other objects must include Liquor::External in order to be accessible from Liquor. They need to call export on module level to explicitly mark functions as accessible to Liquor. The functions always receive two arguments: the unnamed argument and the hash of keyword arguments, e.g.:

class FooExternal
  include Liquor::External

  def meth(arg, kwargs)
    # ...
  end
  export :meth

Additionally, external methods can be deprecated using deprecate :meth, date: '2014-11-11', message: 'meth is deprecated, use meth1 instead'. The date (i.e. the intended date of final removal) and message will appear in the message of the emitted Liquor::Diagnostic. The diagnostic will only be emitted when the method is actually called.

Library integrations

Liquor contains code to integrate with some popular libraries

ActiveRecord

Liquor contains built-in support for passing ActiveRecord scopes and model instances as externals. To use it, require 'liquor/dropable', then include Liquor::Dropable in the model, and then simply pass the scope or model instance to the template.

Rails

Liquor contains a Rails renderer for Liquor templates that supports layouts. To use it `require 'liquor/extensions/rails', then use the following code in your actions:

render liquor: {
         manager:     manager,
         template:    template_name,
         layout:      layout_name,
         environment: environment,
       }

See the section "Renering templates" for the meaning of the renderer arguments.

Kaminari

To use Kaminari integration, require 'liquor/extensions/kaminari'. This will monkey-patch Kaminari::PaginatableArray and allow to pass any object paginated by Kaminari to Liquor templates.

Tire

To use Tire integration, require 'liquor/extensions/tire'. This will monkey-patch Tire::Results::Collection and Tire::Search::Search and allow to pass any object generated by Tire to Liquor templates.

ThinkingSphinx

To use ThinkingSphinx integration, require 'liquor/extensions/thinking_sphinx'. This will monkey-patch ThinkingSphinx::Search and allow to pass any object generated by ThinkingSphinx to Liquor templates.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Sponsored by Evil Martians

License

Liquor is distributed under the terms of MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].