The AnyView gem provides helpers for common web related view rendering. The AnyView gem is a collection of mixins that work together to provide tags, forms asset paths and more. AnyView is based heavily on the Padrino framework helpers module.
The requirements of AnyView are kept very small. There’s two required methods inside your view context.
capture_content(*args, &block)
and
concat_content(string, opts ={})
By implementing these two methods on your view context, you’re able to get just about all the view helpers in anyview including form_for helpers.
== Installation
$ sudo gem install any_view
This will install the required gems. Next, simply include the AnyView module into your view context
class ViewContext
include AnyView
end
If you’re using Tilt, then AnyView can provide the capture_content and concat_content helpers too. These will work for erubis, erb and haml templates.
class ViewContext
include AnyView::TiltBase
include AnyView
end
A full setup for a custom class, including rendering the tilt templates might look like this:
require ‘dirge’ # nice relative paths with ~
class Renderer
def self.path
@path ||= File.expand_path(~‘./views’)
end
def self.template(name)
@templates ||= {}
@templates[name] ||= Tilt.new(File.join(path, name))
@templates[name]
end
def template(name)
self.class.template(name)
end
def render(name, locals = {})
template(name).render(ViewContext.new, locals)
end
end
Usage would be as simple as
Renderer.new.render(“my_template.erb”)
== Usage
=== Tag Helpers
Tag helpers are the basic building blocks used to construct html ‘tags’ within a view template. There
are three major functions for this category: tag, content_tag and input_tag.
The tag and content_tag are for building arbitrary html tags with a name and specified options. If
the tag contains ‘content’ within then content_tag is used. For example:
tag(:br, :style => ‘clear:both’) =>
content_tag(:p, “demo”, :class => ‘light’) =>
demo
The input_tag is used to build tags that are related to accepting input from the user:
Asset helpers are intended to help insert useful html onto a view template such as stylesheet link tags ,
hyperlinks, mail_to links, images, stylesheets and javascript. An example of their uses would be on a
simple view template:
Returns an html script tag for each of the sources provided.
javascript_include_tag ‘application’, ‘special’
To make use of these in a situation where you’re application is in a mounted url space, you should include a method +uri_root+ in your view_context. This allows you to put a uri prifix on the “/stylesheets/my_style.css” relative url
=== Form Helpers
Form helpers are the ‘standard’ form tag helpers you would come to expect when building forms. A simple
example of constructing a non-object form would be:
form_tag ‘/destroy’, :class => ‘destroy-form’, :method => :delete do
You can also easily build your own FormBuilder which allows for customized fields and behavior:
class MyCustomFormBuilder < AbstractFormBuilder
# Here we have access to a number of useful variables
#
# * view_context (use this to invoke any helpers)(ex. view_context.hidden_field_tag(…))
# * object (the record for this form) (ex. object.valid?)
# * object_name (object’s underscored type) (ex. object_name => ‘admin_user’)
#
# We also have access to self.field_types => [:text_field, :text_area, …]
# In addition, we have access to all the existing field tag helpers (text_field, hidden_field, file_field, …)
end
Once a custom builder is defined, any call to form_for can use the new builder:
Format helpers are several useful utilities for manipulating the format of text to achieve a goal.
The format helper escape_html is also aliased as h and sanitize_html
The escape_html function is for taking an html string and escaping certain characters. escape_html will escape ampersands, brackets and quotes to their HTML/XML entities. This is useful
to sanitize user content before displaying this on a template.
escape_html(‘&’) # => <hello>&<goodbye>
Format helpers also includes a number of useful text manipulation functions such as simple_format, and truncate.
simple_format(“hello\nworld”) # => “
hello world
”
truncate(“Once upon a time in a world far far away”, :length => 8) => “Once upon…”
The list of defined helpers in the ‘format helpers’ category:
simple_format(text, html_options)
Returns text transformed into HTML using simple formatting rules.
simple_format(“hello\nworld”) => “
hello world
”
truncate(text, *args)
Truncates a given text after a given :length if text is longer than :length (defaults to 30).
truncate(“Once upon a time in a world far far away”, :length => 8) => “Once upon…”
escape_html (alias h and h!)
(from RackUtils) Escape ampersands, brackets and quotes to their HTML/XML entities.