Module: Merb::Template

Defined in:
merb-core/lib/merb-core/controller/template.rb,
merb-haml/lib/merb-haml/template.rb

Defined Under Namespace

Classes: Erubis, Haml

Constant Summary

EXTENSIONS =
{}
METHOD_LIST =
{}
SUPPORTED_LOCALS_LIST =
Hash.new([].freeze)
MTIMES =
{}

Class Method Summary (collapse)

Class Method Details

+ (Class) engine_for(path)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Finds the engine for a particular path.



142
143
144
145
# File 'merb-core/lib/merb-core/controller/template.rb', line 142

def engine_for(path)
  path = File.expand_path(path)      
  EXTENSIONS[path.match(/\.([^\.]*)$/)[1]]
end

+ (Symbol) inline_template(io, locals = [], mod = Merb::InlineTemplates)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Even though this method supports inlining into any module, the method must be available to instances of AbstractController that will use it.

Takes a template at a particular path and inlines it into a module and adds it to the METHOD_LIST table to speed lookup later.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'merb-core/lib/merb-core/controller/template.rb', line 122

def inline_template(io, locals=[], mod = Merb::InlineTemplates)
  full_file_path = File.expand_path(io.path)
  engine_neutral_path = full_file_path.gsub(/\.[^\.]*$/, "")
  
  local_list = (SUPPORTED_LOCALS_LIST[engine_neutral_path] |= locals)
  ret = METHOD_LIST[engine_neutral_path] =
    engine_for(full_file_path).compile_template(io, 
    template_name(full_file_path), local_list, mod)
    
  io.close
  ret
end

+ (Boolean) needs_compilation?(path, locals)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decide if a template needs to be re/compiled.



88
89
90
91
92
93
94
# File 'merb-core/lib/merb-core/controller/template.rb', line 88

def needs_compilation?(path, locals)
  return true if Merb::Config[:reload_templates] || !METHOD_LIST[path]
  
  current_locals = SUPPORTED_LOCALS_LIST[path]
  current_locals != locals &&
    !(locals - current_locals).empty?
end

+ (nil) register_extensions(engine, extensions)

Registers the extensions that will trigger a particular templating engine.

Examples:

Merb::Template.register_extensions(Merb::Template::Erubis, ["erb"])

Raises:

  • (ArgumentError)

    Engine does not have a compile_template method.



162
163
164
165
166
167
168
169
# File 'merb-core/lib/merb-core/controller/template.rb', line 162

def register_extensions(engine, extensions) 
  raise ArgumentError, "The class you are registering does not have a compile_template method" unless
    engine.respond_to?(:compile_template)
  extensions.each{|ext| EXTENSIONS[ext] = engine }
  Merb::AbstractController.class_eval <<-HERE
    include #{engine}::Mixin
  HERE
end

+ (Array<String>) template_extensions

Get all known template extensions



101
102
103
# File 'merb-core/lib/merb-core/controller/template.rb', line 101

def template_extensions
  EXTENSIONS.keys
end

+ (String) template_for(path, template_stack = [], locals = [])

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the name of the template method for a particular path.



66
67
68
69
70
71
72
73
74
75
# File 'merb-core/lib/merb-core/controller/template.rb', line 66

def template_for(path, template_stack = [], locals=[])
  path = File.expand_path(path)
  
  if needs_compilation?(path, locals)
    template_io = load_template_io(path)
    METHOD_LIST[path] = inline_template(template_io, locals) if template_io
  end
  
  METHOD_LIST[path]
end

+ (String) template_name(path)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

We might want to replace this with something that varies the character replaced based on the non-alphanumeric character to avoid edge-case collisions.

Get the template's method name from a full path. This replaces non-alphanumeric characters with __ and "." with "_"

Collisions are potentially possible with something like: ~foo.bar and __foo.bar or !foo.bar.



28
29
30
31
# File 'merb-core/lib/merb-core/controller/template.rb', line 28

def template_name(path)
  path = File.expand_path(path)      
  path.gsub(/[^\.a-zA-Z0-9]/, "__").gsub(/\./, "_")
end