[Radiant] Checking for an asset existence

Brian Sam-Bodden bsbodden at integrallis.com
Sat Dec 16 13:08:00 CST 2006


Sean Cribbs wrote:
> How about this:
> 
> The Radius code:
> 
> <r:if_public_file_exists path="/images/home-highlight.jpg">
>   <img src="/images/home-highlight.jpg" />
> </r:if_public_file_exists>
> 
> 
> The tag definition:
> 
> tag "if_public_file_exists" do |tag|
>   tag.expand if File.exists?(File.expand(File.join(RAILS_ROOT, "public",
> tag.attr['path'])))
> end
> 
> Of course you could change the nomenclature however you like, and a few 
> unit
> tests wouldn't hurt, but that's the basic outline.
> 
> Sean

Sean,
  Well after playing around with the my new tag definition based on your 
example I was having trouble passing the value of the current page slug. 
Being a Radiant and a Ruby newbie I think I might have ended up writing 
the ultimate overkill tag in most Javaesk fashion. Basically I wanted to 
add an image whose name was based on the slug for the current page but 
if that image didn't exist it would use a default image. Below is what 
came out of my experiments.
  Let me know if you have any ideas on how to make this simpler or 
whether I can just pass the slug value without having the tag know how 
to do it.

    # <r:slug_image [prefix=""] [suffix=""] [default=""] [other 
attributes...] /
    #
    # Renders a img tag to the page where part of the image name is 
based on the
    # slug for the current page. It allows you to add a prefix and a 
suffix to
    # build the name of the file. If the file exist (in the public 
directory) it
    # then renders the img tag for the file. If the file does not exist 
it renders
    # a img tag for a default image using the same prefix and suffix and 
using the
    # value of the default attribute. If not default attribute is 
provided it uses
    # the value 'default'. The slug_image tag passes all attributes over 
to the HTML
    # 'img' tag. This is very useful for passing attributes like the 
'class' attribute
    # or 'id' attribute.
    #
    # For example in the page with slug 'home'
    #   <r:slug_image prefix="images/highlights/" 
suffix="-highlight.jpg" border="0" />
    # will render
    #   <img src="image/highlights/home-highlight.jpg" border="0" />
    # if the image exists, if not it will render
    #   <img src="image/highlights/default-highlight.jpg" border="0" />
    define_tag 'slug_image' do |tag|
      options = tag.attr.dup
      prefix = tag.attr['prefix'] ? "#{options.delete('prefix')}" : ''
      suffix = tag.attr['suffix'] ? "#{options.delete('suffix')}" : ''
      default = tag.attr['default'] ? "#{options.delete('default')}" : 
'default'
      attributes = options.inject('') { |s, (k, v)| s << 
%{#{k.downcase}="#{v}" } }.strip
      attributes = " #{attributes}" unless attributes.empty?
      slugFile = prefix + tag.locals.page.slug + suffix
      defaultFile = prefix + default + suffix
      slugUrl = File.expand_path(File.join(RAILS_ROOT, "public", 
slugFile))
      defaultUrl = File.expand_path(File.join(RAILS_ROOT, "public", 
defaultFile))
      url = ""
      if File.exists?(slugUrl)
        url = slugFile
      else #todo: should check that the default file exists
        url = defaultFile
      end

      %{<img src="#{url}"#{attributes} />}
    end

Thanks,
  Brian

-- 
Posted via http://www.ruby-forum.com/.



More information about the Radiant mailing list