|
|
@ -3,6 +3,7 @@ |
|
|
|
require_relative 'dsl/constructors_probe' |
|
|
|
require_relative 'dsl/methods_common' |
|
|
|
require_relative 'dsl/methods_probe' |
|
|
|
require_relative 'dsl/methods_template' |
|
|
|
|
|
|
|
module Hyde |
|
|
|
# All template engine adapters subclassed from Template |
|
|
@ -12,29 +13,37 @@ module Hyde |
|
|
|
|
|
|
|
# Context for template engines |
|
|
|
class TemplateContext |
|
|
|
include Hyde::DSL::CommonMethods |
|
|
|
include Hyde::DSL::ProbeMethods |
|
|
|
include Hyde::DSL::ProbeConstructors |
|
|
|
include Hyde::DSL::ProbeMethods |
|
|
|
include Hyde::DSL::CommonMethods |
|
|
|
include Hyde::DSL::TemplateMethods |
|
|
|
|
|
|
|
# @return [Binding] |
|
|
|
def binding |
|
|
|
Kernel.binding |
|
|
|
end |
|
|
|
|
|
|
|
def initialize(parent) |
|
|
|
def initialize(parent, parent_template) |
|
|
|
@origin = parent |
|
|
|
@parent_template = parent_template |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
# Interface for Template engines |
|
|
|
# @abstract does not represent any actual template engine. |
|
|
|
class Template |
|
|
|
# @param input [String, File] |
|
|
|
# @param context [Binding, nil] |
|
|
|
# @param locals [Hash, nil] |
|
|
|
def initialize(input, toplevel: nil, locals: nil) |
|
|
|
# @param input [String, File] template text |
|
|
|
# @param vars [Hash] local variables for tempalte |
|
|
|
# @param parent [Hyde::Node] parent node |
|
|
|
def initialize(input, vars = {}, parent:) |
|
|
|
@template = input.is_a?(File) ? input.read : input |
|
|
|
@context = TemplateContext.new(parent, self) |
|
|
|
@parent = parent |
|
|
|
input.close if input.is_a? File |
|
|
|
@binding = toplevel or binding |
|
|
|
locals&.each do |k, v| |
|
|
|
@binding.local_variable_set(k,v) |
|
|
|
@binding = @context.binding |
|
|
|
vars.each do |k, v| |
|
|
|
@binding.local_variable_set(k, v) |
|
|
|
end |
|
|
|
@context = TemplateContext.new(self) |
|
|
|
end |
|
|
|
|
|
|
|
# Set local variable |
|
|
@ -71,6 +80,16 @@ module Hyde |
|
|
|
# ... (stub) |
|
|
|
end |
|
|
|
|
|
|
|
# Import a template from within current template |
|
|
|
def import(filepath) |
|
|
|
newtemp = self.class.new(filepath, {}, parent: @parent) |
|
|
|
newtemp.binding = @binding |
|
|
|
puts newtemp.pretty_inspect |
|
|
|
newtemp |
|
|
|
end |
|
|
|
|
|
|
|
protected |
|
|
|
|
|
|
|
attr_accessor :binding |
|
|
|
end |
|
|
|
end |