Class: Twig::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader, options = {}) ⇒ Environment



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/twig/environment.rb', line 9

def initialize(loader, options = {})
  @loader = loader
  @extension_set = ExtensionSet.new
  @options = {
    cache: false,
    debug: false,
    auto_reload: nil,
  }.merge(options)

  @auto_reload = options[:auto_reload].nil? ? options[:debug] : options[:auto_reload]

  self.cache = @options[:cache]

  add_extension(Extension::Core.new)
end

Instance Attribute Details

#cacheCache::Base



6
7
8
# File 'lib/twig/environment.rb', line 6

def cache
  @cache
end

Instance Method Details

#add_extension(extension) ⇒ Object



74
75
76
# File 'lib/twig/environment.rb', line 74

def add_extension(extension)
  @extension_set.add(extension)
end

#compile(node) ⇒ Object



110
111
112
# File 'lib/twig/environment.rb', line 110

def compile(node)
  compiler.compile(node).source
end

#compile_source(source) ⇒ Object



115
116
117
# File 'lib/twig/environment.rb', line 115

def compile_source(source)
  compile(parse(tokenize(source)))
end

#extension(name) ⇒ Object



69
70
71
# File 'lib/twig/environment.rb', line 69

def extension(name)
  @extension_set.extensions[name]
end

#filter(name) ⇒ TwigFilter



84
85
86
# File 'lib/twig/environment.rb', line 84

def filter(name)
  @extension_set.filter(name)
end

#helper_method?(name) ⇒ Boolean



94
95
96
# File 'lib/twig/environment.rb', line 94

def helper_method?(name)
  @extension_set.helper_methods.include?(name)
end

#load_and_compile(name) ⇒ String



120
121
122
123
# File 'lib/twig/environment.rb', line 120

def load_and_compile(name)
  source = loader.get_source_context(name)
  compile_source(source)
end

#load_template(name) ⇒ Twig::Template



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twig/environment.rb', line 32

def load_template(name, **)
  class_name = template_class(name)
  cache_key = cache.generate_key(name, class_name)

  attempt_cache = !@auto_reload || template_fresh?(name, cache.timestamp(cache_key))

  if attempt_cache
    @cache.load(cache_key)
  end

  # Cache didn't load a class or we should load fresh
  unless attempt_cache && Twig.const_defined?(class_name)
    code = render_ruby(name)

    # File cache loader won't rely on eval
    @cache.write(cache_key, code)
    @cache.load(cache_key)

    # Finally just eval the generated code if cache does
    # create the class
    Twig.module_eval(code) unless Twig.const_defined?(class_name)
  end

  Twig.const_get(template_class(name)).
    new(self, **)
end

#operatorsArray



79
80
81
# File 'lib/twig/environment.rb', line 79

def operators
  @extension_set.operators
end

#parse(stream) ⇒ Object



105
106
107
# File 'lib/twig/environment.rb', line 105

def parse(stream)
  parser.parse(stream)
end

#render(name) ⇒ Object



59
60
61
# File 'lib/twig/environment.rb', line 59

def render(name)
  loader.get_source_context(name).code
end

#render_ruby(name) ⇒ Object



63
64
65
66
67
# File 'lib/twig/environment.rb', line 63

def render_ruby(name)
  compile_source(
    loader.get_source_context(name)
  )
end

#template_class(name) ⇒ Object



25
26
27
28
29
# File 'lib/twig/environment.rb', line 25

def template_class(name)
  key = loader.get_cache_key(name)

  "Compiled::Template_#{::Digest::SHA256.hexdigest(key)}"
end

#template_fresh?(name, time) ⇒ Boolean



127
128
129
130
# File 'lib/twig/environment.rb', line 127

def template_fresh?(name, time)
  # @todo check extension set last modified
  loader.fresh?(name, time)
end

#token_parser(name) ⇒ TokenParser::Base



89
90
91
# File 'lib/twig/environment.rb', line 89

def token_parser(name)
  @extension_set.token_parser(name)
end

#tokenize(source) ⇒ TokenStream



100
101
102
# File 'lib/twig/environment.rb', line 100

def tokenize(source)
  lexer.tokenize(source)
end