Skip to content

Commit

Permalink
tilt based template base class for temple engines
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Oct 30, 2010
1 parent 3ee4d7d commit 40e81af
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/temple/template.rb
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

8 comments on commit 40e81af

@judofyr
Copy link
Owner

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:

  1. It won't work when Tilt compiles the template.
  2. Why will it only use the helpers when you set :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.

@minad
Copy link
Collaborator Author

@minad minad commented on 40e81af Oct 31, 2010

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

class MyApplication
    extend Slim::Helpers
end

if MyApplication is the scope.

@stonean
Copy link
Contributor

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.

@minad
Copy link
Collaborator Author

@minad minad commented on 40e81af Oct 31, 2010

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 :-/

@stonean
Copy link
Contributor

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. :)

@judofyr
Copy link
Owner

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.

@minad
Copy link
Collaborator Author

@minad minad commented on 40e81af Oct 31, 2010

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.

@minad
Copy link
Collaborator Author

@minad minad commented on 40e81af Oct 31, 2010

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

Please sign in to comment.