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