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.

Author:

Instance Method Summary collapse

Instance Method Details

#builder(template = nil, options = {}, locals = {}) { ... } ⇒ String

Render a builder template

Examples:

# produces:
#   <?xml version='1.0' encoding='UTF-8'?>
#   <person>
#     <name aka='Frank Sinatra'>Francis Albert Sinatra</name>
#     <email>Frank Sinatra</email>
#   </person>
builder do |xml|
  xml.instruct!
  xml.person do
    xml.name "Francis Albert Sinatra", :aka => "Frank Sinatra"
    xml.email '[email protected]'
  end
end

Parameters:

  • template (Symbol, String) (defaults to: nil)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Yields:

  • block If the builder method is given a block, the block is called directly with an XmlMarkup instance and used as a template.

Returns:

  • (String)

    The rendered template

See Also:



117
118
119
120
# File 'lib/goliath/rack/templates.rb', line 117

def builder(template = nil, options = {}, locals = {}, &block)
  options[:default_content_type] = :xml
  render_ruby(:builder, template, options, locals, &block)
end

#coffee(template, options = {}, locals = {}) ⇒ String

Render a coffee template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



204
205
206
207
# File 'lib/goliath/rack/templates.rb', line 204

def coffee(template, options = {}, locals = {})
  options.merge! :layout => false, :default_content_type => :js
  render :coffee, template, options, locals
end

#erb(template, options = {}, locals = {}) ⇒ String

Render an erb template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



31
32
33
# File 'lib/goliath/rack/templates.rb', line 31

def erb(template, options = {}, locals = {})
  render :erb, template, options, locals
end

#erubis(template, options = {}, locals = {}) ⇒ String

Render an erubis template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



42
43
44
# File 'lib/goliath/rack/templates.rb', line 42

def erubis(template, options = {}, locals = {})
  render :erubis, template, options, locals
end

#find_template(views, name, engine) ⇒ String | nil

Finds template file with same name as extension

Parameters:

  • views (String)

    The view directory

  • name (String)

    The template name

  • engine (String)

    The template type

Returns:

  • (String | nil)

    Template file or nil if it doesn’t exist.



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

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



53
54
55
# File 'lib/goliath/rack/templates.rb', line 53

def haml(template, options = {}, locals = {})
  render :haml, template, options, locals
end

#less(template, options = {}, locals = {}) ⇒ String

Render a less template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



88
89
90
91
# File 'lib/goliath/rack/templates.rb', line 88

def less(template, options = {}, locals = {})
  options.merge! :layout => false, :default_content_type => :css
  render :less, template, options, locals
end

#liquid(template, options = {}, locals = {}) ⇒ String

Render a liquid template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



129
130
131
# File 'lib/goliath/rack/templates.rb', line 129

def liquid(template, options = {}, locals = {})
  render :liquid, template, options, locals
end

#markaby(template = nil, options = {}, locals = {}) { ... } ⇒ String

Render a markaby template

Examples:

markaby do
  html do
    head { title "Sinatra With Markaby" }
    body { h1 "Markaby Is Fun!" }
  end
end

Parameters:

  • template (Symbol, String) (defaults to: nil)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Yields:

  • block A block template

Returns:

  • (String)

    The rendered template

See Also:



193
194
195
# File 'lib/goliath/rack/templates.rb', line 193

def markaby(template = nil, options = {}, locals = {}, &block)
  render_ruby(:mab, template, options, locals, &block)
end

#markdown(template, options = {}, locals = {}) ⇒ String

Render a markdown template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



140
141
142
# File 'lib/goliath/rack/templates.rb', line 140

def markdown(template, options = {}, locals = {})
  render :markdown, template, options, locals
end

#nokogiri(template = nil, options = {}, locals = {}) { ... } ⇒ String

Render a nokogiri template

Examples:

# produces
#   <ul>
#     <li>hello</li>
#     <li class="current">admin</li>
#   </ul>
nokogiri do |doc|
  doc.ul do
    doc.li 'hello'
    doc.li 'admin', :class => 'current' if current_user.is_admin?
  end
end

Parameters:

  • template (Symbol, String) (defaults to: nil)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Yields:

  • block A block template

