Class: Sourcerer::Templating::TemplatedField

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcerer/templating.rb

Overview

Represents a field that will be rendered by a template engine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, compiled, engine, tagged, inferred) ⇒ TemplatedField

Returns a new instance of TemplatedField.

Parameters:

  • raw (String)

    The raw template string.

  • compiled (Object)

    The compiled template object.

  • engine (String)

    The name of the template engine.

  • tagged (Boolean)

    Whether the template was explicitly tagged.

  • inferred (Boolean)

    Whether the template engine was inferred.



74
75
76
77
78
79
80
# File 'lib/sourcerer/templating.rb', line 74

def initialize raw, compiled, engine, tagged, inferred
  @raw      = raw
  @compiled = compiled
  @engine   = engine
  @tagged   = tagged
  @inferred = inferred
end

Instance Attribute Details

#compiledObject (readonly)

Returns The compiled template object.

Returns:

  • (Object)

    The compiled template object.



61
62
63
# File 'lib/sourcerer/templating.rb', line 61

def compiled
  @compiled
end

#engineString (readonly)

Returns The name of the template engine.

Returns:

  • (String)

    The name of the template engine.



63
64
65
# File 'lib/sourcerer/templating.rb', line 63

def engine
  @engine
end

#inferredBoolean (readonly)

Returns Whether the template engine was inferred.

Returns:

  • (Boolean)

    Whether the template engine was inferred.



67
68
69
# File 'lib/sourcerer/templating.rb', line 67

def inferred
  @inferred
end

#rawString (readonly)

Returns The raw, un-rendered template string.

Returns:

  • (String)

    The raw, un-rendered template string.



59
60
61
# File 'lib/sourcerer/templating.rb', line 59

def raw
  @raw
end

#taggedBoolean (readonly)

Returns Whether the template was explicitly tagged.

Returns:

  • (Boolean)

    Whether the template was explicitly tagged.



65
66
67
# File 'lib/sourcerer/templating.rb', line 65

def tagged
  @tagged
end

Instance Method Details

#deferred?Boolean

Returns True if the field is deferred (not yet compiled).

Returns:

  • (Boolean)

    True if the field is deferred (not yet compiled).



88
89
90
# File 'lib/sourcerer/templating.rb', line 88

def deferred?
  compiled.nil?
end

#render(context = {}) ⇒ String

Renders the template with the given context.

Parameters:

  • context (Hash, Liquid::Context) (defaults to: {})

    The context for rendering.

Returns:

  • (String)

    The rendered output.



100
101
102
103
# File 'lib/sourcerer/templating.rb', line 100

def render context = {}
  scope = context.respond_to?(:environments) ? context.environments.first : context
  Engines.render(compiled, engine, scope)
end

#templated?true

Returns Always returns true to indicate this is a templated field.

Returns:

  • (true)

    Always returns true to indicate this is a templated field.



83
84
85
# File 'lib/sourcerer/templating.rb', line 83

def templated?
  true
end

#to_liquidself

Returns self for Liquid compatibility.

Returns:

  • (self)

    Returns self for Liquid compatibility.



93
94
95
# File 'lib/sourcerer/templating.rb', line 93

def to_liquid
  self
end

#to_sString

Renders the template with an empty context.

Returns:

  • (String)

    The rendered output.



107
108
109
# File 'lib/sourcerer/templating.rb', line 107

def to_s
  render({})
end