Sha256: 76d2c7070867b4e06d0e8fdd78a5d11debbb5fae3eb36e066775af2cd0e582db

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 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, path, ext) }
        html = view.render(context)

        return html, 'text/html'
      end

      def self.class_defined?(action)
        return {}, 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

2 entries across 2 versions & 1 rubygems

Version Path
ramaze-2010.03 lib/ramaze/view/mustache.rb
ramaze-2010.01 lib/ramaze/view/mustache.rb