Namespace

Class Index [+]

Quicksearch

Merb::Template

Constants

EXTENSIONS
METHOD_LIST
SUPPORTED_LOCALS_LIST
MTIMES

Public Class Methods

engine_for(path) click to toggle source

Finds the engine for a particular path.

Parameters

path

The path of the file to find an engine for.

Returns

Class

The engine.

:api: private

     # File lib/merb-core/controller/template.rb, line 153
153:     def engine_for(path)
154:       path = File.expand_path(path)      
155:       EXTENSIONS[path.match(/\.([^\.]*)$/)[1]]
156:     end
inline_template(io, locals=[], mod = Merb::InlineTemplates) click to toggle source

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

io<#>

An IO that responds to # (File or VirtualFile)

locals

A list of local names that should be assigned in the template method from the arguments hash. Defaults to [].

mod

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

Returns

Symbol

The name of the method that the template was compiled into.

Notes

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

:api: private

     # File lib/merb-core/controller/template.rb, line 131
131:     def inline_template(io, locals=[], mod = Merb::InlineTemplates)
132:       full_file_path = File.expand_path(io.path)
133:       engine_neutral_path = full_file_path.gsub(/\.[^\.]*$/, "")
134:       
135:       local_list = (SUPPORTED_LOCALS_LIST[engine_neutral_path] |= locals)
136:       ret = METHOD_LIST[engine_neutral_path] =
137:         engine_for(full_file_path).compile_template(io, 
138:         template_name(full_file_path), local_list, mod)
139:         
140:       io.close
141:       ret
142:     end
load_template_io(path) click to toggle source

For a given path, get an IO object that responds to #.

This is so that plugins can override this if they provide mechanisms for specifying templates that are not just simple files. The plugin is responsible for ensuring that the fake path provided will work with #, and thus the RenderMixin in general.

Parameters

path

A full path to find a template for. This is the

  path that the RenderMixin assumes it should find the template
  in.

Returns

IO#path

An IO object that responds to path (File or VirtualFile).

:api: plugin @overridable

    # File lib/merb-core/controller/template.rb, line 54
54:       def load_template_io(path)
55:         file = Dir["#{path}.{#{template_extensions.join(',')}}"].first
56:         File.open(file, "r") if file
57:       end
needs_compilation?(path, locals) click to toggle source

Decide if a template needs to be re/compiled.

Parameters

path

The full path of the template to check support for.

locals

The list of locals that need to be supported

Returns

Boolean

Whether or not the template for the provided path needs to be recompiled

:api: private

    # File lib/merb-core/controller/template.rb, line 92
92:     def needs_compilation?(path, locals)
93:       return true if Merb::Config[:reload_templates] || !METHOD_LIST[path]
94:       
95:       current_locals = SUPPORTED_LOCALS_LIST[path]
96:       current_locals != locals &&
97:         !(locals - current_locals).empty?
98:     end
register_extensions(engine, extensions) click to toggle source

Registers the extensions that will trigger a particular templating engine.

Parameters

engine

The class of the engine that is being registered

extensions

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

Raises

ArgumentError

engine does not have a compile_template method.

Returns

nil

Example

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

:api: plugin

     # File lib/merb-core/controller/template.rb, line 177
177:     def register_extensions(engine, extensions) 
178:       raise ArgumentError, "The class you are registering does not have a compile_template method" unless
179:         engine.respond_to?(:compile_template)
180:       extensions.each{|ext| EXTENSIONS[ext] = engine }
181:       Merb::AbstractController.class_eval         include #{engine}::Mixin
182:     end
template_extensions() click to toggle source

Get all known template extensions

Returns

  Array:: Extension strings.

:api: plugin

     # File lib/merb-core/controller/template.rb, line 106
106:     def template_extensions
107:       EXTENSIONS.keys
108:     end
template_for(path, template_stack = [], locals=[]) click to toggle source

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

Parameters

path

A full path to find a template method for.

template_stack

The template stack. Not used.

locals

The names of local variables

Returns

name of the method that inlines the template.

:api: private

    # File lib/merb-core/controller/template.rb, line 71
71:     def template_for(path, template_stack = [], locals=[])
72:       path = File.expand_path(path)
73:       
74:       if needs_compilation?(path, locals)
75:         template_io = load_template_io(path)
76:         METHOD_LIST[path] = inline_template(template_io, locals) if template_io
77:       end
78:       
79:       METHOD_LIST[path]
80:     end
template_name(path) click to toggle source

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

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.

:api: private

    # File lib/merb-core/controller/template.rb, line 30
30:     def template_name(path)
31:       path = File.expand_path(path)      
32:       path.gsub(/[^\.a-zA-Z0-9]/, "__").gsub(/\./, "_")
33:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.