Permalink
Browse files

Liquid custom tags refactoring, menu generator with liquid

  • Loading branch information...
1 parent fb699db commit 2cc59697ecdf3d05f2b35cbcbe9cbc4011178d5f @anupnivargi anupnivargi committed Feb 17, 2010
@@ -1,27 +1,26 @@
module WebsitesHelper
- def menu_generator(ul_class='', li_class='')
- str = ""
- @menus = @current_website.menus
- str += "<ul class=#{ul_class}>"
- @menus.roots.each do |root|
- str +="<li>#{link_to root.name, (root.title_sanitized.to_s == '' ? '#' : '/' + root.title_sanitized) }"
- str += root.children.blank? ? '</li>' : create_child(root) + '</li>'
- end
- str += '</ul>'
- end
+# def menu_generator(ul_class='', li_class='')
+# str = ""
+# @menus = @current_website.menus
+# str += "<ul class=#{ul_class}>"
+# @menus.roots.each do |root|
+# str +="<li>#{link_to root.name, (root.title_sanitized.to_s == '' ? '#' : '/' + root.title_sanitized) }"
+# str += root.children.blank? ? '</li>' : create_child(root) + '</li>'
+# end
+# str += '</ul>'
+# end
- def create_child(object)
-
- str = ""
- str = '<ul id="' + object.name + '">'
- object.children.each do |child|
- str += "<li>#{link_to child.name, (child.title_sanitized.to_s == '' ? child.title_sanitized : '/' + child.title_sanitized)}"
- str += child.children.blank? ? '</li>' : create_child(child) + '</li>'
- end
- str += '</ul>'
- end
-
+# def create_child(object)
+# str = ""
+# str = '<ul id="' + object.name + '">'
+# object.children.each do |child|
+# str += "<li>#{link_to child.name, (child.title_sanitized.to_s == '' ? child.title_sanitized : '/' + child.title_sanitized)}"
+# str += child.children.blank? ? '</li>' : create_child(child) + '</li>'
+# end
+# str += '</ul>'
+# end
+#
# include GoogleTranslate
# def translate_text(content_text)
View
@@ -23,6 +23,7 @@
#
# See the ActsAsItem:ModelMethods module to have further informations.
#
+require 'friendly_url'
class Article < ActiveRecord::Base
# Method defined in the ActsAsItem:ModelMethods:ClassMethods (see that library fro more information)
View
@@ -29,6 +29,7 @@
#
# See the ActsAsItem:ModelMethods module to have further informations.
#
+require 'friendly_url'
class Audio < ActiveRecord::Base
# Method defined in the ActsAsItem:ModelMethods:ClassMethods (see that library fro more information)
View
@@ -33,6 +33,7 @@
#
# See the ActsAsItem:ModelMethods module to have further informations.
#
+require 'friendly_url'
class Bookmark < ActiveRecord::Base
#to access helper methods
View
@@ -27,6 +27,7 @@
#
# See the ActsAsItem:ModelMethods module to have further informations.
#
+require 'friendly_url'
class CmsFile < ActiveRecord::Base
# Method defined in the ActsAsItem:ModelMethods:ClassMethods (see that library fro more information)
@@ -32,6 +32,7 @@
require 'open-uri'
require 'regexps'
require 'feedzirra'
+require 'friendly_url'
# This class is defining an item object called 'FeedSource'.
#
View
@@ -16,6 +16,7 @@
#
require 'fastercsv'
+require 'friendly_url'
# This class is defining an object called 'Group'. It is close to an item but the difference is that
# a group can only be in one uniq workspace.
View
@@ -26,6 +26,7 @@
#
# See the ActsAsItem:ModelMethods module to have further informations.
#
+require 'friendly_url'
class Image < ActiveRecord::Base
# Method defined in the ActsAsItem:ModelMethods:ClassMethods (see that library fro more information)
View
@@ -1,3 +1,4 @@
+require 'friendly_url'
class Menu < ActiveRecord::Base
acts_as_tree
View
@@ -19,6 +19,7 @@
#
require 'regexps'
+require 'friendly_url'
# This class is defining an item object called 'Newsletter'.
#
View
@@ -32,6 +32,7 @@
#
# See the ActsAsItem:ModelMethods module to have further informations.
#
+require 'friendly_url'
class Video < ActiveRecord::Base
# Method defined in the ActsAsItem:ModelMethods:ClassMethods (see that library fro more information)
View
@@ -1,60 +1,138 @@
module CustomTags
- class Item < Liquid::Tag
- #include ActionView::Helpers::UrlHelper
- def initialize(tag_name, markup, tokens)
- super
- p markup
- @tag_name = tag_name
- @args = eval('{' + markup + '}')
- end
-
- def render(context)
- send(@args[:item].pluralize, @args)
- end
- ITEMS.each do |item|
- define_method item.pluralize.to_sym do |*args|
- options = args.extract_options!
- items = item.classify.constantize.get_da_objects_list(build_params(options.merge!(:items => [item])))
+ ITEMS.each do |item|
+ class_name = "#{item.camelize}tag"
+ Object.const_set(class_name, Class.new(Liquid::Tag) {
+ def initialize(tag_name, markup, tokens)
+ super
+ @tag_name = tag_name
+ @args = eval('{' + markup + '}')
+ end
+
+ def render(context)
+ items = @tag_name.classify.constantize.get_da_objects_list(build_params(@args.merge!(:items => [@tag_name.singularize])))
str = ""
- items.each do |item|
- str += "<li><a href='/#{item.class.to_s.underscore}/#{item.title_sanitized}'>#{item.title}</a></li>"
+ items.each do |i|
+ str += "<li><a href='/#{i.class.to_s.underscore}/#{i.title_sanitized}'>#{i.title}</a></li>"
#str += "<li>" item.title, "/#{item}/#{item.title_sanitized}") + "</li>"
end
return str
end
- end
-# def items(*args)
-# options = args.extract_options!
-# options[:items] ||= @current_website.available_items.split(',')
-# search = Search.new(setting_searching_params(:from_params => build_params(options)))
-# items = search.do_search
-# str = ""
-# items.each do |item|
-# str += "<li>" + (link_to item.title, "/#{item.class.to_s.underscore}/#{item.title_sanitized}") + "</li>"
-# end
-# return str
-# end
+ protected
+
+ def build_params(options)
+ options[:field] ||= 'created_at'
+ options[:order] ||= 'desc'
+ options[:limit] ||= 5
+ hash = {
+ :models => options[:items],
+ :filter => { :field => options[:field], :way => options[:order]},
+ :pagination => { :page => options[:page] || 1, :per_page => options[:limit]},
+ :containers => {:website => [$current_website.id.to_s]},
+ :user => $current_website.creator,
+ :permission => 'show',
+ :opti => options[:opti] ? options[:opti] : 'skip_pag_but_filter'
+ }
+ hash
+ end
+ })
+ Liquid::Template.register_tag(item.pluralize, class_name.classify.constantize)
+ end
- protected
-
- def build_params(options)
- options[:field] ||= 'created_at'
- options[:order] ||= 'desc'
- options[:limit] ||= 5
- hash = {
- :models => options[:items],
- :filter => { :field => options[:field], :way => options[:order]},
- :pagination => { :page => options[:page] || 1, :per_page => options[:limit]},
- :containers => {:website => [$current_website.id.to_s]},
- :user => $current_website.creator,
- :permission => 'show',
- :opti => options[:opti] ? options[:opti] : 'skip_pag_but_filter'
- }
- hash
+ class MenuGenerator < Liquid::Tag
+ def initialize(tag_name, markup, tokens )
+ super
+ @tag_name = tag_name
+ @args = eval('{'+ markup +'}')
end
+ def render(context)
+ menu_generator(@args)
+ end
+
+ def menu_generator(args)
+ str = ""
+ @menus = $current_website.menus
+ ul = args[:ul] ? "<ul id=#{args[:ul]} class=#{args[:ul]}>" : '<ul>'
+ li = args[:li] ? "<li id=#{args[:li]} class=#{args[:li]}>" : '<li>'
+ current = args[:current] ? "<li id=#{args[:current]} class=#{args[:current]}>" : '<li>'
+ str += ul
+ @menus.roots.each do |root|
+ str += li + "<a href=#{root.title_sanitized.to_s == '' ? '#' : '/' + root.title_sanitized}>#{root.name}</a>"
+ #str += li + "#{link_to root.name, (root.title_sanitized.to_s == '' ? '#' : '/' + root.title_sanitized) }"
+ str += root.children.blank? ? '</li>' : create_child(root) + '</li>'
+ end
+ str += '</ul>'
+ end
+
+ def create_child(object)
+ str = ""
+ str = '<ul id="' + object.name + '">'
+ object.children.each do |child|
+ str += "<li><a href=#{child.title_sanitized.to_s == '' ? '#' : '/' + child.title_sanitized}>#{child.name}</a>"
+ #str += "<li>#{link_to child.name, (child.title_sanitized.to_s == '' ? '#' : '/' + child.title_sanitized)}"
+ str += child.children.blank? ? '</li>' : create_child(child) + '</li>'
+ end
+ str += '</ul>'
+ end
end
- Liquid::Template.register_tag('items', Item)
+ Liquid::Template.register_tag('menu_generator', MenuGenerator)
+# class Item < Liquid::Tag
+# #include ActionView::Helpers::UrlHelper
+# def initialize(tag_name, markup, tokens)
+# super
+# @tag_name = tag_name
+# @args = eval('{' + markup + '}')
+# end
+
+# def render(context)
+# send(@args[:item].pluralize, @args)
+# end
+
+# ITEMS.each do |item|
+# define_method item.pluralize.to_sym do |*args|
+# options = args.extract_options!
+# items = item.classify.constantize.get_da_objects_list(build_params(options.merge!(:items => [item])))
+# str = ""
+# items.each do |item|
+# str += "<li><a href='/#{item.class.to_s.underscore}/#{item.title_sanitized}'>#{item.title}</a></li>"
+# #str += "<li>" item.title, "/#{item}/#{item.title_sanitized}") + "</li>"
+# end
+# return str
+# end
+# end
+
+## def items(*args)
+## options = args.extract_options!
+## options[:items] ||= @current_website.available_items.split(',')
+## search = Search.new(setting_searching_params(:from_params => build_params(options)))
+## items = search.do_search
+## str = ""
+## items.each do |item|
+## str += "<li>" + (link_to item.title, "/#{item.class.to_s.underscore}/#{item.title_sanitized}") + "</li>"
+## end
+## return str
+## end
+
+# protected
+
+# def build_params(options)
+# options[:field] ||= 'created_at'
+# options[:order] ||= 'desc'
+# options[:limit] ||= 5
+# hash = {
+# :models => options[:items],
+# :filter => { :field => options[:field], :way => options[:order]},
+# :pagination => { :page => options[:page] || 1, :per_page => options[:limit]},
+# :containers => {:website => [$current_website.id.to_s]},
+# :user => $current_website.creator,
+# :permission => 'show',
+# :opti => options[:opti] ? options[:opti] : 'skip_pag_but_filter'
+# }
+# hash
+# end
+#
+# end
+# Liquid::Template.register_tag('items', Item)
end
View
@@ -70,7 +70,7 @@ def build_params(options)
:m => options[:items],
:by => "#{options[:field]}-#{options[:order]}",
:per_page => options[:limit],
- :containers => {:website => ['1']}
+ :containers => {:website => [$current_website.id]}
}
end
@@ -10,11 +10,9 @@ You can also purchase a PSD-file for this template.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title><%= page_title %></title>
- <meta name='description' content="<%= page_description %>"/>
- <meta name='keywords' content="<%= page_keywords %>"/>
- <meta name='description' content=''/>
- <meta name='keywords' content=''/>
+ <title>{{ page_title }}</title>
+ <meta name='description' content={{page_description}}/>
+ <meta name='keywords' content={{page_keywords}}/>
<link rel="stylesheet" href="/website_templates/carbonizer/stylesheets/style.css" type="text/css" media="screen" title="no title" charset="utf-8"/>
<link rel="stylesheet" href="/stylesheets/colorbox.css" type="text/css" />
<script type="text/javascript" src="javascripts/application.js"></script>
@@ -27,12 +25,12 @@ You can also purchase a PSD-file for this template.
<!-- header -->
<div id="header">
- <div id="logo"><a href=""><%= site_title %></a></div>
+ <div id="logo"><a href="">{{site_title}}</a></div>
</div>
<div id="menu">
- <%= menu_generator %>
+ {% menu_generator %}
</div>
<!--end header -->
<!-- main -->
@@ -41,36 +39,33 @@ You can also purchase a PSD-file for this template.
<div id="sidebar">
<h3>Latest Articles</h3>
<ul>
- <%= articles(:field => 'created_at', :order => 'desc', :limit => 5) %>
+ {% articles :field => 'title', :order => 'desc', :limit => 5 %}
</ul>
- <h3>Other</h3>
+ <h3>Latest Images</h3>
<ul>
- <%= items(:items => ['image','audio', 'video','cms_file']) %>
+ {% images :field => 'title', :order => 'desc', :limit => 5 %}
</ul>
- <h3>Sidebar</h3>
- <ul>
- <li><a href="#">Sidebar item</a></li>
- <li><a href="#">Sidebar item</a></li>
- <li><a href="#">Sidebar item</a></li>
- <li><a href="#">Sidebar item</a></li>
+ <h3>Latest Videos</h3>
+ <ul>
+ {% videos :field => 'title', :order => 'desc', :limit => 5 %}
</ul>
</div>
<div id="text">
- <%=yield%>
+ {{ page_body }}
</div>
</div>
<!-- end main -->
<!-- footer -->
<div id="footer">
- <div id="footer_left">&copy; Copyright 2010 <%= site_title %></div>
+ <div id="footer_left">&copy; Copyright 2010 {{site_title}}</div>
<div id="footer_right">
<!-- Please do not change or delete these links. Read the license! Thanks. :-) -->
Design by <a href="http://www.designity.org/">Free Web Design Community</a>
- Powered by <a href="http://www.blankapplication.org/">ThinkDRY:Blank Application</a>
+ {{powered_by}}
</div>
</div>
Oops, something went wrong.

0 comments on commit 2cc5969

Please sign in to comment.