Class: EasyHtmlGenerator::Generator::Compile::Haml::Context

Inherits:
Object
  • Object
show all
Includes:
Activesupport, AssetHelper
Defined in:
lib/easy_html_generator/generator/compile/haml/context.rb

Overview

this class represents the render context for a haml file

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AssetHelper

#external_path?, #file_exists?, #glyph_icon, #glyph_icon_classes, #headjs_javascript_include_tag, #headjs_stylesheet_link_tag, #inline_javascript_include_tag, #inline_stylesheet_link_tag, #link_tag, #meta_tag, #meta_tag_http, #with_coffee, #with_sass

Methods included from Activesupport

#append_asset_version, #content_for, #orig_content_for, #path_to_image, #path_to_javascript, #path_to_stylesheet

Constructor Details

#initialize(project, config, scope) ⇒ Context

Returns a new instance of Context.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/easy_html_generator/generator/compile/haml/context.rb', line 15

def initialize(project, config, scope)
  @config       = config
  @project      = project
  @scope        = scope
  @input_folder = @project.src_path_to :views

  _prepare_context # ActionView::Context

  load_helper EasyHtmlGenerator::SHARED_HELPER_PATH
  load_helper @project.src_path_to :helper
end

Instance Attribute Details

#projectObject (readonly)

Any properties of this object are available in the Haml templates.



11
12
13
# File 'lib/easy_html_generator/generator/compile/haml/context.rb', line 11

def project
  @project
end

Instance Method Details

#load_helper(folder) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/easy_html_generator/generator/compile/haml/context.rb', line 27

def load_helper(folder)
  Dir.glob("#{folder}/**/*.rb").each do |path|
    load path
    file_without_ext = path.split('/')[-1].split('.').first
    module_name      = file_without_ext.classify
    STDERR.puts "  | -> loading haml helper: #{module_name.green}"
    self.class.send(:include, module_name.constantize)
  end
end

#render_partial(file_name, locals = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/easy_html_generator/generator/compile/haml/context.rb', line 37

def render_partial(file_name, locals={})
  file_to_render = "#{@input_folder}/#{file_name}.haml"
  if @scope
    # Look for a partial prefixed with the current "scope"
    # (which is just the name of the primary template being rendered).
    scope_file = "#{@input_folder}/#{@scope}.#{file_name}.haml"
    # Use it if it's there.
    file_to_render = scope_file if File.exist? scope_file
  end
  Haml::Engine.new(File.read(file_to_render), @config).render(self, locals)
rescue StandardError => e
  raise e, "#{e.message} in #{file_name} ", e.backtrace
end