# File lib/wikicloth/wiki_buffer/var.rb, line 47
  def to_html
    return "" if will_not_be_rendered

    if self.is_function?
      if Extension.function_exists?(function_name)
        return Extension.functions[function_name][:klass].new(@options).instance_exec( params.collect { |p| p.strip }, &Extension.functions[function_name][:block] ).to_s
      end
      ret = default_functions(function_name,params.collect { |p| p.strip })
      ret ||= @options[:link_handler].function(function_name, params.collect { |p| p.strip })
      ret.to_s
    elsif self.is_param?
      ret = nil
      @options[:buffer].buffers.reverse.each do |b|
        ret = b.get_param(params[0],params[1]) if b.instance_of?(WikiBuffer::HTMLElement) && b.element_name == "template"
        break unless ret.nil?
      end
      ret.to_s
    else
      # put template at beginning of buffer
      template_stack = @options[:buffer].buffers.collect { |b| b.get_param("__name") if b.instance_of?(WikiBuffer::HTMLElement) && 
        b.element_name == "template" }.compact
      if template_stack.last == params[0]
        debug_tree = @options[:buffer].buffers.collect { |b| b.debug }.join("-->")
        "<span class=\"error\">#{I18n.t('template loop detected', :tree => debug_tree)}</span>"
      else
        key = params[0].to_s.strip
        key_options = params[1..-1].collect { |p| p.is_a?(Hash) ? { :name => p[:name].strip, :value => p[:value].strip } : p.strip }
        key_options ||= []
        key_digest = Digest::MD5.hexdigest(key_options.to_a.sort {|x,y| (x.is_a?(Hash) ? x[:name] : x) <=> (y.is_a?(Hash) ? y[:name] : y) }.inspect)

        return @options[:params][key] if @options[:params].has_key?(key)
        # if we have a valid cache fragment use it
        return @options[:cache][key][key_digest] unless @options[:cache].nil? || @options[:cache][key].nil? || @options[:cache][key][key_digest].nil?

        ret = @options[:link_handler].include_resource(key,key_options).to_s

        ret.gsub!(/<!--(.|\s)*?-->/,"")
        count = 0
        tag_attr = key_options.collect { |p|
          if p.instance_of?(Hash)
            "#{p[:name]}=\"#{p[:value].gsub(/"/,'&quot;')}\""
          else
            count += 1
            "#{count}=\"#{p.gsub(/"/,'&quot;')}\""
          end
        }.join(" ")

        self.data = ret.blank? ? "" : "<template __name=\"#{key}\" __hash=\"#{key_digest}\" #{tag_attr}>#{ret}</template>"
        ""
      end
    end
  end