modified parser and also rendering of 404 not found page .. also added force method for frontend and also to disbale layout in frontend

This commit is contained in:
Harry Bomrah 2014-05-05 14:04:50 +08:00
parent 751b6ab2ea
commit 7f921e6f21
4 changed files with 59 additions and 47 deletions

View File

@ -1,3 +1,6 @@
# pass the layout=false for not rendering the layouts and also can specify the methods to in the backend controller.
# data-layout-content="arrayname" in layouts can be used to render data in the array
class PagesController < ApplicationController
before_action :get_key
layout :get_layout
@ -22,7 +25,7 @@ class PagesController < ApplicationController
page = Page.first
OrbitHelper.set_params params
OrbitHelper.set_site_locale locale
render :html => render_final_page("home",page).html_safe
render :html => render_final_page("home",page,true).html_safe
end
def show
@ -33,15 +36,16 @@ class PagesController < ApplicationController
if path.last.include? '-'
params[:page_id] = path[-2]
params[:slug] = path[-1]
params[:uid] = path[-1].split("-").last
params[:target_action] = "show"
uid = path[-1].split("-").last
uid = uid.split("?").first
params[:uid] = uid
params[:target_action] = params[:method] || "show"
else
params[:page_id] = path[-1]
params[:slug] = nil
params[:uid] = nil
params[:target_action] = "index"
params[:target_action] = params[:method] || "index"
end
page = Page.find_by_param(params[:page_id])
if !page.nil?
if page.enabled_for.include? I18n.locale.to_s
@ -56,9 +60,14 @@ class PagesController < ApplicationController
OrbitHelper.set_site_locale locale
OrbitHelper.set_page_categories page.categories || []
OrbitHelper.set_page_data_count page.data_count
if params[:layout].kind_of?(String)
layout = to_bool(params[:layout])
else
layout = true
end
# render render_final_page("#{module_app}/#{params[:target_action]}",page)
render :html => render_final_page("#{module_app}/#{params[:target_action]}",page).html_safe
render :html => render_final_page("#{module_app}/#{params[:target_action]}",page,layout).html_safe
else
render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found
end
@ -154,9 +163,9 @@ class PagesController < ApplicationController
private
def render_final_page(original_view=get_view,page)
parts = page.page_parts
def render_final_page(original_view=get_view,page,layout)
if layout
parts = page.page_parts rescue []
@part_partials = {}
parts.each do |part|
@ -192,6 +201,11 @@ class PagesController < ApplicationController
viewarea = doc.css("*[data-content='true']")[0]
viewarea.inner_html = render_to_string(original_view)
end
doc.to_html
else
render_to_string(original_view)
end
# newlayout = "#{page.name}_layout.html.erb"
# file_path = File.join(Rails.root, 'app', 'views', 'frontend', newlayout)
@ -200,7 +214,6 @@ class PagesController < ApplicationController
# f.close
# view_to_render = "frontend/#{newlayout}"
# view_to_render
doc.to_html
end
def get_widget_path(widget)
@ -208,8 +221,8 @@ class PagesController < ApplicationController
end
def get_view
page = Page.find(params[:id])
if page == Page.first
page = Page.find(params[:id]) rescue Page.root
if page == Page.root
@view = File.join(Rails.root, 'app', 'templates', "#{@key}", 'home/index.html.erb')
else
module_name = page.module.downcase.singularize
@ -247,5 +260,8 @@ class PagesController < ApplicationController
false
end
end
def to_bool(str)
return true if str == "true"
return false if str == "false"
end
end

View File

@ -118,15 +118,18 @@ module ApplicationHelper
file = File.open(File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', params[:target_controller].singularize, "#{params[:target_action]}.html.erb"))
doc = Nokogiri::HTML(file, nil, "UTF-8")
file.close
wrap_element_html = doc.to_s
html_to_render = ""
controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new
data = controller.send("#{params[:target_action]}")
if data.blank? || data.empty?
render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found
file = File.open("#{Rails.root}/public/404.html")
doc = Nokogiri::HTML(file, nil, "UTF-8")
file.close
doc.to_html.html_safe
else
wrap_elements = doc.css("*[data-repeat]")
wrap_elements = doc.css("*[data-repeat-content]")
if wrap_elements.count == 0
wrap_element_html = doc.to_s
el = wrap_element_html
data.each do |key,value|
value = value.nil? ? "" : value
@ -136,9 +139,14 @@ module ApplicationHelper
el.html_safe
else
keys = data.keys
wrap_elements.each do |wrap_element|
not_array_key = nil
data.keys.each do |key|
not_array_key = key if data["#{key}"].kind_of?(Hash)
end
wrap_elements.each do |wrap_element|
html_to_render = ""
(data[keys[0]].kind_of?(Array) ? data[keys[0]] : data[keys[1]]).each do |item|
content_array = wrap_element.attr("data-repeat-content")
data["#{content_array}"].each do |item|
el = wrap_element.inner_html
item.each do |key,value|
value = value.nil? ? "" : value
@ -150,8 +158,8 @@ module ApplicationHelper
wrap_element.inner_html = html_to_render
end
html = doc.to_html
if keys[1]
(data[keys[1]].kind_of?(Array) ? data[keys[0]] : data[keys[1]]).each do |key,value|
if data["#{not_array_key}"]
data["#{not_array_key}"].each do |key,value|
value = value.nil? ? "" : value
html = html.gsub("{{#{key}}}",value)
html = html.gsub("%7B%7B#{key}%7D%7D",value)
@ -164,7 +172,7 @@ module ApplicationHelper
end
def action_data
controller = "#{params[:target_controller].capitalize}Controller".constantize.new
controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new
data = controller.send("#{params[:target_action]}")
data
end

View File

@ -17,7 +17,7 @@
<p class="pic"><img src="{{image}}" alt=""></p>
<p>{{body}}</p>
</section>
<div class="post-related">
asdf
<div class="post-related" data-repeat="">
<a href="{{link_to_file}}">{{file}}</a>
</div>
</article>

View File

@ -1,22 +1,10 @@
<section class="show" module="gallery">
<div class="post-title">
<h1>{{album-title}}</h1>
</div>
<small class="post-meta-date">
<span>
TAGS :
<a href="#"><span class="label label-info">theme</span></a>
<a href="#"><span class="label label-info">theme</span></a>
<a href="#"><span class="label label-info">theme</span></a>
</span>
<span class="author">BY : author</span>
<span class="unit">AT : unit</span>
<span class="date">2014-03-07 19:24:37</span>
</small>
<section class="post">
<img class="photo" src="http://placehold.it/768x600" alt="">
</section>
<div class="post-related">
asdf
</div>
</section>
<div class="index" module="gallery">
<h2 class="widget-title">{{album-title}}</h2>
<ul class="row" data-repeat-content="images">
<li class="col-md-3 col-sm-4 col-xs-3">
<a href="{{link_to_show}}">
<img class="img-rounded" src="{{thumb-src}}" alt="">
</a>
</li>
</ul>
</div>