need if else statement for images [SOLVED]

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
Post Reply
darksaga
Forum Members
Forum Members
Posts: 10
Joined: Sat Nov 07, 2009 8:42 pm

need if else statement for images [SOLVED]

Post by darksaga »

Hey everybody,

I have a few co-workers who have use CMSMS and got me on the bandwagon.  I just re-built my own site using CMSMS and it's been a smooth transition so far, but I've run into a bit of problem.

My template is the same for the home and internal pages for the site, except the banner image changes on several of the pages.  I'm trying to find a PHP script or an "if then" statement where I could tell CMS to display a certain image for a certain page instead of having 10 templates, one for each page.

Something along the lines of assigning a specific image to a specific page, and then have a default image for the rest of the pages.

Since I don't know PHP at all, I was hoping someone could help me.

Thanks a lot for your help,

K
Last edited by darksaga on Mon Nov 23, 2009 2:20 am, edited 1 time in total.
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: need if else statement for images

Post by Dr.CSS »

You say "banner image" is this like ad image or just like the header image thing, if header image you can do a class on the container/header box like class="{$page_alias}" then each header will have a different class that CSS can use to put diff. images in...
Peciura

Re: need if else statement for images

Post by Peciura »

You can place address to image to "Extra Page Attribute 1" of appropriate page.
Use code in template.

Code: Select all

{if !empty($content_obj->GetPropertyValue('extra1'))}
<img src="{$content_obj->GetPropertyValue('extra1')}"/>
{else}
<img src="default_image.jpg"/>
{/if}
More info about "{if}" tag at
http://smarty.net/manual/en/language.function.if.php
Last edited by Peciura on Sun Nov 15, 2009 11:18 am, edited 1 time in total.
darksaga
Forum Members
Forum Members
Posts: 10
Joined: Sat Nov 07, 2009 8:42 pm

Re: need if else statement for images

Post by darksaga »

Tell me if this will work:

{if $page_alias|truncate:4:"" == "home"}
 {elseif $page_alias|truncate:4:"" == "snow"}
   {elseif $page_alias|truncate:8:"" == "landscap"}
     {elseif $page_alias|truncate:3:"" == "sod"}
       {else} {/if}

Then I just apply the image to the body class - right?  One of my friends sent this to me.  What's the "truncate" function for?
Last edited by darksaga on Sun Nov 08, 2009 1:47 am, edited 1 time in total.
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm

Re: need if else statement for images

Post by Russ »

I'm with mark on this one, you are making this way to complicated! His idea is simple and very easy to change with CSS. I think there are even some "Banner" modules in the forge you could take a look at?
Peciura

Re: need if else statement for images

Post by Peciura »

darksaga: Yes it works.
What's the "truncate" function for?
Your friend has 5 page groups (home, snow, land, sod , and default one) and he uses different theme( or just background) for each group. To set what group page belongs to he adds appropriate prefix (or not for default group).

It is good example. But you could use something more simple:

Code: Select all

{$content_obj->GetPropertyValue('extra1')|default:"/default/image/path.jpg"}
In any way smarty makes your job a piece of cake.
darksaga
Forum Members
Forum Members
Posts: 10
Joined: Sat Nov 07, 2009 8:42 pm

Re: need if else statement for images

Post by darksaga »

Peciura wrote: darksaga: Yes it works.
What's the "truncate" function for?
Your friend has 5 page groups (home, snow, land, sod , and default one) and he uses different theme( or just background) for each group. To set what group page belongs to he adds appropriate prefix (or not for default group).

It is good example. But you could use something more simple:

Code: Select all

{$content_obj->GetPropertyValue('extra1')|default:"/default/image/path.jpg"}
In any way smarty makes your job a piece of cake.
Now remember I don't know any PHP, so bear with me.

{$content_obj->GetPropertyValue('extra1')|default:"/default/image/path.jpg"}

I have 9 images which are going on specific pages, then I have 1 image for the home page and 1 image for the rest of the pages as a default.

This is the only thing that changes from page to page.  With your example, how do I match up the page with it's specific page alias?

Still thinking this over. . .
Peciura

Re: need if else statement for images

Post by Peciura »

1. On page "Options" tab you will find you will find field "Extra Page Attribute 1:".
2. For every "specific" page write url to appropriate image, the same thing do with home page.
3. Open template where your banner is
and paste smarty tag "{$content_obj->GetPropertyValue('extra1')|default:"/default/image/path.jpg"}"

