# Conditional display of image

**URL:** <https://www.ruby-forum.com/t/conditional-display-of-image/116939>\
**Category:** Radiant CMS\
**Created:** [October 11, 2007, 1:29pm UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939 "2007-10-11T13:29:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [October 11, 2007, 1:29pm UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/1 "2007-10-11T13:29:28Z")

</div>

Hi,

I’m attaching some images to some posts on my blog, and as I don’t want  
to  
have too big images on the home page and on the archive pages, I devised  
the  
following code that I put on the first part of my page

\<r:unless cond="!request.nil? && Regexp.new(tag.locals.page.slug) =~  
request.env[‘REQUEST\_URI’]"\>  
!{float:right}\<r:attachment\_url transform=“120” id=“10”/\>(foo)!  
\</r:unless\>  
\<r:if cond="!request.nil? && Regexp.new(tag.locals.page.slug) =~  
request.env[‘REQUEST\_URI’]"\>  
!{float:right}\<r:attachment\_url transform=“200” id=“10”/\>(foo)!  
\</r:if\>

(the 120 and 200 transforms resize to 120x120 and 200x200)

The strange thing, it that I don’t always get a “request” object, and  
I’m  
wondering if there could be a better way to do that, the main point  
being “is  
the page being displayed is the page I belong too”.

Regards,

---

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [October 12, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/2 "2007-10-12T14:04:13Z")

</div>

> The strange thing, it that I don’t always get a “request” object, and I’m  
> wondering if there could be a better way to do that, the main point  
> being  
> “is  
> the page being displayed is the page I belong too”.

Sorry, i don’t quite understand the question. The code you post, is  
in a  
page, snippet or layout?

/AITOR

---

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [October 12, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/3 "2007-10-12T15:27:28Z")

</div>

±le 12.10.2007 13:37:30 +0200, Aitor Garay-Romero a dit :  
| \>The strange thing, it that I don’t always get a “request” object, and I’m  
| \>wondering if there could be a better way to do that, the main point being  
| “is  
| \>the page being displayed is the page I belong too”.  
|  
| Sorry, i don’t quite understand the question. The code you post, is in a  
| page, snippet or layout?

The code I post is in a page part.  
I’m wondering if I could do something like tags.locals.page.id ==  
self.id.  
And after a few though, I could/should maybe make two parts, one that  
would  
show on the page and one that would show in the Archive index.

---

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [October 12, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/4 "2007-10-12T15:31:34Z")

</div>

±le 12.10.2007 15:26:40 +0200, Mathieu A. a dit :  
| ±le 12.10.2007 13:37:30 +0200, Aitor Garay-Romero a dit :  
|| \> The strange thing, it that I don’t always get a “request” object, and  
|| \> I’m wondering if there could be a better way to do that, the main  
|| \> point being  
|| “is  
|| \> the page being displayed is the page I belong too”.  
||  
|| Sorry, i don’t quite understand the question. The code you post, is in  
|| a page, snippet or layout?  
|  
| The code I post is in a page part.  
| I’m wondering if I could do something like tags.locals.page.id == self.id.  
| And after a few though, I could/should maybe make two parts, one that would  
| show on the page and one that would show in the Archive index.

Hum, maybe I should show what I mean :  
[http://w.mat.cc/](http://w.mat.cc/)  
and  
[http://w.mat.cc/articles/2007/10/06/a-vif-une-touche-d-humanite/](http://w.mat.cc/articles/2007/10/06/a-vif-une-touche-d-humanite/)

That is, having a smaller image on the home page so that it does not  
take too  
much room.

Regards,

---

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [October 13, 2007, 3:29am UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/5 "2007-10-13T03:29:57Z")

</div>

On Fri, 12 Oct 2007 15:26:40 +0200  
Mathieu A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m wondering if I could do something like tags.locals.page.id ==  
> self.id. And after a few though, I could/should maybe make two parts,  
> one that would show on the page and one that would show in the  
> Archive index.

You should get the request object from tags.globals.page, not  
tag.locals.page.

Dan.

---

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [April 27, 2008, 11:46am UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/6 "2008-04-27T11:46:29Z")

</div>

On 27 Apr 2008, at 01:08, Ben M. wrote:

> > Usage:  
> > \<r:attachment:image name=“file.jpg” [size=“icon”]/\>
> 
> Also, are there other sizes available like “large” and “medium”, or is  
> “icon” the only one?

I think you used to have to change a couple of files.

In vendor/page\_attachments/app/models/page\_attachment.rb you can add  
more thumbnail sizes:  
:thumbnails =\> {:icon =\> ‘50x50\>’, :small =\> ‘160x160\>’, :medium =\>  
‘320x320\>’, :large =\> ‘640x640\>’},

and also change the admin view to find the right icon thumbnail:  
\<%= link\_to image\_tag(attachment.thumbnails.find(:first, :conditions  
=\> “thumbnail = ‘icon’”).public\_filename, :class =\> “thumbnail”) %\>

Specifying a image size seems to expose the size=“icon” attribute in  
the outputted page though. Is there a patch out there for fixing this?

Dominic

---

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [April 27, 2008, 2:08am UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/7 "2008-04-27T02:08:27Z")

</div>

Mathieu A. wrote:

> Hi,
> 
> I’m attaching some images to some posts on my blog, and as I don’t want  
> to  
> have too big images on the home page and on the archive pages, I devised  
> the  
> following code that I put on the first part of my page
> 
> \<r:unless cond="!request.nil? && Regexp.new(tag.locals.page.slug) =~  
> request.env[‘REQUEST\_URI’]"\>  
> !{float:right}\<r:attachment\_url transform=“120” id=“10”/\>(foo)!  
> \</r:unless\>  
> \<r:if cond="!request.nil? && Regexp.new(tag.locals.page.slug) =~  
> request.env[‘REQUEST\_URI’]"\>  
> !{float:right}\<r:attachment\_url transform=“200” id=“10”/\>(foo)!  
> \</r:if\>
> 
> (the 120 and 200 transforms resize to 120x120 and 200x200)

Is that “transform” something custom or does it ship with the Page  
Attachments plugin?

I see that in the “available tags for page” help, it says:

> \<r:attachment:image /\>  
> Renders an image tag for the attachment (assuming itâ€™s an image).  
> The â€˜nameâ€™ attribute is required on this tag or the parent tag.  
> Any other attributes will be added as HTML attributes to the rendered tag.  
> The optional â€˜sizeâ€™ attribute allows you to show the icon size of the image.

> Usage:  
> \<r:attachment:image name=“file.jpg” [size=“icon”]/\>

Also, are there other sizes available like “large” and “medium”, or is  
“icon” the only one?

Regards,  
Ben

---

<div class="post-metadata">

**Author:** ![Mathieu\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@Mathieu\_A](https://www.ruby-forum.com/u/Mathieu_A)\
**Post date:** [May 9, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/conditional-display-of-image/116939/8 "2008-05-09T18:00:17Z")

</div>

Dominic wrote:

> I think you used to have to change a couple of files.
> 
> In vendor/page\_attachments/app/models/page\_attachment.rb you can add  
> more thumbnail sizes:  
> :thumbnails =\> {:icon =\> ‘50x50\>’, :small =\> ‘160x160\>’, :medium =\>  
> ‘320x320\>’, :large =\> ‘640x640\>’},
> 
> and also change the admin view to find the right icon thumbnail:  
> \<%= link\_to image\_tag(attachment.thumbnails.find(:first, :conditions  
> =\> “thumbnail = ‘icon’”).public\_filename, :class =\> “thumbnail”) %\>

Just had a chance to try this. It works brilliantly! In the page admin,  
I can simply drag the link from “Attachments” section and add the size I  
want to the end of the file name. Ex:

[http://domain.com/image\_small.jpg](http://domain.com/image_small.jpg)

Since I use Textile, I then add the image tag (!) and float it right  
like so:

!\>[http://domain.com/image\_small.jpg](http://domain.com/image_small.jpg)!

Thanks for your help, Dominic!
