Undies

A pure-Ruby DSL for writing HTML or plain text templates. Named for its gratuitous use of the underscore.

Installation

$ gem install undies

Usage

Empty tag:

_br # => "<br />"

Tag with content:

_h1 {
  _ "Some Header"
} # => "<h1>Some Header</h1>"

Nested tags:

_body {
  _div {
    _ "Some Content"
  }
} # => "<body><div>Some Content</div></body>"

Buffer escaped output:

_ "this will be escaped & and added to the buffer"
# => "this will be escaped &amp; added to the buffer"

Buffer un-escaped output:

__ "this will <em>not</em> be escaped"
# => "this will <em>not</em> be escaped"

Tag with attributes

_h1(:class => 'title', :title => "A Header")
# => "<h1 class=\"title\" title=\"A Header\" />"

Tag with id attribute

_h1.header!
# => "<h1 id=\"header\" />"

Tag with class attributes

_h1.header.awesome
# => "<h1 class=\"header awesome\" />"

Rendering Templates

  • basic render:

    # render a block of markup
    Undies::Template.new do
      _div {
        _ "Some Content"
      }
    end.to_s
    
  • streaming render

    # render the template and push it to the output stream as it is being constructed
    # just pass an IO to the template constructor
    Undies::Template.new(output_io).new do
      _div {
        _ "Some Content"
      }
    end
    
  • render passing local vars in and pretty printing:

    # render a file with markup
    # make the local vars 'four' and 'name' available the the template markup
    # pretty print the output with 2-space indentation
    Undies::Template.new("path/to/file", {
      :four => 2 + 2,
      :name => "Awesome"
    }).to_s(2)
    

Pretty Printing

If you want to pretty print the output, pass an integer specifying the number of spaces to indent to the ‘to_s` method.

License

Copyright © 2011 Kelly Redding

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.