2006-12-03 23:56:47 +00:00
|
|
|
require 'haml/engine'
|
2006-11-08 03:39:00 +00:00
|
|
|
require 'rubygems'
|
2006-10-14 22:24:53 +00:00
|
|
|
require 'active_support'
|
|
|
|
require 'action_view'
|
2006-09-29 18:39:13 +00:00
|
|
|
|
|
|
|
module Haml
|
|
|
|
class Template
|
2006-10-30 06:59:57 +00:00
|
|
|
class << self
|
|
|
|
@@options = {}
|
2006-11-04 08:35:06 +00:00
|
|
|
|
2006-12-18 01:31:11 +00:00
|
|
|
# Gets various options for Haml. See README for details.
|
2006-10-30 06:59:57 +00:00
|
|
|
def options
|
|
|
|
@@options
|
|
|
|
end
|
2006-11-04 08:35:06 +00:00
|
|
|
|
2006-12-18 01:31:11 +00:00
|
|
|
# Sets various options for Haml. See README for details.
|
2006-10-30 06:59:57 +00:00
|
|
|
def options=(value)
|
|
|
|
@@options = value
|
|
|
|
end
|
|
|
|
end
|
2006-09-29 18:39:13 +00:00
|
|
|
end
|
2006-10-14 22:24:53 +00:00
|
|
|
end
|
|
|
|
|
2006-11-21 05:54:26 +00:00
|
|
|
# This module refers to the ActionView module that's part of Ruby on Rails.
|
|
|
|
# Haml can be used as an alternate templating engine for it,
|
|
|
|
# and includes several modifications to make it more Haml-friendly.
|
|
|
|
# The documentation can be found
|
|
|
|
# here[http://rubyonrails.org/api/classes/ActionView/Base.html].
|
|
|
|
module ActionView
|
|
|
|
class Base # :nodoc:
|
2007-11-24 22:17:11 +00:00
|
|
|
def delegate_template_exists_with_haml(template_path)
|
|
|
|
template_exists?(template_path, :haml) && [:haml]
|
|
|
|
end
|
|
|
|
alias_method :delegate_template_exists_without_haml, :delegate_template_exists?
|
|
|
|
alias_method :delegate_template_exists?, :delegate_template_exists_with_haml
|
2006-11-21 05:54:26 +00:00
|
|
|
|
2007-11-24 22:17:11 +00:00
|
|
|
def compile_template_with_haml(extension, template, file_name, local_assigns)
|
|
|
|
return compile_haml(template, file_name, local_assigns) if extension.to_s == "haml"
|
|
|
|
compile_template_without_haml(extension, template, file_name, local_assigns)
|
2006-10-14 22:24:53 +00:00
|
|
|
end
|
2007-11-24 22:17:11 +00:00
|
|
|
alias_method :compile_template_without_haml, :compile_template
|
|
|
|
alias_method :compile_template, :compile_template_with_haml
|
|
|
|
|
|
|
|
def compile_haml(template, file_name, local_assigns)
|
|
|
|
render_symbol = assign_method_name(:haml, template, file_name)
|
|
|
|
locals = local_assigns.keys
|
|
|
|
|
|
|
|
@@template_args[render_symbol] ||= {}
|
|
|
|
locals_keys = @@template_args[render_symbol].keys | locals
|
|
|
|
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
|
|
|
|
|
|
|
|
options = Haml::Template.options.dup
|
|
|
|
options[:filename] = file_name || 'compiled-template'
|
|
|
|
|
|
|
|
begin
|
|
|
|
Haml::Engine.new(template, options).def_method(CompiledTemplates, render_symbol, *locals)
|
|
|
|
rescue Exception => e
|
|
|
|
if logger
|
|
|
|
logger.debug "ERROR: compiling #{render_symbol} RAISED #{e}"
|
|
|
|
logger.debug "Backtrace: #{e.backtrace.join("\n")}"
|
|
|
|
end
|
|
|
|
|
|
|
|
raise TemplateError.new(@base_path, file_name || template, @assigns, template, e)
|
|
|
|
end
|
2006-11-06 03:33:58 +00:00
|
|
|
|
2007-11-24 22:17:11 +00:00
|
|
|
@@compile_time[render_symbol] = Time.now
|
2006-11-21 05:54:26 +00:00
|
|
|
end
|
2006-11-06 03:33:58 +00:00
|
|
|
end
|
2006-10-14 22:24:53 +00:00
|
|
|
end
|