Neat hack to <r:gallery>

08/10/2009

I just made a little tweak to the gallery_tags.rb from the Gallery Extension.

It makes it easy to show a gallery somewhere on any page, if that page has a base gallery set. You can also hide a potential surrounding div if it is not needed (not containing a gallery). For example:

<r:gallery fail_silently="true"><div id="gallery">
  <r:gallery:lightbox />
</div></r:gallery>

Without the hack, (and the fail_silently attribute) you would get a nasty "undefined method `items' for nil:NilClass" if there was no base gallery set on that page. Of course, it would also work if you are specifying a gallery by name or id, instead of relying on a base gallery being set. 

And all it took was;

tag "gallery" do |tag| tag.locals.gallery = find_gallery(tag)
- tag.expand
+ tag.expand unless tag.locals.gallery.nil? && tag.attr['fail_silently']
end

Auto-prepending links in RSS feeds with your domain (on Radiant)

11/09/2009 1 reactions

When you syndicate Radiant content, you may hit the problem of internal links, or img src attributes pointing to invalid paths on a syndicator's domain. So, for example, an aggregator may link to http://aggregator.com/about-us instead of http://your-site.com/about-us. The simple fix is ofcourse to use absolute URLs for all your href and src attributes, but that is not default behavior in Radiant (or most CMSes as far as i know).

If you are using paperclipped, and use <r:asset /> tags to display images, you could fix their src attributes in script console or via the Settings extension by doing:

Radiant::Config["assets.url"] = "http://your-site.com/:class/:id/:basename:no_original_style.:extension"

But that still leaves you with the fact that <r:url />, <r:link /> and some other radius tags output relative urls. Heck, you may even have a few clients that are smart enough to write urls by hand! Here's a method that will convert all relative URLs to absolute ones;

def fix_for_syndication(text, host)   text.gsub(/href=('|")([^(http:|mailto:)].*?)('|")/,
    'href="' + host + '\2"').gsub(/src=('|")([^http:].*?)('|")/, 'src="' +
    host + '\2"') end

From there you can make a simple radius tag to use where-ever you output stuff to an RSS feed;

tag 'fix_links_for_syndication' do |tag|   fix_for_syndication(tag.expand,
    "#{tag.globals.page.request.protocol}#{tag.globals.page.request.host_with_port}")
    end

I used this in one of our own extensions (works-for-us'ish), use at your own risk; probably not compatible with the standard 'blog' extension for example.

About me

I'm Benny Degezelle, a 26 year-old dude from Ghent, Belgium. I'm self employed, being one half of Gorilla webdesign.
I music, my job, card-playing evenings or party nights with friends and clean code.

Fellow geeks can link up with me on linkedin, ladies and friends may stalk me on Facebook.

What the fuzz is all about

  1. 2 pages are tagged with WYM
  2. 1 page is tagged with absolute-urls
  3. 2 pages are tagged with documentation
  4. 2 pages are tagged with editor
  5. 2 pages are tagged with extension
  6. 2 pages are tagged with extensions
  7. 1 page is tagged with page_attachments
  8. 1 page is tagged with pagination
  9. 9 pages are tagged with radiant
  10. 3 pages are tagged with rails
  11. 2 pages are tagged with ruby
  12. 2 pages are tagged with shell
  13. 1 page is tagged with terminal
  14. 1 page is tagged with timezone
  15. 2 pages are tagged with troubleshooting

Github activity

More places where you can find me

Back to top