Weby
Weby lets you easily handle and generate HTML, XHTML or XML code in Ruby. It can be used both as a templating system and as a programmatic HTML generator. You can even mix templating and programmatic HTML, so you’re free to use it as you like.
Installation
Add this line to your application’s Gemfile:
“by gem ‘weby’
“
And then execute:
$ bundle
Or install it yourself as:
$ gem install weby
Usage
Here’s some example:
“by require ‘rubygems’ require ‘weby’
Loading a template
html = «EOS
Hello
EOS
h = HTML.parse html
puts h
“
Now let’s see some evaluation feature:
“by
num = 2 h.evaluate binding: binding puts h.to_html
“his will output:
“ml <div id=“main”>
Hello
<ul> <li> </ul>
An even number
</div>
“
Now let’s manipulate HTML elements:
“by
h.find(‘#main’).append { h2‘Title’ } h.append { div(id: ‘body’)‘World’ } h.find(‘h1’).add_class ‘header’ h.find(‘h1,h2’).add_class ‘title’ puts h.to_html
“
Output:
“ml <div id=“main”>
Hello
<ul> <li> </ul>
An even number
<h2 class="title">Title</h2>
“
We can also use some element as a template for given data:
“by li = h.find ‘ul li’ li.as_template %w(Apple Banana) puts h
“
Output:
“ml
…
- Apple
- Banana
…
“
More complex templating:
“by o = h.find ‘option’ o.as_template [#fff, content: :white, #000, content: :black]
“
Will produce:
“ml
…
…
“
“by tr = h.find ‘tr’ tr.as_template [{ id: user-1, select: {content: User 1} }, { id: user-2, select: {content: User 2} },]
“
“ml
| User 1 |
| User 2 |
“
We can also easily manipulate style and data:
“by main = h.find ‘#main’ main.style :width, ‘800px’ main.style height: ‘100%’, border: ‘1px solid’ main.data ‘pageid’, ‘1’ main.data userid: 1, username: ‘admin’
“
“ml
Maruku could not parse this XML/HTML:
<div id="main" style="width: 800px; height: 100%; border: 1px solid" data-pageid="1" data-userid="10" data-username="artix">
…
“
Contributing
- Fork it ( https://github.com/artix75/weby/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request