Braai

A fully extensible templating system. Sick and tired of all of those templating systems that force you to do things their way? Yeah, me too! Thankfully there's Braai. Braai let's you write your own Regular Expression matchers and then do whatever you'd like when the template is parsed! Sounds fun, doesn't it?

Installation

Add this line to your application's Gemfile:

gem 'braai'

And then execute:

$ bundle

Or install it yourself as:

$ gem install braai

Usage

Built-in Matchers

Braai comes shipped with two simple matchers for you, but you can easily add your own.

The first matcher is a simple to_s matcher. It will match a single variable and then call to_s on it:

template = "Hi {{ name }}!"
response = Braai::Template.new(template).render(name: "Mark")
response.must_equal "Hi Mark!"

The second matcher will call a method on the variable.

template = "Hi {{ name.upcase }}!"
response = Braai::Template.new(template).render(name: "Mark")
response.must_equal "Hi MARK!"

Custom Matchers

Braai let's you easily define your own matchers to do whatever you would like to do.

template = "I'm {{ name }} and {{ mmm... bbq }}!"
Braai::Template.map(/({{\s*mmm\.\.\. bbq\s*}})/i) do |template, key, matches|
  "Damn, I love BBQ!"
end

Braai::Template.map(/({{\s*name\s*}})/i) do |template, key, matches|
  template.attributes[:name].upcase
end

response = Braai::Template.new(template).render(name: "mark")
response.must_equal "I'm MARK and Damn, I love BBQ!!"

Conditional Logic

Braai supports conditional logic with simple if statements (no support for comparisons yet).

template = '{{ product.name }}{{ if product.featured }}***{{ /if }}'
featured_product = OpenStruct.new(name: 'Special Product', featured: true)
regular_product = OpenStruct.new(name: 'Regular Product', featured: false)

Braai::Template.new(template).render(product: featured_product).must_equal("Special Product***")
Braai::Template.new(template).render(product: regular_product).must_equal("Regular Product")

For Loops

Braai also supports looping right out of the box.

template = <<-EOF
<h1>{{ greet }}</h1>
<ul>
  {{ for product in products }}
    <li>{{ product }}</li>
  {{ /for }}
</ul>
<div>
  {{ for food in foods }}
    <p>{{ food }}</p>
  {{ /for }}
</div>
<h2>{{greet.upcase}}</h2>
EOF

res = Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
res.should match("<h1>mark</h1>")
res.should match("<li>car</li>")
res.should match("<li>boat</li>")
res.should match("<li>truck</li>")
res.should match("<p>apple</p>")
res.should match("<p>orange</p>")
res.should match("<h2>MARK</h2>")

Contributing

  1. Fork it
  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 new Pull Request

Contributers

  • Mark Bates
  • Nick Plante
  • Sam Beam
  • Kevin Incorvia