0.0
No release in over 3 years
roda-papercraft: Papercraft views for Roda
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 5.25.5

Runtime

~> 2.17
>= 0
 Project Readme

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.