class Slim::Interpolation

Perform interpolation of #{var_name} in the expressions `[:slim, :interpolate, string]`.

@api private

Public Instance Methods

on_slim_interpolate(string) click to toggle source

Handle interpolate expression `[:slim, :interpolate, string]`

@param [String] string Static interpolate @return [Array] Compiled temple expression

# File lib/slim/interpolation.rb, line 11
def on_slim_interpolate(string)
  # Interpolate variables in text (#{variable}).
  # Split the text into multiple dynamic and static parts.
  block = [:multi]
  begin
    case string
    when %r\A\\#\{/
      # Escaped interpolation
      # Use [:slim, :output] because this is used by InterpolateTiltEngine
      # to filter out protected strings (Issue #141).
      block << [:slim, :output, false, '\#{\', [:multi]]
      string = $'
    when %r\A#\{/
      # Interpolation
      string, code = parse_expression($')
      escape = code !~ %r\A\{.*\}\Z/
      block << [:slim, :output, escape, escape ? code : code[1..-2], [:multi]]
    when %r\A([#\\]|[^#\\]*)/
      # Static text
      block << [:static, $&]
      string = $'
    end
  end until string.empty?
  block
end

Protected Instance Methods

parse_expression(string) click to toggle source
# File lib/slim/interpolation.rb, line 39
def parse_expression(string)
  count, i = 1, 0
  while i < string.size && count != 0
    if string[i] == {{
      count += 1
    elsif string[i] == }}
      count -= 1
    end
    i += 1
  end

  raise "Text interpolation: Expected closing }" if count != 0

  return string[i..-1], string[0, i-1]
end