Papercraft

Introduction

Overview

Getting Started

Resources

CHANGELOG

Basic Usage

Writing Templates

Rendering Templates

HTML and XML Elements

Built-in Methods

Template Composition

Component Templates

Working with Layouts

Web Frameworks

Roll Your Own

Hanami on Papercraft

Roda on Papercraft

Advanced Topics

Deferred Rendering

Extending Papercraft

How Papercraft Works

Roda on Papercraft

The roda-papercraft Roda plugin adds some methods to let you render Papercraft templates in your Roda app. This plugin lets you either define templates inline inside your Roda router, or load templates from files. This repository includes an example directory with an example Roda app showing typical use.

To use Papercraft with Roda, do the following:

1. Add roda-papercraft to your App

In your Gemfile, add the following line:

gem "roda-papercraft"

2. Activate the Papercraft Plugin

In your Roda app, load roda-papercraft:

require "roda-papercraft"

Then load the plugin inside your app:

class App < Roda
  plugin :papercraft
  ...
end

By default, the template root is "templates" (relative to the working directory). To change the template root, you can configure the plugin:

class App < Roda
  plugin :papercraft
  ...
end

Rendering Templates from Files

Your template files are normal Ruby source files. Each file should contain a single lambda. Here’s an example:

# templates/hello.rb
-> {
  h1 "Hello from Papercraft"
}

To render the template, first load it using the #template method, and then run #render on it:

route do |r|
  r.get "hello" do
    Papercraft.html(template, "hello")
  end

  ...
end

If the template takes arguments, you can pass them through the #render call:

# templates/greet.rb
->(name) {
  h1 "Hello, #{name}!"
}

# in your app
route do |r|
  r.get "greet", String do |name|
    Papercraft.html(template("greet"), name)
  end
  ...
end

Rendering Templates Inline

To render templates inline, just use #render directly.

route do |r|
  r.get "hello" do
    render {
      h1 "Hello from Papercraft"
    }
  end
end