The named_templates plugin allows you to specify templates by name, providing the template code to use for a given name:
plugin :named_templates template :layout do "<html><body><%= yield %></body></html>" end template :index do "<p>Hello <%= @user %>!</p>" end route do |r| @user = 'You' render(:index) end # => "<html><body><p>Hello You!</p></body></html>"
You can provide options for the template, for example to change the engine that the template uses:
template :index, engine: :str do "<p>Hello #{@user}!</p>" end
The block you use is reevaluted on every call, allowing you to easily include additional setup logic:
template :index do greeting = ['hello', 'hi', 'howdy'].sample @user.downcase! "<p>#{greating} <%= @user %>!</p>" end
This plugin also works with the view_options plugin, as long as you prefix the template name with the view subdirectory:
template "main/index" do "<html><body><%= yield %></body></html>" end route do |r| set_view_subdir("main") @user = 'You' render(:index) end
Classes and Modules
Public Class methods
configure(app)
Initialize the storage for named templates.
[show source]
# File lib/roda/plugins/named_templates.rb 59 def self.configure(app) 60 app.opts[:named_templates] ||= {} 61 end
load_dependencies(app)
Depend on the render plugin
[show source]
# File lib/roda/plugins/named_templates.rb 54 def self.load_dependencies(app) 55 app.plugin :render 56 end