Module: Sinatra::Tags

Defined in:
lib/sinatra/tags.rb

Overview

Sinatra::Tags

A Sinatra Extension that provides easy creation of flexible HTML tags.

Installation

#  Add RubyGems.org (former Gemcutter) to your RubyGems sources 
$  gem sources -a http://rubygems.org

$  (sudo)? gem install sinatra-tags

Dependencies

This Gem depends upon the following:

Runtime:

Optionals:

Development & Tests:

  • sinatra-tests (>= 0.1.6)

  • rspec (>= 1.3.0 )

  • rack-test (>= 0.5.3)

  • rspec_hpricot_matchers (>= 0.1.0)

Getting Started

To start using Sinatra::Tags, just require and register the extension

…in your App…

require 'sinatra/tags'

class YourApp < Sinatra::Base
  register(Sinatra::Tags)

  <snip...>

end

…or your Extension…

require 'sinatra/tags'

module Sinatra
  module YourExtension

    <snip...>

    def self.registered(app)
      app.register Sinatra::Tags
      <snip...>
    end

  end
end

Usage

Sinatra::Tags has only one public method, with this dynamic syntax:

tag(name)
tag(name, &block)

tag(name, content)
tag(name, content, attributes)
tag(name, content, attributes, &block)

tag(name, attributes)
tag(name, attributes, &block)

This dynamic syntax provides a very flexible method as you can see in the examples below:

Self closing tags:

tag(:br)  # => 

  <br> or <br/> if XHTML

tag(:hr, :class => "space") # => 

  <hr class="space">

Multi line tags:

tag(:div) # => 

  <div></div>

tag(:div, 'content') # => 

  <div>
    content
  </div>

tag(:div, 'content', :id => 'comment') # => 

  <div id="comment">
    content
  </div>

# NB! no content
tag(:div, :id => 'comment') # => 

  <div id="comment"></div>

Single line tags:

tag(:h1,'Header') # => 

  <h1>Header</h1>

tag(:abbr, 'WHO', :title => "World Health Organization") # => 

  <abbr title="World Health Organization">WHO</abbr>

Working with blocks

tag(:div) do
  tag(:p, 'Hello World')
end
# => 

  <div>
    <p>Hello World</p>
  </div>

<% tag(:ul) do %>
  <li>item 1</li>
  <%= tag(:li, 'item 2') %>
  <li>item 3</li>
<% end %>
# => 

  <ul>
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
  </ul>

# NB! ignored tag contents if given a block
<% tag(:div, 'ignored tag-content') do  %>
  <%= tag(:label, 'Comments:', :for => :comments)  %>
  <%= tag(:textarea,'textarea contents', :id => :comments)  %>
<% end  %>
# => 

  <div>
    <label for="comments">Comments:</label>
    <textarea id="comments">
      textarea contents
    </textarea>
  </div>

Boolean attributes:

tag(:input, :type => :checkbox, :checked => true)
# => 

  <input type="checkbox" checked="checked" />

tag(:option, 'Sinatra', :value => "1" :selected => true)
# => 

  <option value="1">Sinatra</option>

tag(:option, 'PHP', :value => "0" :selected => false)
# => 

  <option value="0">PHP</option>

That’s more or less it. Try it out and you’ll see what it can do for you.

Configuration Settings

The default settings should help you get moving quickly, and are fairly common sense based.

:tags_output_format_is_xhtml

Sets the HTML output format, toggling between HTML vs XHTML. Default is: false

I prefer to output in HTML 4.0.1 Strict, but you can easily switch to XHTML by setting the value in your App or Extension:

set :tags_output_format_is_xhtml, true

…or on the fly like this

YourApp.tags_output_format_is_xhtml = true / false

self.class.tags_output_format_is_xhtml = true / false

settings.tags_output_format_is_xhtml = true / false

:tags_add_newlines_after_tags

Sets the formatting of the HTML output, whether it should be more compact in nature or slightly better layed out. Default is: true

Copyright © 2010 Kematzy

Released under the MIT License.

See LICENSE for further details.

Code Inspirations:

  • The ActiveSupport gem by DHH & Rails Core Team

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Registers these Extensions:

  • Sinatra::OutputBuffer

Default Settings:

  • :tags_output_format_is_xhtml => sets the HTML output format. Default is: false

  • :tags_add_newlines_after_tags => sets whether to add new lines to the HTML tags. Default is: true



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/sinatra/tags.rb', line 563

def self.registered(app) 
  app.register(Sinatra::OutputBuffer)
  app.helpers Sinatra::Tags::Helpers
  
  # set the HTML output formats
  app.set :tags_output_format_is_xhtml, false
  # set the HTML output formats
  app.set :tags_add_newlines_after_tags, true
  
  ## add the extension specific options to those inspectable by :settings_inspect method
  #  provided by the Sinatra::Settings extension
  if app.respond_to?(:sinatra_settings_for_inspection)
    %w( tags_add_newlines_after_tags tags_output_format_is_xhtml ).each do |m|
      app.sinatra_settings_for_inspection << m
    end
  end
  
end

.versionObject

Returns the version string for this extension

Examples

Sinatra::Tags.version  => 'Sinatra::Tags v0.1.0'


272
# File 'lib/sinatra/tags.rb', line 272

def self.version; "Sinatra::Tags v#{VERSION}"; end