= Application Extensions and Helpers (padrino-helpers)
=== Overview
This component provides a great deal of view helpers related to html markup generation.
There are helpers for generating tags, forms, links, images, and more. Most of the basic
methods should be very familiar to anyone who has used rails view helpers.
=== Output Helpers
Output helpers are a collection of important methods for managing, capturing and displaying output
in various ways and is used frequently to support higher-level helper functions. There are
three output helpers worth mentioning: content_for, capture_html, and concat_content
The content_for functionality supports capturing content and then rendering this into a different place
such as within a layout. One such popular example is including assets onto the layout from a template:
app/views/site/index.erb
...
<% content_for :assets do %>
<%= stylesheet_link_tag 'index', 'custom' %>
<% end %>
...
Added to a template, this will capture the includes from the block and allow them to be yielded into the layout:
app/views/layout.erb
...
Example
<%= stylesheet_link_tag 'style' %>
<%= yield_content :assets %>
...
This will automatically insert the contents of the block (in this case a stylesheet include) into the
location the content is yielded within the layout. You can also check if content exists for a block using
content_for?(true) which returns true if content exists.
The capture_html and the concat_content methods allow content to be manipulated and stored for use in building
additional helpers accepting blocks or displaying information in a template. One example is the use of
these in constructing a simplified 'form_tag' helper which accepts a block.
This will capture the template body passed into the form_tag block and then append the content
to the template through the use of concat_content. Note have been built to work for both haml and erb
templates using the same syntax.
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:
Asset helpers are intended to help insert useful html onto a view template such as 'flash' notices,
hyperlinks, mail_to links, images, stylesheets and javascript. An example of their uses would be on a
simple view template:
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:
Format helpers are several useful utilities for manipulating the format of text to achieve a goal.
The four format helpers are escape_html, time_ago_in_words, and js_escape_html.
The escape_html and js_escape_html function are 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. js_escape_html is used for
passing javascript information from a js template to a javascript function.
escape_html('&') # => <hello>&<goodbye>
There is also an alias for escape_html called h for even easier usage within templates.
Format helpers also includes a number of useful text manipulation functions such as simple_format,
pluralize, word_wrap, truncate and truncate_words.
simple_format("hello\nworld") # => "
hello world
"
pluralize(2, 'person') => '2 people'
word_wrap('Once upon a time', :line_width => 8) => "Once upon\na time"
truncate("Once upon a time in a world far far away", :length => 8) => "Once upon..."
truncate_words("Once upon a time in a world far far away", :length => 4) => "Once upon a time..."
These helpers can be invoked from any route or view within your application.
This component provides a number of rendering helpers making the process of displaying templates a bit easier.
This plugin also has support for useful additions such as partials (with support for :collection) for the templating system.
Using render plugin helpers is extremely simple. If you want to render an erb template in your view path:
render :erb, 'path/to/erb/template'
or using haml templates works just as well:
render :haml, 'path/to/haml/template'
There is also a method which renders the first view matching the path and removes the need to define an engine:
render 'path/to/any/template'
Finally, we have the all-important partials support for rendering mini-templates onto a page:
Copyright (c) 2011-2015 Padrino. See LICENSE for details.
FAQs
Unknown package
We found that padrino-helpers demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 4 open source maintainers collaborating on the project.
Package last updated on 09 Aug 2024
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
By Kirill Boychenko, Peter van der Zee - Jan 29, 2025