Alter block output when page template changes

Permalink
I have a block (built via Designer Content), part of which contains an image, hard-coded into the html.
When I switch to a different page template, I'd like the image in the block to automatically change to a different coloured one.

What's the easiest way of telling the block it's sitting in a different page template? Do I need to set a php variable in each template that I can look for within the block's view.php or something? (Please go easy as PHP still not my strong point.)

I'm not sure if this question is related to the "Two sides of one site two backgrounds" question Alan asked earlier, which hasn't been answered yet?

Many thanks in advance...

prestressed
 
12345j replied on at Permalink Best Answer Reply
12345j
id use page attributes. Usinghttp://www.concrete5.org/community/forums/customizing_c5/color-pick...
make a color attribute for each page. Then in the blocks view.php get the color from the attribute, and using that color select an image. Seems fairly straightforward.
seehttp://www.concrete5.org/documentation/developers/pages/overview...
for some php help.
prestressed replied on at Permalink Reply
prestressed
thanks, I will give it a go tomorrow & let you know if I fix it... need to sleep now!

I suppose once I do this it will make my separate-template redundant? I only made two templates because I wanted 2 different coloured pages, so they reference different CSS files. But I suppose I could also add in the relevant CSS based on the value of the page attribute? Is that right?

(definitely need sleep first before attempting that)
12345j replied on at Permalink Reply
12345j
yeah, thats the idea, that the css would automatically change.
prestressed replied on at Permalink Reply
prestressed
OK it is all working, brilliant. Here for posterity is what I did in case it helps others.

I was a bit confused by the color attribute thread you referred to earlier (esp. as the bottom of that thread suggested it may not work in latest versions) so I just made a basic Select attribute.

Then I put some code in my default template which calls the extra stylesheet when that attribute is selected, like this:
<?php $templateselect = $c->getAttribute('templateselect');
if ($templateselect == 'orange') {?>
<link rel="stylesheet" media="all" type="text/css" href="<?php echo $this->getStyleSheet('orange.css')?>" />
<?php } ?>


Finally I edited the view.php of my designer content block along these lines:
<?php $templateselect = $c->getAttribute('templateselect');
if ($templateselect == 'orange') {?>
<img alt="alttext" src="/files/etc/orangeimage.png" />
<?php } 
else {?>
<img alt="alttext" src="/files/etc/blueimage.png" />
<?php } ?>


not sure it's very good PHP so feel free to correct my code, but it all seems to work.

Now I have a redundant page type - once I have assigned all my 'orange page type' pages the new templateselect attribute, I'll try to delete that page type.

Worth noting I had an issue before where I couldn't delete a page type because there was a page with that type in the trash - this thread was useful:http://www.concrete5.org/community/forums/chat/emptying-trash-delet...
12345j replied on at Permalink Reply
12345j
that seems about right. You definitely don't need a color picker, that would be only if you needed the solution to be a bit more automatic. But if its only a couple main colors then select is probably easier.