# Checking for an asset existence

**URL:** <https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353>\
**Category:** Radiant CMS\
**Created:** [December 14, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353 "2006-12-14T15:35:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Sam-Bodden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@Brian\_Sam-Bodden](https://www.ruby-forum.com/u/Brian_Sam-Bodden)\
**Post date:** [December 14, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353/1 "2006-12-14T15:35:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Sam-Bodden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@Brian\_Sam-Bodden](https://www.ruby-forum.com/u/Brian_Sam-Bodden)\
**Post date:** [December 14, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353/2 "2006-12-14T20:30:49Z")

</div>

How about this:

The Radius code:

\<r:if\_public\_file\_exists path="/images/home-highlight.jpg"\>  
 ![](https://www.ruby-forum.com/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

---

<div class="post-metadata">

**Author:** ![Brian\_Sam-Bodden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@Brian\_Sam-Bodden](https://www.ruby-forum.com/u/Brian_Sam-Bodden)\
**Post date:** [December 14, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353/3 "2006-12-14T20:32:50Z")

</div>

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

Sean

---

<div class="post-metadata">

**Author:** ![Brian\_Sam-Bodden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@Brian\_Sam-Bodden](https://www.ruby-forum.com/u/Brian_Sam-Bodden)\
**Post date:** [December 14, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353/4 "2006-12-14T20:36:50Z")

</div>

coffee…yeah right! 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Sam-Bodden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@Brian\_Sam-Bodden](https://www.ruby-forum.com/u/Brian_Sam-Bodden)\
**Post date:** [December 14, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353/5 "2006-12-14T21:54:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Sam-Bodden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@Brian\_Sam-Bodden](https://www.ruby-forum.com/u/Brian_Sam-Bodden)\
**Post date:** [December 16, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353/6 "2006-12-16T20:08:00Z")

</div>

Sean C. wrote:

> How about this:
> 
> The Radius code:
> 
> \<r:if\_public\_file\_exists path="/images/home-highlight.jpg"\>  
> ![](https://www.ruby-forum.com/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

---

<div class="post-metadata">

**Author:** ![Brian\_Sam-Bodden](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/4da419/32.png) [@Brian\_Sam-Bodden](https://www.ruby-forum.com/u/Brian_Sam-Bodden)\
**Post date:** [December 16, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/checking-for-an-asset-existence/79353/7 "2006-12-16T23:01:09Z")

</div>

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
