We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
nex3 (author)
Sun Dec 03 18:47:37 -0800 2006
commit  c4032e6acff73620512b25332a9cb4a917643582
tree    189206bc8867e64c9eae6864693acca2246f8248
parent  a6ef2d2e951f22bb23832fe15e78e497e996f4f8
haml / lib / haml / template.rb
100644 130 lines (111 sloc) 4.081 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require File.dirname(__FILE__) + '/../haml'
require 'haml/engine'
require 'rubygems'
require 'active_support'
require 'action_view'
 
module Haml
  # This class interfaces with ActionView
  # to make Haml usable as a Ruby on Rails plugin.
  # It usually shouldn't need to be used by end users.
  # Just in case, though, here's what you might do to render
  # <tt>templates/index.haml</tt>:
  #
  # ActionView::Base.register_template_handler("haml", Haml::Template)
  # base = ActionView::Base.new("templates")
  # base.render("index")
  #
  # Or, if you want to really get into the nitty-gritty:
  #
  # base = ActionView::Base.new
  # template = Haml::Template.new(base)
  # template.render("templates/index.haml")
  #
  class Template
 
    class << self
      @@options = {}
 
      # Gets various options for Haml. See REFERENCE for details.
      def options
        @@options
      end
 
      # Sets various options for Haml. See REFERENCE for details.
      def options=(value)
        @@options = value
      end
    end
 
    # Creates a new Haml::Template object that uses <tt>view</tt>
    # to render its templates.
    def initialize(view)
      @view = view
      @@precompiled_templates ||= {}
    end
 
    # Renders the file at the location <tt>template</tt>,
    # with <tt>local_assigns</tt> available as local variables within the template.
    # Returns the result as a string.
    def render(template, local_assigns={})
      @view.instance_eval do
        evaluate_assigns
      end
 
      options = @@options.dup
      locals = options[:locals] || {}
      locals.merge! local_assigns
      options[:locals] = locals
 
      if @view.haml_inline
        engine = Haml::Engine.new(template, options)
      elsif @precompiled = get_precompiled(template)
        options[:precompiled] ||= @precompiled
        engine = Haml::Engine.new("", options)
      else
        engine = Haml::Engine.new(File.read(template), options)
        set_precompiled(template, engine.precompiled)
      end
 
      yield_proc = @view.instance_eval do
        proc { |*name| instance_variable_get("@content_for_#{name.first || 'layout'}") }
      end
 
      engine.to_html(@view) { |*args| yield_proc.call(*args) }
 
    end
    
    private
 
    # Gets the cached, precompiled version of the template at location <tt>filename</tt>
    # as a string.
    def get_precompiled(filename)
      # Do we have it on file? Is it new enough?
      if (precompiled, precompiled_on = @@precompiled_templates[filename]) &&
             (precompiled_on == File.mtime(filename).to_i)
        precompiled
      end
    end
 
    # Sets the cached, precompiled version of the template at location <tt>filename</tt>
    # to <tt>precompiled</tt>.
    def set_precompiled(filename, precompiled)
      @@precompiled_templates[filename] = [precompiled, File.mtime(filename).to_i]
    end
  end
end
 
# 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:
    attr :haml_filename, true
    attr :haml_inline
 
    alias_method :haml_old_render_file, :render_file
    def render_file(template_path, use_full_path = true, local_assigns = {})
      @haml_filename = File.basename(template_path)
      haml_old_render_file(template_path, use_full_path, local_assigns)
    end
 
    alias_method :read_template_file_old, :read_template_file
    def read_template_file(template_path, extension)
      if extension =~ /haml/i
        template_path
      else
        read_template_file_old(template_path, extension)
      end
    end
 
    alias_method :render_template_old, :render_template
    def render_template(template_extension, template, file_path = nil, local_assigns = {})
      @haml_inline = !template.nil?
      render_template_old(template_extension, template, file_path, local_assigns)
    end
  end
end