Module: Goliath::Rack::Templates
- Defined in:
- lib/goliath/rack/templates.rb
Overview
Template rendering methods. Each method takes t as a Symbol (for template file lookup) or as a String (to render directly), as well as an optional hashes giving additional options and local variables. It returns a String with the rendered output.
This is mostly similar to the code from Sinatra (indeed, it’s stolen from there). It does not compile or cache templates, and the find_template method is simpler.
Instance Method Summary collapse
-
#builder(template = nil, options = {}, locals = {}) { ... } ⇒ String
Render a builder template.
-
#coffee(template, options = {}, locals = {}) ⇒ String
Render a coffee template.
-
#erb(template, options = {}, locals = {}) ⇒ String
Render an erb template.
-
#erubis(template, options = {}, locals = {}) ⇒ String
Render an erubis template.
-
#find_template(views, name, engine) ⇒ String | nil
Finds template file with same name as extension.
-
#haml(template, options = {}, locals = {}) ⇒ String
Render a haml template.
-
#less(template, options = {}, locals = {}) ⇒ String
Render a less template.
-
#liquid(template, options = {}, locals = {}) ⇒ String
Render a liquid template.
-
#markaby(template = nil, options = {}, locals = {}) { ... } ⇒ String
Render a markaby template.
-
#markdown(template, options = {}, locals = {}) ⇒ String
Render a markdown template.
-
#nokogiri(template = nil, options = {}, locals = {}) { ... } ⇒ String
Render a nokogiri template.
-
#radius(template, options = {}, locals = {}) ⇒ String
Render a radius template.
-
#rdoc(template, options = {}, locals = {}) ⇒ String
Render an rdoc template.
-
#render(engine, data, options = {}, locals = {}, &block) ⇒ String
Renders a template with the given engine.
-
#sass(template, options = {}, locals = {}) ⇒ String
Render a sass template.
-
#scss(template, options = {}, locals = {}) ⇒ String
Render an scss template.
-
#slim(template, options = {}, locals = {}) ⇒ String
Render a slim template.
-
#textile(template, options = {}, locals = {}) ⇒ String
Render a textile template.
Instance Method Details
#builder(template = nil, options = {}, locals = {}) { ... } ⇒ String
Render a builder template
117 118 119 120 |
# File 'lib/goliath/rack/templates.rb', line 117 def builder(template = nil, = {}, locals = {}, &block) [:default_content_type] = :xml render_ruby(:builder, template, , locals, &block) end |
#coffee(template, options = {}, locals = {}) ⇒ String
Render a coffee template
204 205 206 207 |
# File 'lib/goliath/rack/templates.rb', line 204 def coffee(template, = {}, locals = {}) .merge! :layout => false, :default_content_type => :js render :coffee, template, , locals end |
#erb(template, options = {}, locals = {}) ⇒ String
Render an erb template
31 32 33 |
# File 'lib/goliath/rack/templates.rb', line 31 def erb(template, = {}, locals = {}) render :erb, template, , locals end |
#erubis(template, options = {}, locals = {}) ⇒ String
Render an erubis template
42 43 44 |
# File 'lib/goliath/rack/templates.rb', line 42 def erubis(template, = {}, locals = {}) render :erubis, template, , locals end |
#find_template(views, name, engine) ⇒ String | nil
Finds template file with same name as extension
252 253 254 255 |
# File 'lib/goliath/rack/templates.rb', line 252 def find_template(views, name, engine) filename = ::File.join(views, "#{name}.#{engine}") File.exist?(filename) ? filename : nil end |
#haml(template, options = {}, locals = {}) ⇒ String
Render a haml template
53 54 55 |
# File 'lib/goliath/rack/templates.rb', line 53 def haml(template, = {}, locals = {}) render :haml, template, , locals end |
#less(template, options = {}, locals = {}) ⇒ String
Render a less template
88 89 90 91 |
# File 'lib/goliath/rack/templates.rb', line 88 def less(template, = {}, locals = {}) .merge! :layout => false, :default_content_type => :css render :less, template, , locals end |
#liquid(template, options = {}, locals = {}) ⇒ String
Render a liquid template
129 130 131 |
# File 'lib/goliath/rack/templates.rb', line 129 def liquid(template, = {}, locals = {}) render :liquid, template, , locals end |
#markaby(template = nil, options = {}, locals = {}) { ... } ⇒ String
Render a markaby template
193 194 195 |
# File 'lib/goliath/rack/templates.rb', line 193 def markaby(template = nil, = {}, locals = {}, &block) render_ruby(:mab, template, , locals, &block) end |
#markdown(template, options = {}, locals = {}) ⇒ String
Render a markdown template
140 141 142 |
# File 'lib/goliath/rack/templates.rb', line 140 def markdown(template, = {}, locals = {}) render :markdown, template, , locals end |
#nokogiri(template = nil, options = {}, locals = {}) { ... } ⇒ String
Render a nokogiri template
230 231 232 233 |
# File 'lib/goliath/rack/templates.rb', line 230 def nokogiri(template = nil, = {}, locals = {}, &block) [:default_content_type] = :xml render_ruby(:nokogiri, template, , locals, &block) end |
#radius(template, options = {}, locals = {}) ⇒ String
Render a radius template
173 174 175 |
# File 'lib/goliath/rack/templates.rb', line 173 def radius(template, = {}, locals = {}) render :radius, template, , locals end |
#rdoc(template, options = {}, locals = {}) ⇒ String
Render an rdoc template
162 163 164 |
# File 'lib/goliath/rack/templates.rb', line 162 def rdoc(template, = {}, locals = {}) render :rdoc, template, , locals end |
#render(engine, data, options = {}, locals = {}, &block) ⇒ String
Renders a template with the given engine. Don’t call this directly – call one of the sugar methods.
You may set template-global defaults in config, for example
config[:template] = {
:layout_engine => :haml,
}
and engine-specific defaults in config, for example
config[:template_engines] = {
:haml => {
:escape_html => true
}
}
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/goliath/rack/templates.rb', line 295 def render(engine, data, = {}, locals = {}, &block) # merge app-level options if config.has_key?(:template_engines) && config[:template_engines].has_key?(engine) = config[:template_engines][engine].merge() end = config[:template].merge() if config.has_key?(:template) # extract generic options locals = .delete(:locals) || locals || {} views = .delete(:views) || Goliath::Application.root_path('views') default_layout = .delete(:default_layout) || :layout layout = .delete(:layout) layout = default_layout if layout.nil? || layout == true content_type = .delete(:content_type) || .delete(:default_content_type) layout_engine = .delete(:layout_engine) || engine scope = .delete(:scope) || self layout_filename = find_template(views, layout, layout_engine) layout_template = if layout == false || layout_filename == nil NullLayout else Tilt.new(layout_filename, nil, ) end # mimic sinatra behavior, if a string is given consider it as a template source # otherwise a symbol is expected to be a template path if data.is_a?(Symbol) template_filename = find_template(views, data, engine) unless template_filename raise Goliath::Validation::InternalServerError, "Template #{data} not found in #{views} for #{engine}" end template = Tilt.new(template_filename, nil, ) output = layout_template.render(scope, locals) do template.render(scope, locals) end else template = Tilt[engine].new(nil, nil, ){ data } output = layout_template.render(scope, locals) do template.render(scope, locals) end end output.extend(ContentTyped).content_type = content_type if content_type output end |
#sass(template, options = {}, locals = {}) ⇒ String
Render a sass template
64 65 66 67 |
# File 'lib/goliath/rack/templates.rb', line 64 def sass(template, = {}, locals = {}) .merge! :layout => false, :default_content_type => :css render :sass, template, , locals end |
#scss(template, options = {}, locals = {}) ⇒ String
Render an scss template
76 77 78 79 |
# File 'lib/goliath/rack/templates.rb', line 76 def scss(template, = {}, locals = {}) .merge! :layout => false, :default_content_type => :css render :scss, template, , locals end |
#slim(template, options = {}, locals = {}) ⇒ String
Render a slim template
242 243 244 |
# File 'lib/goliath/rack/templates.rb', line 242 def slim(template, = {}, locals = {}) render :slim, template, , locals end |
#textile(template, options = {}, locals = {}) ⇒ String
Render a textile template
151 152 153 |
# File 'lib/goliath/rack/templates.rb', line 151 def textile(template, = {}, locals = {}) render :textile, template, , locals end |