How to create a section template

index.html.erb

The example below shows a sidebar template for the link_section.

<dl class="sidebar">
  <dt><%= link_section.title %></dt>
  <dd>
    <% if link_section.links.any? %>
      <ul class="links">
        <% link_section.links.each do |link| %>
          <% options = link.external? || link.file? ? {:target => "_blank"} : {} %>
          <% if link.file? && link.linked.present? %>
            <li class="<%= link.linked.file_type %>">
              <%= link_to link.title.to_s + "(#{link.linked.file_type.upcase} #{number_to_human_size(link.linked.size)})", link.url, options %>
            </li>
          <% else %>
            <li>
              <%= link_to link.title, link.url, options %>
            </li>
          <% end %>
        <% end %>
      </ul>
    <% end %>
  </dd>
</dl>

A closer look

The section we're creating the template for is identified by its module namespace.

So to insert the title into the template we simply write:

<%= link_section.title %>
Hint

Have a look at vendor/skyline/app/models/sections/link_section.rb to see how the link section is defined.

Multiple items in a section

A link section has many links. And we can loop through them with:

<% if link_section.links.any? %>
  <% link_section.links.each do |link| %>

Links in Skyline can either be internal or external and an internal link can either point to a page or a file.

In our example template these properties are used to open files and external links in a new window. And the property file_type is used to show the correct icon for a linked file.

<% options = link.external? || link.file? ? {:target => "_blank"} : {} %>
<% if link.file? && link.linked.present? %>
  <li class="<%= link.linked.file_type %>">
    <%= link_to link.title.to_s + "(#{link.linked.file_type.upcase} #{number_to_human_size(link.linked.size)})", link.url, options %>
  </li>
<% else %>
  <li>
    <%= link_to link.title, link.url, options %>
  </li>
<% end %>