Module: Roda::RodaPlugins::NamedTemplates
- Defined in:
- lib/roda/plugins/named_templates.rb
Overview
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_subdirs 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
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Class Method Summary collapse
-
.configure(app) ⇒ Object
Initialize the storage for named templates.
-
.load_dependencies(app) ⇒ Object
Depend on the render plugin.
Class Method Details
.configure(app) ⇒ Object
Initialize the storage for named templates.
59 60 61 |
# File 'lib/roda/plugins/named_templates.rb', line 59 def self.configure(app) app.opts[:named_templates] ||= {} end |
.load_dependencies(app) ⇒ Object
Depend on the render plugin
54 55 56 |
# File 'lib/roda/plugins/named_templates.rb', line 54 def self.load_dependencies(app) app.plugin :render end |