Project

semplice

0.0
No commit activity in last 3 years
No release in over 3 years
Semplice is a mote inspired template engine with Django-like syntax and inheritance.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 5.7
 Project Readme

Semplice

Gem Version

Semplice is a mote inspired template engine with Django-like syntax and inheritance.

Usage

Semplice use is quite strightforward, put your content on a template (or not) and call render.

Semplice.render('path/to/template', {
  foo: 'bar',
  baz: 'quox'
})

Or you can render directly from text:

Semplice.render_text('hello {{ world }}', {
  world: 'earthlings'
})

To simplify paths, it's possible to define a set of directories where Semplice will look for template files:

Semplice.template_dirs(['path/to/dir1', 'other/dir2', ...])

Templates

Values

Values passed through context can be rendered into the content by using {{ ... }} (automatically escapes HTML) or {! ... !} (doesn't escape the content). For instance, consider val equals to <strong>world</strong>:

Hello {{ val }} -> Hello &lt;strong&gt;world&lt;/strong&gt;
Hello {! val !} -> Hello <strong>world</strong>

There's also the syntax {- ... -} that will supress the output, this is useful for small computations, usually assignments:

{- foo = "bar" -}
Hello {{ foo }}

Code blocks

Ruby code can be injected using the {% ... %}...{% end %} syntax:

Loops:

{% val.map do |v| %}
  ...
{% end %}
{% while ... %}
  ...
{% end %}

Conditionals:

{% if val == "foo" %}
  ...
{% elsif val == "bar" %}
  ...
{% else %}
  ...
{% end %}
{% case val %}
  ...
{% when "foo" %}
  ...
{% when "bar" %}
  ...
{% else %}
  ...
{% end %}

Any ruby construct is valid here.

Comments

Comments can be added with {# ... #}, they will be ignored from the output.

Inclusion

Include other templates using the {% include ... %} tag:

{% include "path/to/other-template" %}

Content blocks

A template can define content blocks that can be overriden later when using inheritance. For example:

This content goes before the block.
{% block content %}
  This is the default content of this block.
{% end %}
This content goes after the block.

Inheritance

Templates can be extended using the {% extends ... %} syntax, content blocks can be overriden to provide new content. Take for instance this base template:

{# this is foo.html #}
<h1>{% block title %}Foo title{% end %}</h1>

Then we can extend it:

{# this is bar.html #}
{% extends "foo.html" %}
{% block title %}Bar title{% end %}

Rendering foo.html will output:

<h1>Foo title</h1>

But rendering bar.html:

<h1>Bar title</h1>

Global context

Any method defined in Semplice::GlobalContext will be available in the template context at rendering time:

module Semplice::GlobalContext
  def twice(val)
    val * 2
  end
end

Then:

10 * 2 = {{ twice(10) }}

Helpers

You can include the Helpers module to simplify access to render and render_text methods.

include Semplice::Helpers

Install

$ gem install semplice