Class: EJX::Template::Multitemplate

Inherits:
Subtemplate show all
Defined in:
lib/ejx/template/multitemplate.rb

Instance Attribute Summary

Attributes inherited from Subtemplate

#modifiers

Attributes inherited from Node

#children

Instance Method Summary collapse

Methods inherited from Subtemplate

#arrow_function?, #balance, #balanced?, #ending_balance, #function?, #initialize, #push, #to_sub_js

Methods inherited from Node

#initialize, #push

Constructor Details

This class inherits a constructor from EJX::Template::Subtemplate

Instance Method Details

#to_js(indentation: 4, var_generator: nil, append: "__output", promises: '__promises') ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ejx/template/multitemplate.rb', line 3

def to_js(indentation: 4, var_generator: nil, append: "__output", promises: '__promises')
  already_assigned = @children.first =~ /\A\s*(var|const|let)\s+(\S+)/
  output_var = $2
  output = ""
  if already_assigned# || !@append
    output << "#{' '*indentation}#{@children.first}\n"
  else
    output << "#{' '*indentation}__ejx_append("
    output << @children.first << "\n"
  end
  
  @children[1..-2].each do |child|
    output << case child
    when EJX::Template::String
      "#{' '*(indentation+4)}__ejx_append(#{child.to_js}, #{append}, 'unescape', __promises);\n"
    when String
      "#{' '*(indentation)}#{child}\n"
    when EJX::Template::Subtemplate
      child.to_sub_js(indentation: indentation + 4, var_generator: var_generator, promises: promises)
    else
      child.to_js(indentation: indentation + 4, var_generator: var_generator, append: append)
    end
  end
  
  output << ' '*indentation << @children.last.strip.delete_suffix(';')
  if already_assigned
    output << ";\n#{' '*indentation}__ejx_append(#{output_var}, #{append}, 'escape', __promises);\n"
  else
    output << ", #{append}, 'escape', __promises);\n"
  end

  output
end