Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

127 lines (92 sloc) 3.581 kb

Plate DSL

Any .rb files located in the lib/ directory will be automatically loaded and interpretaed with the Plate DSL.

The following options are available:

Callbacks

Callbacks can be used to perform custom actions during the site build process.

The callbacks currently available are:

  • Site - before_render, after_render
  • Page/Post - before_render, after_render, before_write, after_write
  • Asset - before_render, after_render, before_write, after_write

To register a callback, create a new Ruby file in the lib directory. Within that file, use the callback DSL method:

callback :site, :after_render do |site|
   puts "the site finished rendering!"
end

This can be used to write custom pages to your site throughout the render process. For example, you could write out a list of all tags used on your site when the site finishes loading:

Callback file for lib/tags_list.rb:

callback :site, :after_render do |site|
  site.log("Creating tag pages...")

  site.tags.each do |tag|
    page = DynamicPage.new(site, "/tags/#{site.to_url(tag)}/index.html", :layout => 'tag')
    page.title = "Posts tagged “#{tag}”"
    page.locals = { :tag => tag }
    page.write!
  end
end

And here's the layout for layouts/tag.erb:

---
layout: default

<h1>Posts tagged &#8220;<%= tag %>&#8221;</h1>

<ul>
  <% posts.tags[tag].reverse.each do |post| %>
    <li>
      <a href="<%= post.path %>"><%= post.title %></a>
      <time datetime="<%= post.date.strftime('%Y-%m-%d') %>" pubdate="pubdate"><%= post.date.strftime('%B %-d, %Y') %></time>
    </li>
  <% end %>
</ul>

Template Engines

To register a new templating engine based on a Tilt Template, use the register_template_engine method:

# Assuming the 'rdoc' gem is installed.
require 'rdoc'
register_template_engine :rdoc, Tilt::RDocTemplate

After registering a template engine, any file ending with that extension will automatically be processed with the given class.

Asset Engines

To register a new asset template engine based on a Tilt Template, use the register_asset_engine method:

# Assuming the 'sprockets' and 'eco' gems are installed.
require 'sprockets'
require 'eco'
register_asset_engine :eco, Sprockets::EcoTemplate

After registering an asset engine, any asset file ending with that extension will automatically be processed with the given class.

Dynamic Pages

Using the write command, dynamic pages can be created and written to the destination directory.

write '/path-to-write' do
  title   "Page Title"
  content "<p>My dynamic page content.</p>"
  layout  "default"  
end

When a site instance is not passed into the write command, the write will happen after the site is rendered. If a site instance is passed, for example within another callback, that site is used.

For example, given a layout named tag that displays all posts for a tag, the following callback would write a summary for each tag:

lib/tags.rb

callback :site, :after_render do |site|
  site.tags.each do |tag|
    write "/tags/#{site.to_url(tag)}", site do
      layout  "tag"
      locals  :tag => tag
    end
  end
end

layouts/tags.html.erb

---
layout: default

<h1>Posts tagged with <%= tag %></h1>

<ul>
  <% site.posts.tags[tag].each do |post| %>
    <li><a href="<%= post.path %>"><%= post.title %></a></li>
  <% end %>  
</ul>
Jump to Line
Something went wrong with that request. Please try again.