Module: Fastr::Template
- Included in:
- Controller
- Defined in:
- lib/fastr/template.rb,
lib/fastr/template/haml.rb,
lib/fastr/template/erubis.rb
Defined Under Namespace
Modules: ClassMethods Classes: Erubis, Haml
Constant Summary collapse
- EXTENSIONS =
{}
- TEMPLATE_CACHE =
{}
Class Method Summary collapse
- .included(kls) ⇒ Object
-
.register_extensions(engine, extensions) ⇒ Object
Registers the extensions that will trigger a particular templating engine.
Instance Method Summary collapse
-
#engine_for(path) ⇒ Object
Finds the engine for a particular path.
- #render(kind, tpl, opts = {}) ⇒ Object
- #render_json(obj, opts = {}) ⇒ Object
- #render_template(tpl_path, opts = {}) ⇒ Object
- #render_template_to_string(tpl_path, opts = {}) ⇒ Object
- #render_text(text, opts = {}) ⇒ Object
-
#template_extensions ⇒ Object
Get all known template extensions.
Class Method Details
.included(kls) ⇒ Object
8 9 10 |
# File 'lib/fastr/template.rb', line 8 def self.included(kls) kls.extend(ClassMethods) 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
Fastr::Template.register_extensions(Fastr::Template::Erubis, ["erb"])
53 54 55 56 57 58 59 60 |
# File 'lib/fastr/template.rb', line 53 def self.register_extensions(engine, extensions) raise ArgumentError, "The class you are registering does not have a result method" unless engine.respond_to?(:result) extensions.each{|ext| EXTENSIONS[ext] = engine } Fastr::Controller.class_eval <<-HERE include #{engine}::Mixin HERE end |
Instance 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.
23 24 25 26 |
# File 'lib/fastr/template.rb', line 23 def engine_for(path) path = File.(path) EXTENSIONS[path.match(/\.([^\.]*)$/)[1]] end |
#render(kind, tpl, opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fastr/template.rb', line 62 def render(kind, tpl, opts={}) # Read the cache template settings for this application, unless it is passed in opts[:cache_template] = self.app.settings.cache_templates unless opts[:cache_template] case kind.to_sym when :template then render_template(tpl, opts) when :partial then render_template_to_string(tpl, opts) when :text then render_text(tpl, opts) when :json then render_json(tpl, opts) else raise ArgumentError, "Unknown render type: #{kind.inspect}" end end |
#render_json(obj, opts = {}) ⇒ Object
102 103 104 105 106 |
# File 'lib/fastr/template.rb', line 102 def render_json(obj, opts={}) self.headers['Content-Type'] = 'application/json' @response_code = opts[:response_code] || 200 [ @response_code, @headers, [obj.to_json.to_s] ] end |
#render_template(tpl_path, opts = {}) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/fastr/template.rb', line 80 def render_template(tpl_path, opts={}) self.headers['Content-Type'] = 'text/html' @response_code = opts[:response_code] || 200 [ @response_code, @headers, [render_template_to_string(tpl_path, opts)] ] end |
#render_template_to_string(tpl_path, opts = {}) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/fastr/template.rb', line 87 def render_template_to_string(tpl_path, opts={}) unless engine = engine_for(tpl_path) raise ArgumentError, "No template engine registered for #{tpl_path}" end @vars = opts[:vars] || {} engine.result(tpl_path, binding(), opts[:cache_template]) end |
#render_text(text, opts = {}) ⇒ Object
96 97 98 99 100 |
# File 'lib/fastr/template.rb', line 96 def render_text(text, opts={}) self.headers['Content-Type'] = 'text/plain' @response_code = opts[:response_code] || 200 [ @response_code, @headers, [text] ] end |
#template_extensions ⇒ Object
Get all known template extensions
Returns
Array:: Extension strings.
32 33 34 |
# File 'lib/fastr/template.rb', line 32 def template_extensions EXTENSIONS.keys end |