Module: Merb::Template
- Defined in:
- lib/merb-core/controller/template.rb
Defined Under Namespace
Classes: Erubis
Constant Summary collapse
- EXTENSIONS =
{}
- METHOD_LIST =
{}
- SUPPORTED_LOCALS_LIST =
Hash.new([].freeze)
- MTIMES =
{}
Class Method Summary collapse
-
.engine_for(path) ⇒ Object
Finds the engine for a particular path.
-
.inline_template(io, locals = [], 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.
-
.load_template_io(path) ⇒ Object
For a given path, get an IO object that responds to #path.
-
.needs_compilation?(path, locals) ⇒ Boolean
Decide if a template needs to be re/compiled.
-
.register_extensions(engine, extensions) ⇒ Object
Registers the extensions that will trigger a particular templating engine.
-
.template_extensions ⇒ Object
Get all known template extensions.
-
.template_for(path, template_stack = [], locals = []) ⇒ Object
Get the name of the template method for a particular path.
-
.template_name(path) ⇒ Object
Get the template’s method name from a full path.
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.
:api: private
149 150 151 152 |
# File 'lib/merb-core/controller/template.rb', line 149 def engine_for(path) path = File.(path) EXTENSIONS[path.match(/\.([^\.]*)$/)[1]] end |
.inline_template(io, locals = [], 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
- io<#path>
-
An IO that responds to #path (File or VirtualFile)
- locals<Array>
-
A list of local names that should be assigned in the template method from the arguments hash. Defaults to [].
- mod<Module>
-
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
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/merb-core/controller/template.rb', line 128 def inline_template(io, locals=[], mod = Merb::InlineTemplates) full_file_path = File.(io.path) engine_neutral_path = full_file_path.gsub(/\.[^\.]*$/, "") SUPPORTED_LOCALS_LIST[engine_neutral_path] |= locals unless locals.empty? ret = METHOD_LIST[engine_neutral_path] = engine_for(full_file_path).compile_template(io, template_name(full_file_path), locals, mod) io.close ret end |
.load_template_io(path) ⇒ Object
For a given path, get an IO object that responds to #path.
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 #template_for, and thus the RenderMixin in general.
Parameters
- path<String>
-
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
53 54 55 |
# File 'lib/merb-core/controller/template.rb', line 53 def load_template_io(path) File.open(path, "r") end |
.needs_compilation?(path, locals) ⇒ Boolean
Decide if a template needs to be re/compiled.
Parameters
- path<String>
-
The full path of the template to check support for.
- locals<Array>
-
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
89 90 91 92 93 94 95 |
# File 'lib/merb-core/controller/template.rb', line 89 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 |
.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.
Returns
nil
Example
Merb::Template.register_extensions(Merb::Template::Erubis, ["erb"])
:api: plugin
173 174 175 176 177 178 179 180 |
# File 'lib/merb-core/controller/template.rb', line 173 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_extensions ⇒ Object
Get all known template extensions
Returns
Array:: Extension strings.
:api: plugin
103 104 105 |
# File 'lib/merb-core/controller/template.rb', line 103 def template_extensions EXTENSIONS.keys end |
.template_for(path, template_stack = [], locals = []) ⇒ 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.
- locals<Array>
-
The names of local variables
Returns
- <String>
-
name of the method that inlines the template.
:api: private
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/merb-core/controller/template.rb', line 68 def template_for(path, template_stack = [], locals=[]) path = File.(path) if needs_compilation?(path, locals) file = Dir["#{path}.{#{template_extensions.join(',')}}"].first METHOD_LIST[path] = file ? inline_template(load_template_io(file), locals) : nil end METHOD_LIST[path] 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.
:api: private
30 31 32 33 |
# File 'lib/merb-core/controller/template.rb', line 30 def template_name(path) path = File.(path) path.gsub(/[^\.a-zA-Z0-9]/, "__").gsub(/\./, "_") end |