Skip to content

Instantly share code, notes, and snippets.

@bloopletech
Created November 2, 2008 04:00
Show Gist options
  • Save bloopletech/21648 to your computer and use it in GitHub Desktop.
Save bloopletech/21648 to your computer and use it in GitHub Desktop.
# Radiant CMS grab-bag of features.
# Some of there are quite hackish in that that they copy+paste and then modify code from Radiant itself; thus a lot of this code might only work against version 0.6.9 of Radiant.
# Page extensions; in your extension create a file called lib/page_extensions.rb with the content:
module PageExtensions
def self.included(base)
base.class_eval <<-EODATA
#this snippet adds a has_content="true|false" attribute to the if_content tag, so that the tag only expands of content if the part exists, and if the part has non-blank content.
tag 'if_content' do |tag|
page = tag.locals.page
part_name = tag_part_name(tag)
parts_arr = part_name.split(',')
inherit = boolean_attr_or_error(tag, 'inherit', 'false')
find = attr_or_error(tag, :attribute_name => 'find', :default => 'all', :values => 'any, all')
has_content = boolean_attr_or_error(tag, 'has_content', 'false')
expandable = true
one_found = false
part_page = page
parts_arr.each do |name|
name.strip!
if inherit
while (part_page.part(name).nil? and (not part_page.parent.nil?)) do
part_page = part_page.parent
end
end
x = part_page.part(name)
expandable = false if (has_content ? (x.nil? || x.content.blank?) : x.nil?)
one_found ||= true if (has_content ? (!x.nil? && !x.content.blank?) : !x.nil?)
end
expandable = true if (find == 'any' and one_found)
tag.expand if expandable
end
#this snippet allows you to alias one page to another page, so that every tag operating on the page that's aliasing actually operates on the aliased page.
#This can make nafigation a bit easier, if you want to have a bunch of pages under another page, but the parent page doesn't have any content of it's own.
#Just alias the parent page to one of the child pages, and your problem is solved.
#to alias a page, on the page that's aliasing, add a part called "Alias For" and set it's value to the path to the aliased page.
#E.g. if you want /vip-club to alias to /vip-club/vip-card, add a part called "Alias For" to /vip-club, with the content /vip-club/vip-card.
private
@parser = @context = nil
def lazy_initialize_parser_and_context
unless @parser and @context
page = (p = part("Alias For")).nil? ? self : Page.find_by_url(p.content)
page.instance_variable_set(:@request, @request)
page.instance_variable_set(:@response, @response)
@context = PageContext.new(page)
@parser = Radius::Parser.new(@context, :tag_prefix => 'r')
end
@parser
end
EODATA
end
end
# and put this line in your extension.rb#activate method:
Page.send(:include, PageExtensions)
# Feel free to remove any extensions you don't want.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment