Class: Landline::Template Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/landline/template.rb

Overview

This class is abstract.

does not represent any actual template engine.

Interface for Template engines

Instance Method Summary collapse

Constructor Details

#initialize(input, vars = {}, parent:, filename:) ⇒ Template

Returns a new instance of Template.

Parameters:

  • input (String, File)

    template text

  • vars (Hash) (defaults to: {})

    local variables for tempalte

  • parent (Landline::Node)

    parent node

  • filename (String)

    filename for eval if input is a string



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/landline/template.rb', line 40

def initialize(input, vars = {}, parent:, filename:)
  @template, @filename = if input.is_a?(File)
                           [input.read, input.path]
                         else
                           [input, filename]
                         end
  @context = TemplateContext.new(parent, self)
  @parent = parent
  input.close if input.is_a? File
  @binding = @context.binding
  vars.each do |k, v|
    @binding.local_variable_set(k, v)
  end
end

Instance Method Details

#import(file) ⇒ Landline::Template

Import a template from within current template.

Parameters:

  • file (File)

    template file to import

Returns:



92
93
94
95
96
97
98
99
# File 'lib/landline/template.rb', line 92

def import(file)
  newtemp = self.class.new(file,
                           {},
                           parent: @parent,
                           filename: file.path)
  newtemp.binding = @binding
  newtemp
end

#local_variable_get(key) ⇒ Object

Get local variable

Parameters:

  • key (Symbol)

Returns:

  • (Object)


65
66
67
# File 'lib/landline/template.rb', line 65

def local_variable_get(key)
  @binding.local_variable_get(key)
end

#local_variable_set(key, value) ⇒ Object

Set local variable

Parameters:

  • key (Symbol)
  • value (Object)


58
59
60
# File 'lib/landline/template.rb', line 58

def local_variable_set(key, value)
  @binding.local_variable_set(key, value)
end

#local_variablesArray(Symbol)

Get an array of defined local variables

Returns:

  • (Array(Symbol))


71
72
73
# File 'lib/landline/template.rb', line 71

def local_variables
  @binding.local_variables
end

#override_locals(vars) ⇒ Object

Override binding variables.

Parameters:

  • vars (Hash{Symbol => Object})


77
78
79
80
81
# File 'lib/landline/template.rb', line 77

def override_locals(vars)
  vars.each do |k, v|
    @binding.local_variable_set(k, v)
  end
end

#runObject

Note:

This method is a stub.

Run the template



85
86
87
# File 'lib/landline/template.rb', line 85

def run
  # ... (stub)
end