-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tilt based template base class for temple engines
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'tilt' | ||
|
||
module Temple | ||
# Tilt template implementation for Temple | ||
class Template < Tilt::Template | ||
class << self | ||
def engine(engine = nil) | ||
if engine | ||
@engine = Symbol === engine ? Temple::Engines.const_get(engine) : engine | ||
elsif @engine | ||
@engine | ||
else | ||
raise 'No engine configured' | ||
end | ||
end | ||
|
||
def helpers(helpers = nil) | ||
if helpers | ||
@helpers = helpers | ||
else | ||
@helpers | ||
end | ||
end | ||
end | ||
|
||
# Prepare Temple template | ||
# | ||
# Called immediately after template data is loaded. | ||
# | ||
# @return [void] | ||
def prepare | ||
@src = self.class.engine.new(options.merge(:file => eval_file)).compile(data) | ||
end | ||
|
||
# Process the template and return the result. | ||
# | ||
# Template executationis guaranteed to be performed in the scope object with the locals | ||
# specified and with support for yielding to the block. | ||
# | ||
# @param [Object] scope Scope object where the code is evaluated | ||
# @param [Hash] locals Local variables | ||
# @yield Block given to the template code | ||
# @return [String] Evaluated template | ||
def evaluate(scope, locals, &block) | ||
helpers = self.class.helpers | ||
scope.instance_eval { extend helpers } if options[:helpers] && helpers | ||
super | ||
end | ||
|
||
# A string containing the (Ruby) source code for the template. | ||
# | ||
# @param [Hash] locals Local variables | ||
# @return [String] Compiled template ruby code | ||
def precompiled_template(locals = {}) | ||
@src | ||
end | ||
end | ||
end |
40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm… I'm not a very big fan of the helpers-thingie:
:helpers => true
? What's the use-case here? I don't see how users will explicitly say "I want to use the helpers provided by this template engine". Either it should true by default (aka. it's a feature of the template engine) or the user should choose his helpers himself.40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is how it is done in slim. It should work if the template is compiled. The problem is that the scope is always extended with the helpers. You could also write
if MyApplication is the scope.
40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of the helpers either. I don't think they belong in the template language and was gong to review, for possible removal, their use in Slim.
40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently we have only the helper list_of which is used to port applications from haml. The work done by this helper is quite trivial, can be removed if nobody likes it :-/
40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll remove it tonight. :)
40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO:
If a template engine requires a set of helpers it should generate
extend MyHelpers
in the code.If the user want to use some helpers, he should extend/include them on the scope himself.
40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I remove the support in the temple template.
40e81af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is more clean, I agree