https://github.com/markbates/braai
Fully Extensible Templates
https://github.com/markbates/braai
Last synced: 12 months ago
JSON representation
Fully Extensible Templates
- Host: GitHub
- URL: https://github.com/markbates/braai
- Owner: markbates
- License: mit
- Created: 2012-08-28T20:11:42.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2013-03-12T14:16:02.000Z (over 13 years ago)
- Last Synced: 2025-02-07T18:51:34.354Z (over 1 year ago)
- Language: Ruby
- Size: 212 KB
- Stars: 3
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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:
```ruby
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.
```ruby
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.
```ruby
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).
```ruby
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.
```ruby
template = <<-EOF
{{ greet }}
- {{ product }}
{{ for product in products }}
{{ /for }}
{{ for food in foods }}
{{ food }}
{{ /for }}
{{greet.upcase}}
EOF
res = Braai::Template.new(template).render(greet: "mark", products: %w{car boat truck}, foods: %w{apple orange})
res.should match("
mark
")res.should match("
res.should match("
res.should match("
res.should match("
apple
")res.should match("
orange
")res.should match("
MARK
")```
## 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