Class: ActionView::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TemplateHandlers

extended, handler_class_for_extension, register_default_template_handler, register_template_handler, template_handler_extensions

Methods included from Renderable

#compiled_source, #handler, included, #method_name, #render

Constructor Details

#initialize(template_path, load_paths = []) ⇒ Template

Returns a new instance of Template.



12
13
14
15
16
17
18
19
20
# File 'lib/action_view/template.rb', line 12

def initialize(template_path, load_paths = [])
  template_path = template_path.dup
  @base_path, @name, @format, @extension = split(template_path)
  @base_path.to_s.gsub!(/\/$/, '') # Push to split method
  @load_path, @filename = find_full_path(template_path, load_paths)

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

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



9
10
11
# File 'lib/action_view/template.rb', line 9

def base_path
  @base_path
end

#extensionObject

Returns the value of attribute extension.



9
10
11
# File 'lib/action_view/template.rb', line 9

def extension
  @extension
end

#filenameObject

Returns the value of attribute filename.



9
10
11
# File 'lib/action_view/template.rb', line 9

def filename
  @filename
end

#formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/action_view/template.rb', line 9

def format
  @format
end

#load_pathObject

Returns the value of attribute load_path.



9
10
11
# File 'lib/action_view/template.rb', line 9

def load_path
  @load_path
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/action_view/template.rb', line 9

def name
  @name
end

Instance Method Details

#content_typeObject



31
32
33
# File 'lib/action_view/template.rb', line 31

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

#format_and_extensionObject



22
23
24
# File 'lib/action_view/template.rb', line 22

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

#method_segmentObject



67
68
69
# File 'lib/action_view/template.rb', line 67

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

#mime_typeObject



35
36
37
# File 'lib/action_view/template.rb', line 35

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

#multipart?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/action_view/template.rb', line 27

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

#pathObject



40
41
42
# File 'lib/action_view/template.rb', line 40

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

#path_without_extensionObject



45
46
47
# File 'lib/action_view/template.rb', line 45

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

#path_without_format_and_extensionObject



50
51
52
# File 'lib/action_view/template.rb', line 50

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

#relative_pathObject



55
56
57
58
59
# File 'lib/action_view/template.rb', line 55

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



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/action_view/template.rb', line 72

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



62
63
64
# File 'lib/action_view/template.rb', line 62

def source
  File.read(filename)
end