Sha256: 13135c47d9a73a0423c8e1cc22fb2104fef2095f233ee15c846beeb81bb81f6b

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

require 'mustache'

module Ramaze
  module View
    # Binding to Mustache templating engine.
    #
    # Mustache uses user-defined class for rendering. Ramaze overwrites value,
    # if controller defined same name variable as method that class defined.
    #
    # @see http://github.com/defunkt/mustache
    module Mustache
      def self.call(action, string)
        context, path, ext = class_defined?(action)

        action.sync_variables(action)
        action.variables.each { |k, v| context[k.to_sym] = v }

        view = View.compile(string) { |s| ::Mustache::Template.new(s) }
        html = view.render(context)

        return html, 'text/html'
      end

      def self.class_defined?(action)
        return ::Mustache::Context.new(nil), nil, nil unless action.view

        path = File.dirname(action.view)

        klass = if FileTest.exist?(File.join(path, "#{action.name}.rb"))
          require File.join(path, action.name)
          ::Object.const_get(::Mustache.classify(action.name)) # or eval?
        else
          ::Mustache
        end

        return ::Mustache::Context.new(klass.new), path, View.exts_of(self).first
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ramaze-2011.01.30 lib/ramaze/view/mustache.rb
ramaze-2011.01 lib/ramaze/view/mustache.rb
ramaze-2010.06.18 lib/ramaze/view/mustache.rb
ramaze-2010.04.04 lib/ramaze/view/mustache.rb
ramaze-2010.04 lib/ramaze/view/mustache.rb