lib/wlang/dialect.rb in wlang-2.0.1 vs lib/wlang/dialect.rb in wlang-2.1.0
- old
+ new
@@ -12,12 +12,12 @@
def compile(source, options = {})
Template.new source, :dialect => self
end
- def render(source, scope = {}, buffer = "")
- compile(source).call(scope, buffer)
+ def render(source, *args)
+ compile(source).call(*args)
end
# tag installation and dispatching
# Install a new tag on the dialect for `symbols`.
@@ -137,30 +137,33 @@
# Returns the buffer.
#
def render(fn, scope = nil, buffer = "")
if scope.nil?
case fn
- when String
- buffer << fn
- when Proc
- fn.call(self, buffer)
- when Template
- fn.call(@scope, buffer)
- else
- raise ArgumentError, "Unable to render `#{fn}`"
+ when String then buffer << fn
+ when Proc then fn.call(self, buffer)
+ when Template then fn.call(@scope, buffer)
+ when TiltTemplate then buffer << fn.render(@scope)
+ else
+ raise ArgumentError, "Unable to render `#{fn}`"
end
buffer
else
with_scope(scope){ render(fn, nil, buffer) }
end
end
# evaluation
+ # Returns the execution context, defined as the subject of the root scope.
+ def context
+ scope.root.subject
+ end
+
# Returns the current rendering scope.
def scope
- @scope ||= Scope.root
+ @scope || Scope.null
end
# Yields the block with a scope branched with a sub-scope `x`.
def with_scope(x)
@scope = scope.push(x)
@@ -175,19 +178,20 @@
# * a Proc or a Template, first rendered and then evaluated
#
# Evaluation is delegated to the scope (@see Scope#evaluate) and the result returned
# by this method.
#
- def evaluate(expr, *default)
+ def evaluate(expr, *default, &bl)
case expr
when Symbol, String
catch(:fail) do
- return scope.evaluate(expr, *default)
+ return scope.evaluate(expr, self, *default, &bl)
end
raise NameError, "Unable to find `#{expr}`"
else
- evaluate(render(expr), *default)
+ evaluate(render(expr), *default, &bl)
end
end
+ alias :value_of :evaluate
end # class Dialect
end # module WLang