Module: Merb::Template

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

Defined Under Namespace

Classes: Erubis

Constant Summary collapse

EXTENSIONS =
{}
METHOD_LIST =
{}
MTIMES =
{}

Class Method Summary collapse

Class Method Details

.engine_for(path) ⇒ Object

Finds the engine for a particular path.

Parameters

path<String>

The path of the file to find an engine for.

Returns

Class

The engine.




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

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

.inline_template(path, mod = Merb::InlineTemplates) ⇒ Object

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.

Parameters

path<String>

The full path of the template (minus the templating specifier) to inline.

mod<Module>

The module to put the compiled method into. Defaults to Merb::InlineTemplates

Note

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




75
76
77
78
79
# File 'lib/merb-core/controller/template.rb', line 75

def inline_template(path, mod = Merb::InlineTemplates)
  path = File.expand_path(path)
  METHOD_LIST[path.gsub(/\.[^\.]*$/, "")] = 
    engine_for(path).compile_template(path, template_name(path), mod)
end

.register_extensions(engine, extensions) ⇒ Object

Registers the extensions that will trigger a particular templating engine.

Parameters

engine<Class>

The class of the engine that is being registered

extensions<Array>

The list of extensions that will be registered with this templating language

Raises

ArgumentError

engine does not have a compile_template method.

Example

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

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
# File 'lib/merb-core/controller/template.rb', line 111

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

.template_for(path, template_stack = []) ⇒ Object

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

Parameters

path<String>

A full path to find a template method for.

template_stack<Array>

The template stack. Not used.

Returns

DOC




42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/merb-core/controller/template.rb', line 42

def template_for(path, template_stack = [])
  path = File.expand_path(path)
  
  ret = 
  if Merb::Config[:reload_templates]
    file = Dir["#{path}.{#{Merb::Template::EXTENSIONS.keys.join(',')}}"].first
    METHOD_LIST[path] = file ? inline_template(file) : nil
  else
    METHOD_LIST[path] ||= begin
      file = Dir["#{path}.{#{Merb::Template::EXTENSIONS.keys.join(',')}}"].first          
      file ? inline_template(file) : nil
    end
  end
  
  ret
end

.template_name(path) ⇒ Object

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.

Parameters

path<String>

A full path to convert to a valid Ruby method name

Returns

String

The template name.


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



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

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