module StaticMatic::RenderMixin

Public Instance Methods

clear_template_variables!() click to toggle source

clear all scope variables except @staticmatic

# File lib/staticmatic/mixins/render.rb, line 4
def clear_template_variables!
  
  @scope.instance_variables.each do |var|
    @scope.instance_variable_set(var, nil) unless var == '@staticmatic' || var == :@staticmatic
  end
end
fetch_layout_path(dir = nil) click to toggle source
# File lib/staticmatic/mixins/render.rb, line 59
def fetch_layout_path(dir = nil)
  layout_path = File.join(@src_dir, "_layouts")
  declared_layout_name = @scope.instance_variable_get("@layout")

  if declared_layout_name
    path = determine_template_path declared_layout_name, '', layout_path
    unless path
      error_path = File.join(layout_path, declared_layout_name)
      raise StaticMatic::Error.new("", error_path, "Layout not found")
    end
  end

  if dir
    dir_layout_name = dir.split("/")[1]
    path ||= determine_template_path dir_layout_name, '', layout_path
  end
  path ||= determine_template_path @default_layout_name, '', layout_path

  unless path
    error_path = File.join(layout_path, @default_layout_name)
    raise StaticMatic::Error.new("", error_path, "No default layout could be found")
  end

  return path
end
generate_partial(name, options = {}) click to toggle source
# File lib/staticmatic/mixins/render.rb, line 38
def generate_partial(name, options = {})
  partial_dir, partial_name, partial_ext = expand_path name
  partial_name = "_#{partial_name}"

  context = File.join File.dirname(self.current_file), partial_dir
  partial_path = determine_template_path(partial_name, partial_ext, context)

  unless partial_path && File.exists?(partial_path)
    # partial not found in the current file's directory, so try the _partials folder
    context = File.join @src_dir, '_partials', partial_dir
    partial_name.sub! /^_/, ''
    partial_path = determine_template_path(partial_name, partial_ext, context)
  end

  if partial_path && File.exists?(partial_path)
    return render_template partial_path
  else
    raise StaticMatic::Error.new("", name, "Partial not found")
  end
end
render_template(file_path) click to toggle source

Generate html from source file: render_template(“index.haml”)

# File lib/staticmatic/mixins/render.rb, line 13
def render_template(file_path)
  @current_file_stack.push(file_path)
  begin
    tilt_template(file_path)
  rescue StaticMatic::TemplateError => e
    raise e # re-raise inline errors
  rescue Exception => e
    raise StaticMatic::TemplateError.new(file_path, e)
  ensure
    @current_file_stack.pop
  end
end
render_template_with_layout(file_path) click to toggle source
# File lib/staticmatic/mixins/render.rb, line 26
def render_template_with_layout(file_path)
  @current_file_stack.push(file_path)
  begin 
    rendered_file_content = render_template(file_path)
    tilt_template_with_layout(fetch_layout_path) { rendered_file_content }
  rescue Exception => e
    render_rescue_from_error(e)
  ensure
    @current_file_stack.pop
  end
end

Private Instance Methods

get_engine_options(file_path) click to toggle source
# File lib/staticmatic/mixins/render.rb, line 98
def get_engine_options(file_path)
  ext = File.extname(file_path).sub(/^\./, '')
  options = configuration.engine_options[ext] || {}
  preview_options = configuration.preview_engine_options[ext] || {}

  if @mode == :preview
    options.merge preview_options
  else
    options
  end
end
tilt_template(file_path) click to toggle source

TODO: more code reuse. needs some ruby &block and yield sorcery.

# File lib/staticmatic/mixins/render.rb, line 88
def tilt_template(file_path)
  options = get_engine_options(file_path)
  Tilt.new(file_path, options).render(@scope)
end
tilt_template_with_layout(file_path) { || ... } click to toggle source
# File lib/staticmatic/mixins/render.rb, line 93
def tilt_template_with_layout(file_path)
  options = get_engine_options(file_path)
  Tilt.new(file_path, options).render(@scope) { yield }
end