Modifications in Layout

In the layout file we can have "contents" (for editable content) and
"layout_parts" (for when the layout is created from scratch)
This commit is contained in:
chris2tof 2011-09-05 01:14:49 +08:00
parent b2c0047d0f
commit f4b1d1e182
6 changed files with 43 additions and 48 deletions

View File

@ -70,12 +70,15 @@ class Design
end end
def procs_embedded_objects def procs_embedded_objects
[[self.layout], self.javascripts, self.images].each do |objects| [self.javascripts, self.images].each do |objects|
objects.each do |object| objects.each do |object|
process_object(object) process_object(object)
end end
end end
self.layout.parse_urls if self.layout.to_save
self.layout.save
self.layout.parse_urls
end
self.themes.each do |theme| self.themes.each do |theme|
to_parse_url = theme.to_save ? true : false to_parse_url = theme.to_save ? true : false
process_object(theme) process_object(theme)

View File

@ -12,7 +12,7 @@ class Layout < DesignFile
after_save :parse_layout after_save :parse_layout
def content def content
self.file.read self.file.read.force_encoding("UTF-8")
end end
def self.exist_one? def self.exist_one?
@ -20,11 +20,12 @@ class Layout < DesignFile
end end
def parse_layout def parse_layout
parse_layout_layout_part(self) debugger
parse_layout_contents(self)
end end
def parse_urls def parse_urls
orig_content = content = self.file.read.force_encoding("UTF-8") orig_content = content = self.content
self.remove_file! self.remove_file!
self.remove_file_orig! self.remove_file_orig!
names = [] names = []
@ -52,7 +53,7 @@ class Layout < DesignFile
temp_file.write content.force_encoding("UTF-8") temp_file.write content.force_encoding("UTF-8")
self.file = temp_file self.file = temp_file
Layout.without_callback(:save) do Layout.without_callback(:save, :after, :parse_layout) do
self.save self.save
end end
} }

View File

@ -5,14 +5,14 @@ class LayoutPart
field :name field :name
field :id_tag field :id_tag
field :editable field :editable, :type => Boolean
field :class_tag field :class_tag
field :content field :content
embedded_in :layout embedded_in :layout
def editable? def editable?
self.editable.eql?('true') self.editable
end end
end end

View File

@ -15,22 +15,19 @@
<body> <body>
<!-- example --> <!-- example -->
<div id="header_top" class="header"> <div id="header_top" class="header">
<r:layout_part id='header_top' class='header' name='header_1'> This is the first level of header
</r:layout_part>
</div> </div>
<div id="header_bottom" class="header"> <div id="header_bottom" class="header">
<r:layout_part id='header_bottom' class='header' name='header_2'> This is the second level of header
</r:layout_part>
</div> </div>
<div id="main_content" class="content"> <div id="main_content" class="content">
<r:layout_part id="main_content" class='content' name='block_1' editable="true"> <r:content name='content_1'>
</r:layout_part> </r:content>
</div> </div>
<div id="footer" class="footer"> <div id="footer" class="footer">
<r:layout_part id="footer" class='footer' name='footer_1'> This is the footer
</r:layout_part>
</div> </div>
<!-- END example --> <!-- END example -->
</body> </body>

View File

@ -88,11 +88,11 @@ module Parser
end end
c.define_tag 'layout_part' do |tag| c.define_tag 'layout_part' do |tag|
part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } rescue nil part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } rescue nil
if part part.content
part.content end
else c.define_tag 'content' do |tag|
tag.expand part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } rescue nil
end part.content
end end
c.define_tag 'link' do |tag| c.define_tag 'link' do |tag|
item = Item.first(:conditions => { :full_name => tag.attr['name'] }) item = Item.first(:conditions => { :full_name => tag.attr['name'] })
@ -186,20 +186,20 @@ module Parser
end end
c.define_tag 'layout_part' do |tag| c.define_tag 'layout_part' do |tag|
part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }
if part ret = ''
ret = '' ret << "<div id='#{tag.attr['name']}' part_id='#{part.id}'>"
ret << "<div id='#{tag.attr['name']}'" ret << tag.expand
ret << " part_id='#{part.id}'" ret << '</div>'
ret << " class='editable'" end
ret << " style='border:solid 1px; margin:5px; padding:5px;'>" c.define_tag 'content' do |tag|
ret << "<div class='edit_link' style='display:none'>" part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }
ret << " <a href='#{edit_admin_page_part_path(part.id)}' class='nav'>#{t(:edit)}</a>" ret = ''
ret << '</div>' ret << "<div id='#{tag.attr['name']}' part_id='#{part.id}' class='editable' style='border:solid 1px; margin:5px; padding:5px;'>"
ret << part.i18n_variable[I18n.locale.to_s] rescue '' ret << "<div class='edit_link' style='display:none'>"
ret << '</div>' ret << " <a href='#{edit_admin_page_part_path(part.id)}' class='nav'>#{t(:edit)}</a>"
else ret << '</div>'
tag.expand ret << part.i18n_variable[I18n.locale.to_s] rescue ''
end ret << '</div>'
end end
c.define_tag 'link' do |tag| c.define_tag 'link' do |tag|
item = Item.first(:conditions => { :full_name => tag.attr['name'] }) item = Item.first(:conditions => { :full_name => tag.attr['name'] })
@ -250,14 +250,14 @@ module Parser
end end
#============= #=============
def parse_layout_layout_part(layout) def parse_layout_contents(layout)
content = layout.content.force_encoding('UTF-8') content = layout.content.force_encoding('UTF-8')
context = parser_layout_layout_part(layout) context = parser_layout_contents(layout)
parser = Radius::Parser.new(context, :tag_prefix => 'r') parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(content) parser.parse(content)
end end
def parser_layout_layout_part(layout ) def parser_layout_contents(layout )
Radius::Context.new do |c| Radius::Context.new do |c|
c.define_tag 'javascripts' do |tag| c.define_tag 'javascripts' do |tag|
end end
@ -271,14 +271,8 @@ module Parser
layout.menu.save layout.menu.save
tag.expand tag.expand
end end
c.define_tag 'layout_part' do |tag| c.define_tag 'content' do |tag|
data={} layout.layout_parts.create(:name => tag.attr['name'], :editable => true)
data[:class_tag]=tag.attr['class'].to_s
data[:id_tag]=tag.attr['id'].to_s
data[:name]=tag.attr['name'].to_s
data[:editable]=tag.attr['editable'].to_s
data[:content] = tag.expand.force_encoding('UTF-8') if !(tag.attr['editable'].eql?('true'))
layout.layout_parts.create(data)
end end
end end
end end

View File

@ -115,7 +115,7 @@ namespace :dev do
home = Page.create!( :i18n_variable_id => var_10.id, :design_id => design_1.id, :name => 'home', :is_published => true, :theme_id => theme.id ) home = Page.create!( :i18n_variable_id => var_10.id, :design_id => design_1.id, :name => 'home', :is_published => true, :theme_id => theme.id )
home.page_parts.create!( :name => 'header_1', :content => File.open("#{Rails.root}/lib/template/home.page").read, :kind => 'text', :i18n_variable_id => var_13.id ) home.page_parts.create!( :name => 'content_1', :content => File.open("#{Rails.root}/lib/template/home.page").read, :kind => 'text', :i18n_variable_id => var_13.id )
end end