Class: Texas::Template::Runner::Base

Inherits:
Object
  • Object
show all
Includes:
OutputHelper, Helper::Base, Helper::Info
Defined in:
lib/texas/template/runner/base.rb

Overview

Runs and renders a template.

Direct Known Subclasses

Markdown, TeX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Info

#abstract, #info, #marked_for_rewrite?, #summary

Methods included from Helper::Base

#default_search_paths, #filename_for_find, #find_template_file, #find_template_file!, #partial, #path_with_templates_basename, #render, #render_as_array, #template_extensions, #templates_by_glob

Methods included from OutputHelper

#trace, #verbose, #warning

Constructor Details

#initialize(_filename, _build) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
# File 'lib/texas/template/runner/base.rb', line 22

def initialize(_filename, _build)
  self.filename = _filename
  self.build = _build
  self.content = File.read(filename)
  @output_filename = filename.gsub(/(\.erb)$/, '')
end

Instance Attribute Details

#buildObject

Returns the build object



14
15
16
# File 'lib/texas/template/runner/base.rb', line 14

def build
  @build
end

#contentObject

Returns the original content of the template



17
18
19
# File 'lib/texas/template/runner/base.rb', line 17

def content
  @content
end

#filenameObject

Returns the location of the template (in the build directory)



20
21
22
# File 'lib/texas/template/runner/base.rb', line 20

def filename
  @filename
end

Instance Method Details

#__path__Object

Returns this template’s path.



37
38
39
# File 'lib/texas/template/runner/base.rb', line 37

def __path__
  File.dirname filename
end

#__render__(locals = {}) ⇒ Object

Renders the template into a String.



81
82
83
84
85
86
87
88
# File 'lib/texas/template/runner/base.rb', line 81

def __render__(locals = {})
  @locals = OpenStruct.new(locals)
  ERB.new(@content, nil, nil, "@erbout").result(binding)
rescue TemplateError => ex
  raise ex
rescue StandardError => ex
  raise TemplateError.new(self, ex.message, ex)
end

#__run__(locals = {}) ⇒ Object

Runs the template with the given local variables.



92
93
94
95
96
97
98
99
100
# File 'lib/texas/template/runner/base.rb', line 92

def __run__(locals = {})
  verbose { TraceInfo.new(:template, filename.gsub(build_path, ''), :dark) }

  old_current_template = build.current_template
  build.current_template = self
  @output = after_render __render__(locals)
  build.current_template = old_current_template
  @output
end

#after_render(str) ⇒ Object

Called after __render__. Can be overriden to modify the rendered contents of the template.



110
111
112
# File 'lib/texas/template/runner/base.rb', line 110

def after_render(str)
  str
end

#after_writeObject

Called after write.



104
105
# File 'lib/texas/template/runner/base.rb', line 104

def after_write
end

#append_to_output(str) ⇒ Object



75
76
77
# File 'lib/texas/template/runner/base.rb', line 75

def append_to_output(str)
  @erbout << str
end

#build_pathObject

Shorthand to the build’s __path__.



31
32
33
# File 'lib/texas/template/runner/base.rb', line 31

def build_path
  build.__path__
end

#documentObject

Shorthand to the build’s config’s document object. Can be used inside templates to display config information.

Example:

<%= document.title %>


47
48
49
# File 'lib/texas/template/runner/base.rb', line 47

def document
  build.config.document
end

#oObject

Shorthand to the template’s locals object. Can be used inside templates.

Example:

# some_template.tex.erb

Value: <%= o.some_value %>

# contents.tex.erb

<%= render :some_template, :some_value => 42

# => "Value: 42"


71
72
73
# File 'lib/texas/template/runner/base.rb', line 71

def o
  @locals
end

#storeObject

Shorthand to the build’s store object. Can be used inside templates to store information.



54
55
56
# File 'lib/texas/template/runner/base.rb', line 54

def store
  build.store
end

#writeObject

Runs the template and writes it to disk afterwards.



116
117
118
119
120
# File 'lib/texas/template/runner/base.rb', line 116

def write
  __run__
  File.open(@output_filename, 'w') {|f| f.write(@output) }
  after_write
end