Kiss A simpler, smarter web application framework for Ruby

Kiss::TemplateMethods Module

Public Instance Methods

data() source

Contains data set by action/mailer logic to be displayed in templates.

    # File lib/kiss/template_methods.rb, line 11
    def data
      @data
    end
    
Also aliased as: vars
data=(hash) source

Not documented

    # File lib/kiss/template_methods.rb, line 16
    def data=(hash)
      @data = hash
    end
    
database() source

Alias for db

db() source

Not documented

    # File lib/kiss/template_methods.rb, line 5
    def db
      controller.database
    end
    
Also aliased as: database
erubis(path,template_binding = binding,content = nil) source

Processes specified template file using specified binding. content is used to pass previously rendered results into wrapper templates such as layouts.

    # File lib/kiss/template_methods.rb, line 117
    def erubis(path,template_binding = binding,content = nil)
      # params note: content (if provided) is passed to erubis template via binding
      
      # set template dir to dir that contains this template
      @current_template_dir ||= @template_dir
      old_template_dir = @current_template_dir
      @current_template_dir = path.sub(/\/?[^\/]*\Z/,'')
      
      # read the file, parse into eruby source,
      # and pre-process source for Kiss additions
      eruby_src = file_cache(path) do |template|
        template ? begin
          # macros
          template.gsub!(/\<\%\s*macro\s+(\w+.*?)\s*\%\>/,'<% def \1; _buf = \'\' %>')
          template.gsub!(/\<\%\s*end\s+macro\s*\%\>/,'<% _buf; end %>')
        
          # generic loop iterator, using 'iterate'
          template.gsub!(/\<\%\s*iterate\s+(\w+)\s+(.*?)\s*\%\>/,"<% \\1 = Kiss::Iterator.new; \\2; \\1.increment %>")
          # for loop iterator
          template.gsub!(/\<\%\s*for\s+(\w+)\s+in\s+(.*?)\s*\%\>/,"<% \\1_loop = Kiss::Iterator.new(\\2); for \\1 in \\1_loop.collection; \\1_loop.increment %>")
        
          Erubis::Eruby.new(template).src
        end : nil
      end
      raise Kiss::FileNotFound, "template '#{path}' not found" unless eruby_src
      
      # evaluate source in context of template_binding
      result = eval eruby_src, template_binding, path
      
      # restore previous template dir
      @current_template_dir = old_template_dir
      
      result
    end
    
escape(string) source

Alias for url_escape

escape_html(str) source

Alias for html_escape

escape_url(string) source

Alias for url_escape

get_template_options(*args) source

Convert arguments into options hash for use get_template_path.

    # File lib/kiss/template_methods.rb, line 90
    def get_template_options(*args)
      case args.length
      when 0
        {}
      when 1
        args[0].is_a?(Hash) ? args[0] : { :template => args[0] }
      when 2
        args[1].merge( :template => args[0] )
      else
        raise 'too many arguments'
      end
    end
    
get_template_path(options = {}) source

Gets path to insert/template file from specified options.

    # File lib/kiss/template_methods.rb, line 104
    def get_template_path(options = {})
      @current_template_dir ||= @template_dir + action_subdir
      
      path = options[:template]
      extension = options[:extension]
      
      path += ".#{extension || 'rhtml'}" unless path =~ /\./
      (path =~ /\A\//) ? "#{@template_dir}#{path}" : "#{@current_template_dir}/#{path}"
    end
    
h(str) source

Alias for html_escape

html_escape(str) source

Encodes string for output to HTML.

    # File lib/kiss/template_methods.rb, line 161
    def html_escape(str)
      Kiss.html_escape(str)
    end
    
Also aliased as: h, escape_html
insert(*args) source

Reads file specified by options and return contents (without template processing).

    # File lib/kiss/template_methods.rb, line 64
    def insert(*args)
      path = get_template_path(get_template_options(*args))
      content = ::File.read(path)
    end
    
layout() source

Returns value of next layout file, which will be used to wrap the results of current rendering template. Note: @layout is deprecated for use in templates and will not be supported in Kiss 1.0. Use layout and layout= instead.

    # File lib/kiss/template_methods.rb, line 48
    def layout
      # @layout deprecated; future will be action.layout
      @layout
    end
    
layout=(new_layout) source

Sets path to next layout file, which will be used to wrap the results of current rendering template. Note: @layout is deprecated for use in templates and will not be supported in Kiss 1.0. Use layout and layout= instead.

    # File lib/kiss/template_methods.rb, line 57
    def layout=(new_layout)
      # @layout deprecated; future will be action.layout
      @layout = new_layout
    end
    
process(*args) source

Processes template specified by options and return results.

    # File lib/kiss/template_methods.rb, line 70
    def process(*args)
      options = get_template_options(*args)
      path = get_template_path(options)
      
      if (options[:data] || options[:vars])
        new_data = (options[:data] || {}).merge(options[:vars] || {})
        old_data = self.data
        self.data = self.data.merge(new_data)
      else
        old_data = nil
      end
      
      content = erubis(path)
      self.data = old_data if old_data
      
      content
    end
    
Also aliased as: process_template
process_template(*args) source

Alias for process

set(vars) source

Merges specified data (key-value pairs) into template data hash.

    # File lib/kiss/template_methods.rb, line 22
    def set(vars)
      vars.each_pair do |key,value|
        # convert symbols to strings
        @data[key] = value
      end
    end
    
title() source

Shorthand to access vars.title, here for backward compatibility. Note: @title is deprecated for use in templates and will not be supported in Kiss 1.0. Use title and title= instead.

    # File lib/kiss/template_methods.rb, line 32
    def title
      vars.title
    end
    
title=(new_title) source

Shorthand to set vars.title, here for backward compatibility. Note: @title is deprecated for use in templates and will not be supported in Kiss 1.0. Use title and title= instead.

    # File lib/kiss/template_methods.rb, line 39
    def title=(new_title)
      vars.title = new_title
      @title = new_title
    end
    
url_encode(string) source

Alias for url_escape

url_escape(string) source

Escapes string for use in URLs.

    # File lib/kiss/template_methods.rb, line 153
    def url_escape(string)
      Kiss.url_escape(str)
    end
    
Also aliased as: escape, url_encode, escape_url
vars() source

Alias for data

Disabled; run with $DEBUG to generate this.