Methods
C
E
N
T
Constants
Statement = Struct.new :type, :value
Class Public methods
new(result_variable, continue_result)

Transforms this program into Ruby code which uses the given variable name as the evaluation buffer.

If continue_result is true, the evaluation buffer is reused if it already exists in the rendering context.

# File lib/ember/template.rb, line 435
      def initialize result_variable, continue_result
        @result_variable = result_variable
        @continue_result = continue_result
        @source_lines = [] # each line is composed of multiple statements
      end
Instance Public methods
code(value)

Schedules the given Ruby code to be evaluated when this program is run.

# File lib/ember/template.rb, line 484
      def code value
        statement :code, value
      end
compile()

Transforms this program into executable Ruby source code.

# File lib/ember/template.rb, line 529
      def compile
        '(%s %s []; %s; %s.join)' % [
          @result_variable,
          @continue_result ? '||=' : '=',

          @source_lines.map do |source_line|
            compiled_line = []
            combine_prev = false

            source_line.each do |stmt|
              is_code = stmt.type == :code
              is_expr = stmt.type == :expr

              if is_code
                compiled_line << stmt.value
                combine_prev = false

              else
                code =
                  if is_expr
                    " << (#{stmt.value})"
                  else
                    " << #{stmt.value.inspect}"
                  end

                if combine_prev
                  compiled_line.last << code
                else
                  compiled_line << @result_variable.to_s + code
                end

                combine_prev = true
              end
            end

            compiled_line.join('; ')

          end.join("\n"),

          @result_variable,
        ]
      end
emit_end()

Inserts an <% end %> directive before the oldest non-whitespace statement possible.

Preceding lines that only emit whitespace are skipped.

# File lib/ember/template.rb, line 502
      def emit_end
        ending  = Statement.new(:code, :end)
        current = insertion_point

        can_skip_line = lambda do |line|
          line.empty? ||
          line.all? {|stmt| stmt.type == :text && stmt.value =~ /\A\s*\z/ }
        end

        if can_skip_line[current]
          target = current

          # skip past empty whitespace in previous lines
          @source_lines.reverse_each do |line|
            break unless can_skip_line[line]
            target = line
          end

          target.unshift ending
        else
          current.push ending
        end
      end
empty?()

Returns true if there are no source lines in this program.

# File lib/ember/template.rb, line 444
      def empty?
        @source_lines.empty?
      end
expr(value)

Schedules the given Ruby code to be evaluated and inserted into the evaluation buffer when this program is run.

# File lib/ember/template.rb, line 492
      def expr value
        statement :expr, value
      end
new_line()

Begins a new line in the program’s source code.

# File lib/ember/template.rb, line 451
      def new_line
        @source_lines << []
      end
new_line?()

Returns true if a new (blank) line is ready in the program’s source code.

# File lib/ember/template.rb, line 459
      def new_line?
        ary = insertion_point
        ary.empty? || ary.all? {|stmt| stmt.type == :code }
      end
text(value)

Schedules the given text to be inserted verbatim into the evaluation buffer when this program is run.

# File lib/ember/template.rb, line 468
      def text value
        # don't bother emitting empty strings
        return if value.empty?

        # combine adjacent statements to reduce code size
        if prev = insertion_point.last and prev.type == :text
          prev.value << value
        else
          statement :text, value
        end
      end