Module: Padrino::Rendering::ClassMethods

Defined in:
padrino-helpers/lib/padrino/rendering.rb

Overview

Class methods responsible for rendering templates as part of a request.

Instance Method Summary collapse

Instance Method Details

#cache_layout_path(name) ⇒ Object



121
122
123
124
125
126
127
128
# File 'padrino-helpers/lib/padrino/rendering.rb', line 121

def cache_layout_path(name)
  @_cached_layout ||= {}
  if !reload_templates? && path = @_cached_layout[name]
    path
  else
    @_cached_layout[name] = yield(name)
  end
end

#cache_template_path(options) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'padrino-helpers/lib/padrino/rendering.rb', line 130

def cache_template_path(options)
  began_at = Time.now
  @_cached_templates ||= {}
  logging = defined?(settings) && settings.logging? && defined?(logger)
  if !reload_templates? && path = @_cached_templates[options]
    logger.debug :cached, began_at, path[0] if logging
  else
    path = @_cached_templates[options] = yield(name)
    logger.debug :template, began_at, path[0] if path && logging
  end
  path
end

#fetch_layout_path(given_layout, layouts_path = views) ⇒ Object

Returns the cached layout path.

Parameters:

  • given_layout (String, nil)

    The requested layout.

  • layouts_path (String, nil) (defaults to: views)

    The directory where the layouts are located. Defaults to #views.



110
111
112
113
114
115
116
117
118
119
# File 'padrino-helpers/lib/padrino/rendering.rb', line 110

def fetch_layout_path(given_layout, layouts_path=views)
  layout_name = (given_layout || @layout || :application).to_s
  cache_layout_path(layout_name) do
    if Pathname.new(layout_name).absolute? && Dir["#{layout_name}.*"].any? || Dir["#{layouts_path}/#{layout_name}.*"].any?
      layout_name
    else
      File.join('layouts', layout_name)
    end
  end
end

#layout(name = :layout) { ... } ⇒ Object

Use layout like rails does or if a block given then like sinatra. If used without a block, sets the current layout for the route.

By default, searches in your:

app/views/layouts/application.(haml|erb|xxx) app/views/layout_name.(haml|erb|xxx)

If you define layout :custom then searches for your layouts in app/views/layouts/custom.(haml|erb|xxx) app/views/custom.(haml|erb|xxx)

Parameters:

  • name (Symbol) (defaults to: :layout)

    (:layout) The layout to use.

Yields:



97
98
99
100
# File 'padrino-helpers/lib/padrino/rendering.rb', line 97

def layout(name=:layout, &block)
  return super(name, &block) if block_given?
  @layout = name
end