clean code + tiny optimization
This commit is contained in:
parent
d879b80b7a
commit
08f0fdfb5b
@ -1,42 +1,21 @@
|
||||
class Layout < LiquidTemplate
|
||||
|
||||
## fields ##
|
||||
# field :blocks, :type => Hash
|
||||
|
||||
# def unmarshalled_blocks
|
||||
# # puts "self.blocks = #{self.blocks.class.inspect}"
|
||||
# @unmarshalled_blocks ||= self.blocks.inject({}) do |b, (name, node)|
|
||||
# # puts "b= #{b.inspect} / name = #{name.inspect} / node = #{node.inspect}"
|
||||
# b[name] = Marshal.load(node)
|
||||
# b
|
||||
# end
|
||||
# end
|
||||
|
||||
protected
|
||||
|
||||
|
||||
# TODO: move that in the liquify_template module
|
||||
|
||||
def after_parse_template
|
||||
blocks = self.find_blocks(self.template.root)
|
||||
self.template.send(:instance_variable_set, :"@parent_blocks", blocks)
|
||||
|
||||
# TODO: include parent blocks in self.template before marshalling it
|
||||
|
||||
# puts "[Layout / blocks] #{self.blocks.inspect}"
|
||||
end
|
||||
|
||||
def find_blocks(node, blocks = {})
|
||||
# puts "[Layout/#{self.slug}] ** find_blocks #{node.class.inspect} / #{blocks.keys.inspect}"
|
||||
if node.respond_to?(:nodelist) && node.nodelist
|
||||
# puts " ==> find_blocks nodelist = #{node.nodelist.inspect}"
|
||||
node.nodelist.inject(blocks) do |b, node|
|
||||
if node.is_a?(Locomotive::Liquid::Tags::Block)
|
||||
# b[node.name] = Marshal.dump(node)
|
||||
b[node.name] = node
|
||||
end
|
||||
# else
|
||||
self.find_blocks(node, b) # FIXME: add nested blocks
|
||||
# end
|
||||
self.find_blocks(node, b) # FIXME: find nested blocks too
|
||||
b
|
||||
end
|
||||
end
|
||||
|
@ -51,11 +51,6 @@ module Locomotive
|
||||
end
|
||||
|
||||
self.serialized_template = BSON::Binary.new(Marshal.dump(@template))
|
||||
|
||||
if self.respond_to?(:after_store_template) # kind of callback
|
||||
self.send(:after_store_template)
|
||||
end
|
||||
|
||||
rescue ::Liquid::SyntaxError => error
|
||||
self.errors.add :template, :liquid_syntax_error
|
||||
end
|
||||
|
@ -13,13 +13,11 @@ module Locomotive
|
||||
else
|
||||
raise ::Liquid::SyntaxError.new("Syntax Error in 'block' - Valid syntax: block [name]")
|
||||
end
|
||||
# puts "** [Block/initialize] #{tag_name}, #{@name}, #{tokens.inspect}"
|
||||
|
||||
super if tokens
|
||||
end
|
||||
|
||||
def render(context)
|
||||
# puts "** [Block/render] #{@name} / #{@parent.inspect}"
|
||||
context.stack do
|
||||
context['block'] = Locomotive::Liquid::Drops::Block.new(self)
|
||||
|
||||
|
@ -4,8 +4,6 @@ module Locomotive
|
||||
class Extends < ::Liquid::Block
|
||||
Syntax = /(#{::Liquid::QuotedFragment})/
|
||||
|
||||
# attr_accessor :blocks
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
if markup =~ Syntax
|
||||
@template_name = $1
|
||||
@ -18,36 +16,20 @@ module Locomotive
|
||||
@blocks = @nodelist.inject({}) do |m, node|
|
||||
m[node.name] = node if node.is_a?(Locomotive::Liquid::Tags::Block); m
|
||||
end
|
||||
|
||||
# puts "\n** initialize ** Extends #{@template_name} / #{@blocks.inspect}"
|
||||
end
|
||||
|
||||
def parse(tokens)
|
||||
# puts "[#{@template_name}] parsing...#{tokens.inspect}"
|
||||
parse_all(tokens)
|
||||
end
|
||||
|
||||
def render(context)
|
||||
if OPTIMIZATION
|
||||
template, parent_blocks = load_template(context)
|
||||
else
|
||||
template = load_template(context)
|
||||
parent_blocks = find_blocks(template.root)
|
||||
end
|
||||
template, parent_blocks = load_template(context)
|
||||
|
||||
# puts "** [Extends/render] @blocks = #{@blocks.inspect} / @nodelist = #{@nodelist.inspect} / parent_blocks = #{parent_blocks.inspect}"
|
||||
|
||||
# BUG: parent blocks and parent template blocks are disconnected (OPTIMIZATION). need to resync them along with @nodelist
|
||||
@blocks.each do |name, block|
|
||||
# puts "** [Extends/render] #{name}, #{block.inspect}"
|
||||
if pb = parent_blocks[name]
|
||||
# puts "[#{name}]...found parent block ! #{pb.inspect}"
|
||||
pb.parent = block.parent
|
||||
# puts "[#{name}] pb.parent = #{pb.parent.inspect} / block.parent = #{block.parent.inspect}"
|
||||
pb.add_parent(pb.nodelist)
|
||||
# puts "[#{name}] pb.nodelist = #{pb.nodelist.inspect}"
|
||||
pb.nodelist = block.nodelist
|
||||
# puts "[#{name}] block.nodelist = #{block.nodelist.inspect}"
|
||||
else
|
||||
if is_extending?(template)
|
||||
template.root.nodelist << block
|
||||
@ -61,8 +43,6 @@ module Locomotive
|
||||
private
|
||||
|
||||
def parse_all(tokens)
|
||||
# puts "** [parse_all] #{tokens.inspect}"
|
||||
|
||||
@nodelist ||= []
|
||||
@nodelist.clear
|
||||
|
||||
@ -72,7 +52,6 @@ module Locomotive
|
||||
if token =~ /^#{::Liquid::TagStart}\s*(\w+)\s*(.*)?#{::Liquid::TagEnd}$/
|
||||
# fetch the tag from registered blocks
|
||||
if tag = ::Liquid::Template.tags[$1]
|
||||
# puts "** [parse_all] tag = #{$1}, #{$2}"
|
||||
@nodelist << tag.new($1, $2, tokens)
|
||||
else
|
||||
# this tag is not registered with the system
|
||||
@ -93,33 +72,8 @@ module Locomotive
|
||||
end
|
||||
|
||||
def load_template(context)
|
||||
# puts "** load_template (#{context[@template_name]})"
|
||||
layout = context.registers[:site].layouts.where(:slug => context[@template_name]).first
|
||||
if OPTIMIZATION
|
||||
# [layout.template, layout.unmarshalled_blocks]
|
||||
[layout.template, layout.template.send(:instance_variable_get, :"@parent_blocks")]
|
||||
else
|
||||
layout.template
|
||||
end
|
||||
end
|
||||
|
||||
def find_blocks(node, blocks={})
|
||||
# puts "** find_blocks #{node.class.inspect} / #{blocks.keys.inspect}"
|
||||
if node.respond_to?(:nodelist) && node.nodelist
|
||||
# puts " ==> find_blocks nodelist = #{node.nodelist.inspect}"
|
||||
node.nodelist.inject(blocks) do |b, node|
|
||||
if node.is_a?(Locomotive::Liquid::Tags::Block)
|
||||
b[node.name] = node
|
||||
end
|
||||
# else
|
||||
find_blocks(node, b) # FIXME: add nested blocks
|
||||
# end
|
||||
|
||||
b
|
||||
end
|
||||
end
|
||||
|
||||
blocks
|
||||
[layout.template, layout.template.send(:instance_variable_get, :"@parent_blocks")]
|
||||
end
|
||||
|
||||
def is_extending?(template)
|
||||
|
@ -3,14 +3,6 @@
|
||||
ENV["RAILS_ENV"] ||= 'test'
|
||||
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
|
||||
|
||||
# require File.dirname(__FILE__) + "/../config/application.rb"
|
||||
|
||||
# Mongoid.configure do |config|
|
||||
# config.master = Mongo::Connection.new.db("locomotive_perf_test")
|
||||
# end
|
||||
|
||||
OPTIMIZATION = false
|
||||
|
||||
%w{sites pages layouts}.each do |collection|
|
||||
Mongoid.master.collection(collection).drop
|
||||
end
|
||||
@ -37,8 +29,6 @@ layout_with_sidebar = site.layouts.create :name => 'with_sidebar', :value => %{
|
||||
</html>
|
||||
}
|
||||
|
||||
# puts layout_with_sidebar.unmarshalled_blocks.inspect
|
||||
|
||||
custom_layout_with_sidebar = site.layouts.create :name => 'custom_with_sidebar', :value => %{
|
||||
\{% extends 'with_sidebar' %\}
|
||||
\{% block sidebar %\}A sidebar here\{% endblock %\}
|
||||
|
Loading…
Reference in New Issue
Block a user