Content Tag (ERB)
Basic Syntax
The content tag is the main way to access content from a Template. In addition to being able to fetch every piece of content of a given Model simply, there are also a variety of options that allow you to hone your query to any given set of objects. Every instance of the content tag returns an array of objects that you can iterate over using the standard Ruby iterators, like each:
<% content(:model => :article).each do |article| -%> <h1><%= article.title %></h1> <% end -%>
This also means that you can assign the output to a variable to be used later in your template:
<% articles = content(:model => :article) -%>
Content Tag Options
:order_by— orders the list by a given field name.:order— can be either ‘asc’ or ‘desc’, for ascending or descending order.:limit— limits the results to a list of NUMBER size.:offset— gives the list offset by NUMBER items.:find— returns the set of items specified by the given QUERY. For the query syntax, see the User Manual section on [Search Language].
Paging
The Content tag allows you page content by passing in the :page_size and :page_num options.
<% content(:model => :article, :page_size => 15, :page_num => 1).each do |article| -%> <h1><%= article.title %></h1> <% end -%>
This will yield results 1-15 of your query. By changing :page_num to 2, the results would be offset by 15 (the :page_size), which would yield results 16-30.
It is recommended to set :page_num to a variable that is passed in the URL. For example, if there was a :pg variable in the slug or querystring:
<% content(:model => :article, :page_size => 15, :page_num => @params.pg).each do |article| -%>
If the :page_num is blank, and :page_size is set, :page_num will default to 1.
Building paging links
There are helpers available to help build paging links in ERB templates.
content_count— The number of results returned by the content query.content_page_num— The current:page_num.content_page_size— The current:page_size.content_first_page?— Returns true if thecontent_page_numis the first page.content_last_page?— Returns true if thecontent_page_numis the last page.content_previous_page— Returns the previous page number. If the previous page is less than 1 or greater than thecontent_page_count, returns the second to last page.content_next_page— Returns the next page number. If the next page is greater thancontent_page_count, returns the last page number.
These helpers are available both inside and outside the content block. If you have multiple content blocks on your template, the paging values from the first content block will be overwritten by the second content block, so use these straight after your content tag if there are multiple.
Example Code: Setting up paging in an ERB template
<% content(:model => :article, :page_size => 15, :page_num => params[:pg]).each do |article| -%>
<h1><%= article.title %></h1>
<% end -%>
<p><%= content_count %> results found, showing page <%= content_page_num %> of <%= content_page_count %></p>
<div id="paging">
<% if not content_first_page? -%>
<%= link_to "Previous", "?pg=#{content_previous_page}" %>
<% end -%>
<% if not content_last_page? -%>
<%= link_to "Next", "?pg=#{content_next_page}" %>
<% end -%>
</div>