Checking for an asset existence

Fellow Radiant users:
I have a site for which I have a “highlights” image for certain pages.
Sort of a branding/marketing image. I’ve put those images in a
/highlights directory and named them after the page they belong in, e.g.
home ==> home-highlight.jpg. Of course, for the pages that don’t have an
image I get the always unpleasant image placeholder icon in Safari and
IE. If there a easy way to use something like a or
type of tag? So that I can only include that
tag
for those pages that have one. I can do something with Javascript but
that’s just not pretty. Any ideas?

Thanks,
Brian

How about this:

The Radius code:

<r:if_public_file_exists path="/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

I apologize… File.expand() should be File.expand_path(). That’s what
I
get for drinking too much coffee!!

Sean

coffee…yeah right! :slight_smile:

Sean C. wrote:

I apologize… File.expand() should be File.expand_path(). That’s what
I
get for drinking too much coffee!!

Sean

Thanks Sean. I’ll play with the code tonight and report back.

Sean C. wrote:

How about this:

The Radius code:

<r:if_public_file_exists path="/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
#
# if the image exists, if not it will render
#
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

Brian Sam-Bodden wrote:

it renders
# For example in the page with slug ‘home’
default = tag.attr[‘default’] ? “#{options.delete(‘default’)}” :
url = “”
Brian

It seems you studied some of the built-in tags too, for tricks. Here’s
are a few ways you can clarify the code:

tag ‘slug_image’ do |tag|
options = tag.attr.dup

Well-known Ruby trick for conditional

assignment - nil evaluates the same as false

prefix = options.delete(‘prefix’) || ‘’
suffix = options.delete(‘suffix’) || ‘’
default = options.delete(‘default’) || ‘’
attributes = options.inject([]) { |s, (k, v)|
s << %{#{k.downcase}="#{v}" } }.join(" “).strip
attributes = " #{attributes}” unless attributes.empty?
slugFile = prefix + tag.locals.page.slug + suffix
defaultFile = prefix + default + suffix
slugPath = File.expand_path(File.join(RAILS_ROOT, “public”, slugFile))
defaultPath = File.expand_path(File.join(RAILS_ROOT, “public”,
defaultFile))
url = case
when File.exists?(slugPath)
slugFile
when File.exists?(defaultPath)
defaultFile
else
nil
end
%{<img src="#{url}"#{attributes} />} if url
end

I also completed your to-do. There might be some other tricks you could
employ, parallel assignment with an inject, but why bother? Although
it’s fairly long, the tag definition is really clear - the key is to be
pragmatic. There is a point at which trying to be DRY becomes
impractical and unclear and unnatural.

Cheers,

Sean