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:, fields:, schema: nil, scope: {}, templating_config: nil) ⇒ Object
Compiles templated fields in a data structure.
-
.render_field_if_template(val, context = {}) ⇒ Object
Renders a field if it is a template.
- .resolve_templating_config(templating_config, schema: nil) ⇒ Object
Class Method Details
.compile_templated_fields!(data:, fields:, schema: nil, scope: {}, templating_config: nil) ⇒ 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 |
# File 'lib/sourcerer/templating.rb', line 155 def self.compile_templated_fields! data:, fields:, schema: nil, scope: {}, templating_config: nil fields.each do |field_entry| key = field_entry[:key] 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 = resolve_templating_config(templating_config, schema: schema) 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.
193 194 195 196 197 198 199 |
# File 'lib/sourcerer/templating.rb', line 193 def self.render_field_if_template val, context = {} if val.respond_to?(:templated?) && val.templated? val.render(context) else val end end |
.resolve_templating_config(templating_config, schema: nil) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sourcerer/templating.rb', line 177 def self.resolve_templating_config templating_config, schema: nil return templating_config.call if templating_config.respond_to?(:call) return templating_config if templating_config.is_a?(Hash) if schema.is_a?(Hash) schema_config = schema['templating'] || schema[:templating] return schema_config if schema_config.is_a?(Hash) end { 'default' => 'liquid', 'delay' => false } end |