Module: Sourcerer::Templating
- Defined in:
- lib/sourcerer/templating.rb
Overview
This module provides the core templating functionality for Sourcerer.
It includes modules for template engines, and classes for representing
templated fields and their context.
Defined Under Namespace
Modules: Engines Classes: Context, TemplatedField
Class Method Summary collapse
-
.compile_templated_fields!(data:, schema:, fields:, scope: {}) ⇒ Object
Compiles templated fields in a data structure.
-
.render_field_if_template(val, context = {}) ⇒ Object
Renders a field if it is a template.
Class Method Details
.compile_templated_fields!(data:, schema:, fields:, scope: {}) ⇒ Object
Compiles templated fields in a data structure.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/sourcerer/templating.rb', line 155 def self.compile_templated_fields! data:, schema:, fields:, scope: {} fields.each do |field_entry| key = field_entry[:key] path = field_entry[:path] val = data[key] next unless val.is_a?(String) || (val.is_a?(Hash) && val['__tag__'] && val['value']) raw = val.is_a?(Hash) ? val['value'] : val tagged = val.is_a?(Hash) config = SchemaGraphy::SchemaUtils.templating_config_for(schema, path) engine = tagged ? val['__tag__'] : (config['default'] || 'liquid') compiled = Engines.compile(raw, engine) data[key] = if config['delay'] TemplatedField.new(raw, compiled, engine, tagged, inferred: !tagged) else Engines.render(compiled, engine, scope) end end end |
.render_field_if_template(val, context = {}) ⇒ Object
Renders a field if it is a template.
182 183 184 185 186 187 188 |
# File 'lib/sourcerer/templating.rb', line 182 def self.render_field_if_template val, context = {} if val.respond_to?(:templated?) && val.templated? val.render(context) else val end end |