Class: ActionView::Template

Inherits:
Object show all
Extended by:
TemplateHandlers, ActiveSupport::Memoizable
Includes:
Renderable
Defined in:
lib/action_view/template.rb

Direct Known Subclasses

ReloadableTemplate

Defined Under Namespace

Classes: EagerPath, Path

Constant Summary collapse

@@exempt_from_layout =

Templates that are exempt from layouts

Set.new([/\.rjs$/])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TemplateHandlers

extended, handler_class_for_extension, register_default_template_handler, register_template_handler, registered_template_handler, template_handler_extensions

Methods included from Renderable

#compiled_source, #handler, #method_name, #method_name_without_locals, #render, #render_without_template_tracking

Constructor Details

#initialize(template_path, load_path = nil) ⇒ Template

Returns a new instance of Template.



116
117
118
119
120
121
122
123
# File 'lib/action_view/template.rb', line 116

def initialize(template_path, load_path = nil)
  @template_path, @load_path = template_path.dup, load_path
  @base_path, @name, @locale, @format, @extension = split(template_path)
  @base_path.to_s.gsub!(/\/$/, '') # Push to split method

  # Extend with partial super powers
  extend RenderablePartial if @name =~ /^_/
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



111
112
113
# File 'lib/action_view/template.rb', line 111

def base_path
  @base_path
end

#extensionObject

Returns the value of attribute extension.



112
113
114
# File 'lib/action_view/template.rb', line 112

def extension
  @extension
end

#filenameObject



188
189
190
191
# File 'lib/action_view/template.rb', line 188

def filename
  # no load_path means this is an "absolute pathed" template
  load_path ? File.join(load_path, template_path) : template_path
end

#formatObject

Returns the value of attribute format.



112
113
114
# File 'lib/action_view/template.rb', line 112

def format
  @format
end

#load_pathObject

Returns the value of attribute load_path.



111
112
113
# File 'lib/action_view/template.rb', line 111

def load_path
  @load_path
end

#localeObject

Returns the value of attribute locale.



112
113
114
# File 'lib/action_view/template.rb', line 112

def locale
  @locale
end

#nameObject

Returns the value of attribute name.



112
113
114
# File 'lib/action_view/template.rb', line 112

def name
  @name
end

#template_pathObject

Returns the value of attribute template_path.



111
112
113
# File 'lib/action_view/template.rb', line 111

def template_path
  @template_path
end

Class Method Details

.exempt_from_layout(*extensions) ⇒ Object

Don’t render layouts for templates with the given extensions.



104
105
106
107
108
109
# File 'lib/action_view/template.rb', line 104

def self.exempt_from_layout(*extensions)
  regexps = extensions.collect do |extension|
    extension.is_a?(Regexp) ? extension : /\.#{Regexp.escape(extension.to_s)}$/
  end
  @@exempt_from_layout.merge(regexps)
end

Instance Method Details

#accessible_pathsObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/action_view/template.rb', line 125

def accessible_paths
  paths = []

  if valid_extension?(extension)
    paths << path
    paths << path_without_extension
    if multipart?
      formats = format.split(".")
      paths << "#{path_without_format_and_extension}.#{formats.first}"
      paths << "#{path_without_format_and_extension}.#{formats.second}"
    end
  else
    # template without explicit template handler should only be reachable through its exact path
    paths << template_path
  end

  paths
end

#content_typeObject



153
154
155
# File 'lib/action_view/template.rb', line 153

def content_type
  format.gsub('.', '/')
end

#exempt_from_layout?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/action_view/template.rb', line 184

def exempt_from_layout?
  @@exempt_from_layout.any? { |exempted| path =~ exempted }
end

#format_and_extensionObject



144
145
146
# File 'lib/action_view/template.rb', line 144

def format_and_extension
  (extensions = [format, extension].compact.join(".")).blank? ? nil : extensions
end

#load!Object



216
217
218
# File 'lib/action_view/template.rb', line 216

def load!
  freeze
end

#method_segmentObject



199
200
201
# File 'lib/action_view/template.rb', line 199

def method_segment
  relative_path.to_s.gsub(/([^a-zA-Z0-9_])/) { $1.ord }
end

#mime_typeObject



157
158
159
# File 'lib/action_view/template.rb', line 157

def mime_type
  Mime::Type.lookup_by_extension(format) if format && defined?(::Mime)
end

#multipart?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/action_view/template.rb', line 149

def multipart?
  format && format.include?('.')
end

#pathObject



162
163
164
# File 'lib/action_view/template.rb', line 162

def path
  [base_path, [name, locale, format, extension].compact.join('.')].compact.join('/')
end

#path_without_extensionObject



167
168
169
# File 'lib/action_view/template.rb', line 167

def path_without_extension
  [base_path, [name, locale, format].compact.join('.')].compact.join('/')
end

#path_without_format_and_extensionObject



172
173
174
# File 'lib/action_view/template.rb', line 172

def path_without_format_and_extension
  [base_path, [name, locale].compact.join('.')].compact.join('/')
end

#relative_pathObject



177
178
179
180
181
# File 'lib/action_view/template.rb', line 177

def relative_path
  path = File.expand_path(filename)
  path.sub!(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '') if defined?(RAILS_ROOT)
  path
end

#render_template(view, local_assigns = {}) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/action_view/template.rb', line 204

def render_template(view, local_assigns = {})
  render(view, local_assigns)
rescue Exception => e
  raise e unless filename
  if TemplateError === e
    e.sub_template_of(self)
    raise e
  else
    raise TemplateError.new(self, view.assigns, e)
  end
end

#sourceObject



194
195
196
# File 'lib/action_view/template.rb', line 194

def source
  File.read(filename)
end