lib/staticmatic/mixins/render.rb in staticmatic-0.11.0.alpha.4 vs lib/staticmatic/mixins/render.rb in staticmatic-0.11.0.alpha.5
- old
+ new
@@ -1,28 +1,31 @@
module StaticMatic::RenderMixin
+ # clear all scope variables except @staticmatic
+ def clear_template_variables!
+
+ @scope.instance_variables.each do |var|
+ @scope.instance_variable_set(var, nil) unless var == '@staticmatic' || var == :@staticmatic
+ end
+ end
+
def source_for_layout
- if layout_exists?(@layout)
- File.read(full_layout_path(@layout))
+ if layout_exists?(determine_layout)
+ File.read(full_layout_path(determine_layout))
else
- raise StaticMatic::Error.new("", full_layout_path(@layout), "Layout not found")
+ raise StaticMatic::Error.new("", full_layout_path(determine_layout), "Layout not found")
end
end
# Generate html from source file:
# generate_html("index")
def generate_html(source_file, source_dir = '')
full_file_path = File.join(@src_dir, 'pages', source_dir, "#{source_file}.haml")
begin
- # clear all scope variables except @staticmatic
- @scope.instance_variables.each do |var|
- @scope.instance_variable_set(var, nil) unless var == '@staticmatic' || var == :@staticmatic
- end
+
html = generate_html_from_template_source(File.read(full_file_path))
-
- @layout = determine_layout(source_dir)
rescue StaticMatic::TemplateError => e
raise e # re-raise inline errors
rescue Exception => e
raise StaticMatic::TemplateError.new(full_file_path, e)
end
@@ -33,15 +36,15 @@
def generate_html_with_layout(source, source_dir = '')
@current_page = File.join(source_dir, "#{source}.html")
@current_file_stack.unshift(File.join(source_dir, "#{source}.haml"))
begin
template_content = generate_html(source, source_dir)
- @layout = determine_layout(source_dir)
generate_html_from_template_source(source_for_layout) { template_content }
rescue Exception => e
render_rescue_from_error(e)
ensure
+ clear_template_variables!
@current_page = nil
@current_file_stack.shift
end
end
@@ -104,11 +107,11 @@
locals = options[:locals] || {}
html.render(@scope, locals) { yield }
end
def determine_layout(dir = '')
- layout_name = @layout
+ layout_name ||= @default_layout
if @scope.instance_variable_get("@layout")
layout_name = @scope.instance_variable_get("@layout")
elsif dir
dirs = dir.split("/")
@@ -117,10 +120,10 @@
if layout_exists?(dir_layout_name)
layout_name = dir_layout_name
end
end
- layout_name
+ layout_name
end
# Returns a raw template name from a source file path:
# source_template_from_path("/path/to/site/src/stylesheets/application.sass") -> "application"
def source_template_from_path(path)
\ No newline at end of file