module Roda::RodaPlugins::NamedTemplates
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
Public Class Methods
Source
# File lib/roda/plugins/named_templates.rb, line 59 def self.configure(app) app.opts[:named_templates] ||= {} end
Initialize the storage for named templates.
Source
# File lib/roda/plugins/named_templates.rb, line 54 def self.load_dependencies(app) app.plugin :render end
Depend on the render plugin