Coder Social home page Coder Social logo

cerubis's Introduction

Cerubis Test Status

Cerubis is inspired by Liquid Markup and Mustache. I like how Mustache syntax looks, but it's logic-less. Liquid allows just enough logic, but I don't like the syntax. And so Cerubis was born.

Syntax

A block is started with {{# ... }} and closed with {{/ ... }}. Output is defined as {{ ... }}.

<header>
  <h1>{{ page.title }}</h1>
  {{#unless navigation.empty?}}
  <menu>
    {{#loop nav in navigation}}
    <li>{{ link_to page }}</li>
    {{/loop}}
  </menu>
  {{/unless}}
</header>

Standard Blocks

  • if
  • unless
  • loop

Objects in Template Context

Rendering a Cerubis template with objects in the context is easy:

template_str = <<-STR
  {{#if items.empty?}}
    <p>There are no items!</p>
  {{/if}}
STR

context = { items: [1,2,3] }

Cerubis.render(template_str, context)

You can create your own objects and add them to template context, but you need to make Cerubis aware of the methods it's allowed to call:

class Foo
  include Cerubis::Method
  cerubis_method :foo, :bar, :baz

  def foo; "Foo Method"; end
  def bar; "Bar Method"; end
  def baz; "Baz Method"; end
end

template_str = "<some-template-text>"
context      = { foo: Foo.new }

Cerubis.render(template_str, context)

Shown above, object methods are made available to Cerubis templates by indicating them with cerubis_method. The cerubis_method can have many method names passed to it and cerubis_method can be called multiple times.

Template Helpers

Template helpers are meant to add convienence methods to your code. Below you see how simple it is to add a helper and to use it in your template:

# Adding a new helper
module FooHelper
  def foo_helper(name, value)
    "My name is #{name} and my value is #{value}"
  end
end

Cerubis.register_helper :foo_helper, FooHelper

# Using the helper
content = <<-STR
  Hello {{ foo_helper 'John Doe', '12' }}
STR

template = Cerubis.render(content)
template.to_html

Testing

You can run the tests in a few different ways. First, you've got your standard ruby way:

ruby -Ilib:test test/cerubis_test.rb
ruby -Ilib:test test/all.rb

Or you can run them all with Rake:

rake test

Code Coverage

The cover_me gem has been added to the project. If you'd like to see the current coverage just run thor test:coverage.

cerubis's People

Contributors

dane avatar michaelklishin avatar

Stargazers

Raúl Barroso avatar Dane Harrigan avatar Benedikt Deicke avatar

Watchers

Dane Harrigan avatar James Cloos avatar

Forkers

benedikt

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.