Returns:

  • (String)

    The rendered template

See Also:



230
231
232
233
# File 'lib/goliath/rack/templates.rb', line 230

def nokogiri(template = nil, options = {}, locals = {}, &block)
  options[:default_content_type] = :xml
  render_ruby(:nokogiri, template, options, locals, &block)
end

#radius(template, options = {}, locals = {}) ⇒ String

Render a radius template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



173
174
175
# File 'lib/goliath/rack/templates.rb', line 173

def radius(template, options = {}, locals = {})
  render :radius, template, options, locals
end

#rdoc(template, options = {}, locals = {}) ⇒ String

Render an rdoc template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



162
163
164
# File 'lib/goliath/rack/templates.rb', line 162

def rdoc(template, options = {}, locals = {})
  render :rdoc, template, options, 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
  }
}

Parameters:

  • engine (Symbol)

    The engine (:haml, :erb, :textile, etc) to use

  • template (Symbol, String)

    Either the name or path of the template as symbol (Use ‘:’subdir/myview’‘ for views in subdirectories), or a string that will be rendered. It looks for templates in Goliath::Application.root_path/views by default. It is not as clever as Sinatra: you must name your template file template_name.engine_name – so ’foo.markdown’, not ‘foo.md’.

  • options (Hash) (defaults to: {})

    Options for layout

  • locals (Hash) (defaults to: {})

    You can give a hash of local variables available to the template either directly in options[:local] or in a separate hash as the last parameter.

Options Hash (options):

  • :content_type (String)

    The MIME content type to use.

  • :layout (String)

    If false, no layout is rendered, otherwise the specified layout is used.

  • :layout_engine (String)

    Engine to use for rendering the layout.

  • :locals (Hash)

    A hash with local variables that should be available in the template

  • :scope (Object)

    If set, template is evaluate with the binding of the given object rather than the application instance.

  • :views (String)

    Views directory to use.

Returns:

  • (String)

    The rendered template



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, options = {}, locals = {}, &block)
  # merge app-level options
  if config.has_key?(:template_engines) && config[:template_engines].has_key?(engine)
    options = config[:template_engines][engine].merge(options)
  end

  options = config[:template].merge(options) if config.has_key?(:template)

  # extract generic options
  locals = options.delete(:locals) || locals || {}
  views = options.delete(:views) || Goliath::Application.root_path('views')

  default_layout  = options.delete(:default_layout) || :layout

  layout = options.delete(:layout)
  layout = default_layout if layout.nil? || layout == true

  content_type = options.delete(:content_type) || options.delete(:default_content_type)
  layout_engine = options.delete(:layout_engine) || engine
  scope = options.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, options)
  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, options)
    output = layout_template.render(scope, locals) do
      template.render(scope, locals)
    end

  else
    template = Tilt[engine].new(nil, nil, options){ 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

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



64
65
66
67
# File 'lib/goliath/rack/templates.rb', line 64

def sass(template, options = {}, locals = {})
  options.merge! :layout => false, :default_content_type => :css
  render :sass, template, options, locals
end

#scss(template, options = {}, locals = {}) ⇒ String

Render an scss template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



76
77
78
79
# File 'lib/goliath/rack/templates.rb', line 76

def scss(template, options = {}, locals = {})
  options.merge! :layout => false, :default_content_type => :css
  render :scss, template, options, locals
end

#slim(template, options = {}, locals = {}) ⇒ String

Render a slim template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



242
243
244
# File 'lib/goliath/rack/templates.rb', line 242

def slim(template, options = {}, locals = {})
  render :slim, template, options, locals
end

#textile(template, options = {}, locals = {}) ⇒ String

Render a textile template

Parameters:

  • template (Symbol, String)

    Template Path (symbol) or contents (String)

  • options (Hash) (defaults to: {})

    Rendering options – see #render

  • locals (Hash) (defaults to: {})

    Template-local variables – see #render

Returns:

  • (String)

    The rendered template

See Also:



151
152
153
# File 'lib/goliath/rack/templates.rb', line 151

def textile(template, options = {}, locals = {})
  render :textile, template, options, locals
end