roda-papercraft

Papercraft plugin for Roda

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
    template("hello").render
  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|
    template("greet").render(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

Contributing

I gladly accept contributions to roda-papercraft. Please feel free to open issues or PRs.