Class: Luoma::Template
- Inherits:
-
Object
- Object
- Luoma::Template
- Defined in:
- lib/luoma/template.rb,
sig/luoma/template.rbs
Instance Attribute Summary collapse
-
#env ⇒ Environment
readonly
Returns the value of attribute env.
-
#globals ⇒ t_namespace?
readonly
Returns the value of attribute globals.
-
#name ⇒ String
readonly
Returns the value of attribute name.
-
#nodes ⇒ t_block
readonly
Returns the value of attribute nodes.
-
#overlay ⇒ t_namespace?
readonly
Returns the value of attribute overlay.
-
#source ⇒ String
readonly
Returns the value of attribute source.
-
#up_to_date ⇒ Object
readonly
Returns the value of attribute up_to_date.
Instance Method Summary collapse
-
#analyze(include_partials: false) ⇒ Luoma::StaticAnalysis::Result
Statically analyze this template and report variable, tag and filter usage.
-
#comments ⇒ Array[Comment]
Return an array of comment nodes found in this template.
-
#filter_names(include_partials: false) ⇒ Array[String]
Return the names of all filters used in this template.
-
#global_variable_paths(include_partials: false) ⇒ Array[String]
Return an array of global variables used in this template, including path segments.
-
#global_variable_segments(include_partials: false) ⇒ Array[untyped]
Return an array of global variables used in this template, each as an array of segments.
-
#global_variables(include_partials: false) ⇒ Array[String]
Return an array of global variables used in this template, without path segments.
-
#initialize(env, source, nodes, globals: nil, name: nil, overlay: nil, up_to_date: nil) ⇒ Template
constructor
(Environment, String, t_block, ?globals: t_namespace?, ?name: String?, ?overlay: t_namespace?, ?up_to_date: Proc::_Callable?) -> void.
-
#lines ⇒ Array[String]
Return this template's source code split into lines.
-
#make_globals(namespace) ⇒ t_namespace?
(t_namespace?) -> t_namespace?.
-
#render(data = nil) ⇒ String
(t_namespace?) -> String.
-
#render_with_context(context, buffer, isolated: true) ⇒ String
(RenderContext, String, ?isolated: bool) -> String.
-
#tag_names(include_partials: false) ⇒ Array[String]
Return the names of all tags used in this template.
-
#up_to_date? ⇒ Boolean
Return
falseif this template is stale and needs to be loaded again. -
#variable_paths(include_partials: false) ⇒ Array[String]
Return an array of variables used in this template, including path segments.
-
#variable_segments(include_partials: false) ⇒ Array[untyped]
Return an array of variables used in this template, each as an array of segments.
-
#variables(include_partials: false) ⇒ Array[String]
Return an array of variable names used in this template, without path segments.
-
#with_globals(globals) ⇒ Template
(t_namespace) -> Template.
Constructor Details
#initialize(env, source, nodes, globals: nil, name: nil, overlay: nil, up_to_date: nil) ⇒ Template
(Environment, String, t_block, ?globals: t_namespace?, ?name: String?, ?overlay: t_namespace?, ?up_to_date: Proc::_Callable?) -> void
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/luoma/template.rb', line 14 def initialize(env, source, nodes, globals: nil, name: nil, overlay: nil, up_to_date: nil) @env = env @source = source @nodes = nodes @globals = globals @name = name || "" @overlay = @up_to_date = up_to_date @lines = nil end |
Instance Attribute Details
#env ⇒ Environment (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/luoma/template.rb', line 5 def env @env end |
#globals ⇒ t_namespace? (readonly)
Returns the value of attribute globals.
5 6 7 |
# File 'lib/luoma/template.rb', line 5 def globals @globals end |
#name ⇒ String (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/luoma/template.rb', line 5 def name @name end |
#nodes ⇒ t_block (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/luoma/template.rb', line 5 def nodes @nodes end |
#overlay ⇒ t_namespace? (readonly)
Returns the value of attribute overlay.
5 6 7 |
# File 'lib/luoma/template.rb', line 5 def @overlay end |
#source ⇒ String (readonly)
Returns the value of attribute source.
5 6 7 |
# File 'lib/luoma/template.rb', line 5 def source @source end |
#up_to_date ⇒ Object (readonly)
Returns the value of attribute up_to_date.
5 6 7 |
# File 'lib/luoma/template.rb', line 5 def up_to_date @up_to_date end |
Instance Method Details
#analyze(include_partials: false) ⇒ Luoma::StaticAnalysis::Result
Statically analyze this template and report variable, tag and filter usage.
(?include_partials: bool) -> Luoma::StaticAnalysis::Result
73 74 75 |
# File 'lib/luoma/template.rb', line 73 def analyze(include_partials: false) Luoma::StaticAnalysis.analyze(self, include_partials: include_partials) end |
#comments ⇒ Array[Comment]
Return an array of comment nodes found in this template.
Comment nodes have token and text attributes. Use template.comments.map(&:text)
to get an array of comment strings. Each comment string includes leading and trailing
whitespace.
Note that this method does not try to load included or render templates when looking. for comment nodes.
() -> Array
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/luoma/template.rb', line 143 def comments context = RenderContext.new(self) nodes = [] # : Array[Comment] # @type var visit: ^(Markup) -> void visit = lambda do |node| nodes << node if node.is_a?(Comment) node.children(context).each do |child| visit.call(child) end end @nodes.each { |node| visit.call(node) unless node.is_a?(String) } nodes end |
#filter_names(include_partials: false) ⇒ Array[String]
Return the names of all filters used in this template.
(?include_partials: bool) -> Array
122 123 124 |
# File 'lib/luoma/template.rb', line 122 def filter_names(include_partials: false) analyze(include_partials: include_partials).filters.keys end |
#global_variable_paths(include_partials: false) ⇒ Array[String]
Return an array of global variables used in this template, including path segments.
(?include_partials: bool) -> Array
108 109 110 |
# File 'lib/luoma/template.rb', line 108 def global_variable_paths(include_partials: false) analyze(include_partials: include_partials).globals.values.flatten.map { |v| v[:path] }.uniq end |
#global_variable_segments(include_partials: false) ⇒ Array[untyped]
Return an array of global variables used in this template, each as an array of segments.
(?include_partials: bool) -> Array
115 116 117 |
# File 'lib/luoma/template.rb', line 115 def global_variable_segments(include_partials: false) analyze(include_partials: include_partials).globals.values.flatten.map { |v| v[:segments] }.uniq end |
#global_variables(include_partials: false) ⇒ Array[String]
Return an array of global variables used in this template, without path segments.
(?include_partials: bool) -> Array
101 102 103 |
# File 'lib/luoma/template.rb', line 101 def global_variables(include_partials: false) analyze(include_partials: include_partials).globals.keys end |
#lines ⇒ Array[String]
Return this template's source code split into lines.
60 61 62 |
# File 'lib/luoma/template.rb', line 60 def lines @lines ||= @source.lines(chomp: false) end |
#make_globals(namespace) ⇒ t_namespace?
(t_namespace?) -> t_namespace?
166 167 168 169 170 171 172 173 174 |
# File 'lib/luoma/template.rb', line 166 def make_globals(namespace) return @globals if namespace.nil? && @overlay.nil? namespace_ = {} #: t_namespace namespace_.merge!(@globals) if @globals # steep:ignore namespace_.merge!(@overlay) if @overlay # steep:ignore namespace_.merge!(namespace) if namespace namespace_ end |
#render(data = nil) ⇒ String
(t_namespace?) -> String
27 28 29 30 31 |
# File 'lib/luoma/template.rb', line 27 def render(data = nil) buffer = +"" context = RenderContext.new(self, globals: make_globals(data)) render_with_context(context, buffer) end |
#render_with_context(context, buffer, isolated: true) ⇒ String
(RenderContext, String, ?isolated: bool) -> String
40 41 42 43 |
# File 'lib/luoma/template.rb', line 40 def render_with_context(context, buffer, isolated: true) Luoma.render_block(@nodes, context, buffer, root: isolated) buffer end |
#tag_names(include_partials: false) ⇒ Array[String]
Return the names of all tags used in this template.
(?include_partials: bool) -> Array
129 130 131 |
# File 'lib/luoma/template.rb', line 129 def tag_names(include_partials: false) analyze(include_partials: include_partials)..keys end |
#up_to_date? ⇒ Boolean
Return false if this template is stale and needs to be loaded again.
nil is returned if an up_to_date proc is not available.
66 67 68 |
# File 'lib/luoma/template.rb', line 66 def up_to_date? @up_to_date&.call end |
#variable_paths(include_partials: false) ⇒ Array[String]
Return an array of variables used in this template, including path segments.
(?include_partials: bool) -> Array
87 88 89 |
# File 'lib/luoma/template.rb', line 87 def variable_paths(include_partials: false) analyze(include_partials: include_partials).variables.values.flatten.map { |v| v[:path] }.uniq end |
#variable_segments(include_partials: false) ⇒ Array[untyped]
Return an array of variables used in this template, each as an array of segments.
(?include_partials: bool) -> Array
94 95 96 |
# File 'lib/luoma/template.rb', line 94 def variable_segments(include_partials: false) analyze(include_partials: include_partials).variables.values.flatten.map { |v| v[:segments] }.uniq end |
#variables(include_partials: false) ⇒ Array[String]
Return an array of variable names used in this template, without path segments.
(?include_partials: bool) -> Array
80 81 82 |
# File 'lib/luoma/template.rb', line 80 def variables(include_partials: false) analyze(include_partials: include_partials).variables.keys end |