need if else statement for images [SOLVED]
need if else statement for images [SOLVED]
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
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.
Re: need if else statement for images
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...
Re: need if else statement for images
You can place address to image to "Extra Page Attribute 1" of appropriate page.
Use code in template.
More info about "{if}" tag at
http://smarty.net/manual/en/language.function.if.php
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}
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.
Re: need if else statement for images
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?
{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.
Re: need if else statement for images
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?
Re: need if else statement for images
darksaga: Yes it works.
It is good example. But you could use something more simple:
In any way smarty makes your job a piece of cake.
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).What's the "truncate" function for?
It is good example. But you could use something more simple:
Code: Select all
{$content_obj->GetPropertyValue('extra1')|default:"/default/image/path.jpg"}
Re: need if else statement for images
Now remember I don't know any PHP, so bear with me.Peciura wrote: darksaga: Yes it works.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).What's the "truncate" function for?
It is good example. But you could use something more simple:In any way smarty makes your job a piece of cake.Code: Select all
{$content_obj->GetPropertyValue('extra1')|default:"/default/image/path.jpg"}
{$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. . .
Re: need if else statement for images
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.
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.
Re: need if else statement for images
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: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.
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
Re: need if else statement for images
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.
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.
Re: need if else statement for images
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
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
Re: need if else statement for images
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?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.
Thanks a ton for helping with this - I really appreciate it!!
K
Re: need if else statement for images [SOLVED]
You can use:how do I put alt tags on the images which are pulling from the "attribute 1" line?
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*$/":""}"
/>
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*$/":""}
"
/>

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