Class: ORB::Temple::Filters

Inherits:
Temple::Filter
  • Object
show all
Defined in:
lib/orb/temple/filters.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Filters

Returns a new instance of Filters.



6
7
8
9
# File 'lib/orb/temple/filters.rb', line 6

def initialize(options = {})
  @options = options
  @attributes_compiler = AttributesCompiler.new
end

Instance Method Details

#on_orb_component(node, content = []) ⇒ Array

Handle a component tag expression ‘[:orb, :component, name, attributes, content]`

Parameters:

  • name (String)

    The name of the component

  • attributes (Array<ORB::AST::Attribute>)

    The attributes to be passed in to the component

  • content (Array) (defaults to: [])

    (optional) Temple expression

Returns:

  • (Array)

    compiled Temple core expression



27
28
29
30
31
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
58
59
60
# File 'lib/orb/temple/filters.rb', line 27

def on_orb_component(node, content = [])
  tmp = unique_name

  # Lookup the component class name using the ORB lookup mechanism
  # that traverses the configured namespaces
  name = node.tag.gsub('.', '::')
  komponent = ORB.lookup_component(name)
  komponent_name = komponent || name

  block_name = "__orb__#{komponent_name.rpartition('::').last.underscore}"
  block_name = node.directives.fetch(:with, block_name)

  # We need to compile the attributes into a set of captures and a set of arguments
  # since arguments passed to the view component constructor may be defined as
  # dynamic expressions in our template, and we need to first capture their results.
  arg_captures = @attributes_compiler.compile_captures(node.attributes, tmp)
  args = @attributes_compiler.compile_komponent_args(node.attributes, tmp)

  # Construct the render call for the view component
  code = "render #{komponent_name}.new(#{args}) do |#{block_name}|"

  # Return a compiled Temple expression that captures the component render call
  # and then evaluates the result into the OutputBuffer.
  [:multi,
    *arg_captures,
    # Capture the result of the component render call into a variable
    # we can't do :dynamic here because it's probably not a complete expression
    [:block, "#{tmp} = #{code}",
      # Capture the content of the block into a separate buffer
      # [:capture, unique_name, compile(content)]
      compile(content)],
    # Output the content
    [:escape, true, [:dynamic, tmp.to_s]]]
end

#on_orb_dynamic(node, content) ⇒ Object

Handle a dynamic node expression ‘[:orb, :dynamic, node, content]`



120
121
122
123
124
125
126
127
128
129
# File 'lib/orb/temple/filters.rb', line 120

def on_orb_dynamic(node, content)
  # TODO: Determine whether the node is an html_tag, component, or slot node
  tmp = unique_name
  splats = @attributes_compiler.compile_splat_attributes(node.splat_attributes)
  code = "content_tag('#{node.tag}', #{splats}) do"

  [:multi,
    [:block, "#{tmp} = #{code}", compile(content)],
    [:escape, true, [:dynamic, tmp]]]
end

#on_orb_for(expression, content) ⇒ Array

Handle a for block expression ‘[:orb, :for, expression, content]`

Parameters:

  • expression (String)

    The iterator expression to be evaluated

  • content (Array)

    The content to be rendered for each iteration

Returns:

  • (Array)

    compiled Temple expression



109
110
111
112
113
114
115
116
# File 'lib/orb/temple/filters.rb', line 109

def on_orb_for(expression, content)
  enumerator, collection = expression.split(' in ')
  code = "#{collection}.each do |#{enumerator}|"

  [:multi,
    [:code, code], compile(content),
    [:code, "end"]]
end

#on_orb_if(condition, yes, no = nil) ⇒ Array

Handle an if block expression ‘[:orb, :if, condition, yes, no]`

Parameters:

  • condition (String)

    The condition to be evaluated

  • yes (Array)

    The content to be rendered if the condition is true

  • no (Array) (defaults to: nil)

    (optional) The content to be rendered if the condition is false

Returns:

  • (Array)

    compiled Temple expression



98
99
100
101
102
# File 'lib/orb/temple/filters.rb', line 98

def on_orb_if(condition, yes, no = nil)
  result = [:if, condition, compile(yes)]
  result << compile(no) if no
  result
end

#on_orb_slot(node, content = []) ⇒ Array

Handle a component slot tag expression ‘[:orb, :slot, name, attributes, content]`

Parameters:

  • name (String)

    The name of the slot

  • attributes (Array<ORB::AST::Attribute>)

    the attributes to be passed in to the slot

  • content (Array) (defaults to: [])

    (optional) Temple expression for the slot content

Returns:

  • (Array)

    compiled Temple expression



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/orb/temple/filters.rb', line 68

def on_orb_slot(node, content = [])
  tmp = unique_name

  # We need to compile the attributes into a set of captures and a set of arguments
  # since arguments passed to the view component constructor may be defined as
  # dynamic expressions in our template, and we need to first capture their results.
  arg_captures = @attributes_compiler.compile_captures(node.attributes, tmp)
  args = @attributes_compiler.compile_komponent_args(node.attributes, tmp)

  # Prepare the slot name, parent name, and block name
  slot_name = node.slot
  parent_name = "__orb__#{node.component.underscore}"
  block_name = node.directives.fetch(:with, "__orb__#{slot_name}")

  # Construct the code to call the slot on the parent component
  code = "#{parent_name}.with_#{slot_name}(#{args}) do |#{block_name}|"

  # Return a compiled Temple expression that captures the slot call
  [:multi,
    *arg_captures,
    [:code, code], compile(content),
    [:code, "end"]]
end

#on_orb_tag(name, attributes, content = nil) ⇒ Array

Handle an HTML tag expression ‘[:orb, :tag, name, attributes, content]`

Parameters:

  • name (String)

    The name of the tag

  • attributes (Array)

    The attributes to be passed in to the tag

  • content (Array) (defaults to: nil)

    (optional) child nodes of the tag

Returns:

  • (Array)

    compiled Temple core expression



17
18
19
# File 'lib/orb/temple/filters.rb', line 17

def on_orb_tag(name, attributes, content = nil)
  [:html, :tag, name, @attributes_compiler.compile_attributes(attributes), compile(content)]
end