To inspect available variables use tags:
{get_template_vars}, {dump item=$entry}, {$entry|print_r}

"$entry" represents most things "{get_template_vars}" shows to you.
darksaga
Forum Members
Forum Members
Posts: 10
Joined: Sat Nov 07, 2009 8:42 pm

Re: need if else statement for images

Post by darksaga »

Peciura wrote: 1. On page "Options" tab you will find you will find field "Extra Page Attribute 1:".
2. For every "specific" page write url to appropriate image, the same thing do with home page.
3. Open template where your banner is
and paste smarty tag "{$content_obj->GetPropertyValue('extra1')|default:"/default/image/path.jpg"}"

To inspect available variables use tags:
{get_template_vars}, {dump item=$entry}, {$entry|print_r}

"$entry" represents most things "{get_template_vars}" shows to you.
Ok, so here's what I did - I followed your directions and on each page put the path to each image in the images folder:

images/contextual-business.jpg

Then in the templates section, put the smarty tag with the path to the default image I want to use:


     
       
       
       
     
   
    {$content_obj->GetPropertyValue('extra1')|default:"images/contextual-default.jpg"}
 
   

And now it's not showing any images on any of the pages.

I know I'm missing something, but don't really know for sure. This is supposed to pull the image in the "extra1" field from the options menu, otherwise, it should put in the default image - right?

Thanks for the continued help!


K
Peciura

Re: need if else statement for images

Post by Peciura »

Here you go.
Check how images are changed at the very top of the page (visit pages in the same submenu)
test2.lvjc.lt/index.php?page=apie-lvjc&hl=en_US

It is all "Smarty" - noting more.
Attachments
top_banner_2.jpg
top_banner_1.jpg
jack4ya
Power Poster
Power Poster
Posts: 294
Joined: Thu Oct 19, 2006 10:07 am

Re: need if else statement for images

Post by jack4ya »

Have you considered using a modified content_image and use it directly in the layout then?

It does mean that the user has to / can select the image themselves, and it will be used the in the layout:



Off course this is not proper symantics, but a backend user, can select which headerimage should be used, and the can alter it themselves. If the don't select one I have a fallback image in the attached css.

One catch to this:
I had to alter the content_image tag.
I stripped the width, height, path etc.
I only want the image file name outputted, and not the complete html tag.

so only
/images/headers/file.jpg
instead of
darksaga
Forum Members
Forum Members
Posts: 10
Joined: Sat Nov 07, 2009 8:42 pm

Re: need if else statement for images

Post by darksaga »

Peciura wrote: Here you go.
Check how images are changed at the very top of the page (visit pages in the same submenu)
test2.lvjc.lt/index.php?page=apie-lvjc&hl=en_US

It is all "Smarty" - noting more.
WOAH! That is pretty slick.  Got it working, and I know I put solved in the topic title, but one more thing (not a major thing) how do I put alt tags on the images which are pulling from the "attribute 1" line?

Thanks a ton for helping with this - I really appreciate it!!


K
Peciura

Re: need if else statement for images [SOLVED]

Post by Peciura »

how do I put alt tags on the images which are pulling from the "attribute 1" line?
You can use:
1. "extra2" field,
2. paste static alt attribute in page template,
3. play with variable modifyer "regex_replace" to extract file name:

Code: Select all

<img 
src="{$content_obj->GetPropertyValue('extra1')|default:"/uploads/images/albumas/bureliai/rusio_galerija_2009/thumb_rusio_galerija_01.jpg"}" 
alt="{$content_obj->GetPropertyValue('extra1')|regex_replace:"/\.\w+?$/":""|regex_replace:"/^(.+\/)+(thumb_*)*/":""|regex_replace:"/_*\d*$/":""}"
/>
This way it looks better but will not work because of line breaks

Code: Select all

<img 
src="{$content_obj->GetPropertyValue('extra1')|default:"/uploads/images/albumas/bureliai/rusio_galerija_2009/thumb_rusio_galerija_01.jpg"}" 
alt="{$content_obj->GetPropertyValue('extra1')
|regex_replace:"/\.\w+?$/":""
|regex_replace:"/^(.+\/)+(thumb_*)*/":""
|regex_replace:"/_*\d*$/":""}
"
/>
4. Think what else you would like to do and do it of course :)

P.S you can read reference and test regular expressions at
regular-expressions.info/javascriptexample.html
Post Reply

Return to “Layout and Design (CSS & HTML)”