Class: Philiprehberger::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/template.rb,
lib/philiprehberger/template/cache.rb,
lib/philiprehberger/template/parser.rb,
lib/philiprehberger/template/context.rb,
lib/philiprehberger/template/filters.rb,
lib/philiprehberger/template/version.rb,
lib/philiprehberger/template/renderer.rb

Defined Under Namespace

Modules: Filters Classes: Cache, Context, Parser, Renderer, UndefinedFilterError, UndefinedVariableError

Constant Summary collapse

VERSION =
'0.6.0'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, strict: false) ⇒ Template

Returns a new instance of Template.



90
91
92
93
94
# File 'lib/philiprehberger/template.rb', line 90

def initialize(source, strict: false)
  @source = source
  @strict = strict
  @tree = Parser.new(source).parse
end

Class Attribute Details

.cacheObject (readonly)

Returns the value of attribute cache.



37
38
39
# File 'lib/philiprehberger/template.rb', line 37

def cache
  @cache
end

.layoutsObject (readonly)

Returns the value of attribute layouts.



37
38
39
# File 'lib/philiprehberger/template.rb', line 37

def layouts
  @layouts
end

.partialsObject (readonly)

Returns the value of attribute partials.



37
38
39
# File 'lib/philiprehberger/template.rb', line 37

def partials
  @partials
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



30
31
32
# File 'lib/philiprehberger/template.rb', line 30

def source
  @source
end

#treeObject (readonly)

Returns the value of attribute tree.



30
31
32
# File 'lib/philiprehberger/template.rb', line 30

def tree
  @tree
end

Class Method Details

.clear_cache!Object



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

def clear_cache!
  @cache.clear
end

.clear_layouts!Object



55
56
57
# File 'lib/philiprehberger/template.rb', line 55

def clear_layouts!
  @layouts = {}
end

.clear_partials!Object



47
48
49
# File 'lib/philiprehberger/template.rb', line 47

def clear_partials!
  @partials = {}
end

.compile(source, strict: false) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/philiprehberger/template.rb', line 75

def compile(source, strict: false)
  cache_key = [source, strict]
  cached = @cache.fetch(cache_key)
  return cached if cached

  template = new(source, strict: strict)
  @cache.store(cache_key, template)
  template
end

.from_file(path, strict: false) ⇒ Object



39
40
41
# File 'lib/philiprehberger/template.rb', line 39

def from_file(path, strict: false)
  new(File.read(path), strict: strict)
end

.register_layout(name, source) ⇒ Object



51
52
53
# File 'lib/philiprehberger/template.rb', line 51

def register_layout(name, source)
  @layouts[name.to_s] = source
end

.register_partial(name, source) ⇒ Object



43
44
45
# File 'lib/philiprehberger/template.rb', line 43

def register_partial(name, source)
  @partials[name.to_s] = source
end

.registered_filtersArray<String>

Names of every custom filter currently registered via Philiprehberger::Template::Filters.register. Built-in filters are not listed.

Returns:

  • (Array<String>)

    custom filter names



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

def registered_filters
  Filters.registered_custom
end

.registered_layoutsObject



63
64
65
# File 'lib/philiprehberger/template.rb', line 63

def registered_layouts
  @layouts.keys
end

.registered_partialsObject



59
60
61
# File 'lib/philiprehberger/template.rb', line 59

def registered_partials
  @partials.keys
end

Instance Method Details

#render(variables = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/philiprehberger/template.rb', line 100

def render(variables = {})
  ctx = Context.new(variables)
  Renderer.new(
    @tree,
    ctx,
    partials: self.class.partials,
    layouts: self.class.layouts,
    strict: @strict
  ).render
end

#strict?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/philiprehberger/template.rb', line 96

def strict?
  @strict
end