Sha256: 998de0060006d947f5aa5c92631b9fde39c31448c2a3d1210f1b89ce3517854f

Contents?: true

Size: 1.52 KB

Versions: 28

Compression:

Stored size: 1.52 KB

Contents

module Tilt
  # 
  # Include it into your template context class for :output, :capture and :concat support
  # 
  module ContextExt
    def output
      _tilt_template.class.output(self)
    end
  
    def capture *args, &block
      _tilt_template.class.capture self, &block      
    end
  
    def concat value
      _tilt_template.class.concat self, value
    end
  end  
  
  class Template
    def self.get_output context
      raise "Output variable for #{self.class} not defined!"
    end
    
    def self.capture context, &block
      raise "capture not implemented for #{self.class}!"
    end
    
    def self.concat context, value
      raise "concat not implemented for #{self.class}!"
    end
  end
  
  class ERBTemplate
    def self.get_output context
      context.instance_variable_get "@output"
    end
    
    def self.capture context, &block
      begin
        old_output = context.instance_variable_get "@output"
        old_output.must_be.defined
        context.instance_variable_set "@output", ""
        block.call
        context.instance_variable_get "@output"
      ensure
        context.instance_variable_set "@output", old_output
      end      
    end
    
    def self.concat context, value
      get_output(context) << value
    end    
  end
  
  class HamlTemplate
    def self.get_output context
      context.haml_buffer.buffer
    end
    
    def self.capture context, &block
      context.capture_haml &block
    end
    
    def self.concat context, value
      context.haml_concat value
    end
  end
end

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
rad_core-0.0.17 lib/rad/template/_support/tilt.rb
rad_core-0.0.16 lib/rad/template/_support/tilt.rb
rad_core-0.0.15 lib/rad/template/_support/tilt.rb
rad_core-0.0.14 lib/rad/template/_support/tilt.rb
rad_core-0.0.13 lib/rad/template/support/tilt.rb
crystal-0.0.13 lib/crystal/template/support/tilt.rb
crystal-0.0.12 lib/crystal/template/support/tilt.rb
crystal_ext-0.0.11 lib/crystal/template/support/tilt.rb