Custom pagelist template - if no image is set for a page, a thumbnail displays but is the previous page's thumb
Permalink
HI There,
I have a custom pagelist template which pulls a thumbnail from a custom attribute - this works great when an image is selected.
However, if a page doesn't have an image chosen, it seems to display the thumbnail from the previous page.
Here's the code which grabs the image from the custom attribute:
Is there a way to not display an image if one isn't set?
Any help would be much appreciated.
Cheers
Ben
I have a custom pagelist template which pulls a thumbnail from a custom attribute - this works great when an image is selected.
However, if a page doesn't have an image chosen, it seems to display the thumbnail from the previous page.
Here's the code which grabs the image from the custom attribute:
Is there a way to not display an image if one isn't set?
Any help would be much appreciated.
Cheers
Ben
Thanks for that - the only php I know has been picked up working with c5 so it's fairly crappy sorry.
I have this call in the first php block at the top of the page:
Then I thought I'd try to use a conditional when outputting the image in the markup:
But because I've copied and pasted form other examples, I think my syntax is messed up.
BTW, is a conditional the right way to handle the duplicate image issue?
Cheers
Ben
I have this call in the first php block at the top of the page:
// load image helper $imageHelper = Loader::helper("image"); // load page attribute 'project_main_image' and apply thumbnail size $thumbnail = $cobj->getAttribute('main_image'); if ($thumbnail) { $thumbnailObj = $imageHelper->getThumbnail($thumbnail, 240, 155); }
Then I thought I'd try to use a conditional when outputting the image in the markup:
But because I've copied and pasted form other examples, I think my syntax is messed up.
BTW, is a conditional the right way to handle the duplicate image issue?
Cheers
Ben
Ah ok, you want to generate the thumbnail from an image - sorry, I missed that part.
You're pretty much there, but it just needs a bit tidying up.
You definitely want some sort of conditional logic instead of assuming anything. I even go so far as to check that even if the page attribute is set, that it's the right kind of data. There's nothing stopping you/someone else from changing the page attribute's type from a file to ordinary plain text. If that were to happen you could potentially generate a fatal PHP error that could be displayed to the general public, or, if your site is in production mode, screw things up without you knowing why.
Also, skip using the PHP block - just put this directly in your template file to keep everything together.
You're pretty much there, but it just needs a bit tidying up.
You definitely want some sort of conditional logic instead of assuming anything. I even go so far as to check that even if the page attribute is set, that it's the right kind of data. There's nothing stopping you/someone else from changing the page attribute's type from a file to ordinary plain text. If that were to happen you could potentially generate a fatal PHP error that could be displayed to the general public, or, if your site is in production mode, screw things up without you knowing why.
Also, skip using the PHP block - just put this directly in your template file to keep everything together.
<?php $objThumb = NULL; // Get the file object of the image assigned to your page attribute (if any) $objFile = $cobj->getAttribute('image_attr_handle_here'); // If it's a valid file object... if(is_object($objFile) && $objFile instanceof File && !$objFile->error){ // ...load the image helper and generate its thumbnail $ih = Loader::helper('image'); $objThumb = $ih->getThumbnail($objFile, 123, 456); } // If we successfully got a valid thumbnail we write out an HTML <img> tag with the appropriate attributes if(!is_null($objThumb)){ $strHtml = '<img src="' . $objThumb->src . '" width="' . $objThumb->width . '" height="' . $objThumb->height . '" alt="Alt text here" />'; }else{ // Handle your fallback here (eg. Text, a hardcoded default image, etc)
Viewing 15 lines of 20 lines. View entire code block.
Thanks heaps for the reply - the thumbnails now display correctly, but can the markup be rendered only if an image is set?
i.e. I'd like this markup to appear only if an image is set - otherwise nothing:
Hope that makes sense - each time I try to echo the markup into the if statement itself, I get syntax errors.
Cheers
Ben
i.e. I'd like this markup to appear only if an image is set - otherwise nothing:
Hope that makes sense - each time I try to echo the markup into the if statement itself, I get syntax errors.
Cheers
Ben
Change this line in beebs93's code:
...to this:
$strHtml = '<span>Fallback HTML here</span>';
...to this:
$strHtml = '';
Thanks for the reply,
I tried your suggestion but because the final markup is rendered outside the if statement (at the bottom of beebs93's code), the output is rendered even if no image is set - it just renders it unparsed like this:
I think I need to move the above markup into the if conditional. Then leave the else one blank which would render nothing when no image is set - is that right?
The problem is that I run into syntax errors when I try moving the markup into the if statement. I tried removing the extra <?php calls but realised I have no idea what I'm doing.
Sorry if I'm missing something simple.
I tried your suggestion but because the final markup is rendered outside the if statement (at the bottom of beebs93's code), the output is rendered even if no image is set - it just renders it unparsed like this:
I think I need to move the above markup into the if conditional. Then leave the else one blank which would render nothing when no image is set - is that right?
The problem is that I run into syntax errors when I try moving the markup into the if statement. I tried removing the extra <?php calls but realised I have no idea what I'm doing.
Sorry if I'm missing something simple.
Please post your entire template file -- it's getting very difficult to tell what's going on exactly :)
(Note that the forum software prevents attaching files that end in .php, so either ZIP it up or change the extension to .txt)
(Note that the forum software prevents attaching files that end in .php, so either ZIP it up or change the extension to .txt)
Thanks mate,
The page_list template is attached. The code which renders the markup is located further down on line 65.
The page_list template is attached. The code which renders the markup is located further down on line 65.
Ok, I *think* I get it. As you're looping through the list of pages, if a thumbnail has NOT been set you're wanting to skip said page and continue to the next one? If so:
Let me know if I'm getting warmer...
P.S. On line 61, change:
to
as you've got your closing tags backwards.
Let me know if I'm getting warmer...
P.S. On line 61, change:
to
as you've got your closing tags backwards.
Thanks for that.
I don't want to skip the page, I just don't want the image markup appearing so the excerpt text slides over.
You can see what's happening on this page:http://dev.sciasciabrothers.co.nz/steelpipe/index.php/projects/...
As you scroll down, you'll notice some entries don't have images but there's a hole for one. That's because the markup is getting rendered even when no image is set.
What I would like is for the markup to not render if no image is set - so that the text slides over. I'm sure I've done this on a c5 site but I can't find the markup.
From memory, I moved the if statement to where the markup is being rendered so that it contains the <div class="unit unit size2of5"> markup.
But each time I do, I get syntax errors.
Hope this makes sense!
I don't want to skip the page, I just don't want the image markup appearing so the excerpt text slides over.
You can see what's happening on this page:http://dev.sciasciabrothers.co.nz/steelpipe/index.php/projects/...
As you scroll down, you'll notice some entries don't have images but there's a hole for one. That's because the markup is getting rendered even when no image is set.
What I would like is for the markup to not render if no image is set - so that the text slides over. I'm sure I've done this on a c5 site but I can't find the markup.
From memory, I moved the if statement to where the markup is being rendered so that it contains the <div class="unit unit size2of5"> markup.
But each time I do, I get syntax errors.
Hope this makes sense!
Try this (see attached)
Ok, I get it now. Starting from line 29:
<?php //IMAGE THUMBS $objThumb = NULL; // Get the file object of the image assigned to your page attribute (if any) $objFile = $cobj->getAttribute('main_image'); // If it's a valid file object... if(is_object($objFile) && $objFile instanceof File && !$objFile->error){ // ...load the image helper and generate its thumbnail $ih = Loader::helper('image'); $objThumb = $ih->getThumbnail($objFile, 240, 155); } ?> <div class="group project"> <h4><a <?php echo $target ?> href="<?php echo $link ?>"><?php echo $title ?></h4></a> <!-- entry thumbnail -->
Viewing 15 lines of 20 lines. View entire code block.
Thanks guys - much appreciated.
But neither example actually renders the thumbnail - I see the div and link markup but no image.
Is this the bit that's supposed to render the image?
But neither example actually renders the thumbnail - I see the div and link markup but no image.
Is this the bit that's supposed to render the image?
<?php echo $strHtml; ?>
I modified my last example - I accidently snipped out the part that actually echoed the <img> thumbnail
Thanks for that works great!
If I could mark both answers as best I would - in the end I understood beebs93 answer more so have chosen his.
Thanks heaps for both your answers - very much appreciated.
Cheers
Ben
If I could mark both answers as best I would - in the end I understood beebs93 answer more so have chosen his.
Thanks heaps for both your answers - very much appreciated.
Cheers
Ben
That's cool -- he needs the karma more than me ;)
(Also he put in more effort here explaining it -- nice work!)
Glad you were able to solve the problem.
-Jordan
(Also he put in more effort here explaining it -- nice work!)
Glad you were able to solve the problem.
-Jordan
Glad you got it working :)
Plus, I agree with Jordan; he has enough karma to ensure he'll be at the cool table in the afterlife (the one where Johnny Cash, John Wayne and Johnny Carson sit) ;)
Plus, I agree with Jordan; he has enough karma to ensure he'll be at the cool table in the afterlife (the one where Johnny Cash, John Wayne and Johnny Carson sit) ;)
Nice...
thanks it works:)
I'm not sure on your setup, but you may want to modify this so if the page attribute isn't set that the entire link is not shown, but that's up to you.
Hope this helps :)