Is it possible to create a custom template for the image block?
Permalink 2 users found helpful
I've copied the default block and setup/applied a custom template but don't understand how you actually pull and render the parameters:
- Image and custom image size
- Image on state
- Link
- Link text
And the simple markup I would like to use:
The default view file only contains:
There's kind of nothing to go off for this block so don't understand how you grab and render the parameters separately so they can be inserted into custom markup.
Any help would be much appreciated.
- Image and custom image size
- Image on state
- Link
- Link text
And the simple markup I would like to use:
<p class="features"><a href="#"><img src="http://placehold.it/140x80" alt="" /><br />Link text</a></p>
The default view file only contains:
echo($controller->getContentAndGenerate());
There's kind of nothing to go off for this block so don't understand how you grab and render the parameters separately so they can be inserted into custom markup.
Any help would be much appreciated.
Thanks mate,
So there's no hope if you don't know PHP?
Cheers
Ben
So there's no hope if you don't know PHP?
Cheers
Ben
I know that there was a discussion about that file a while ago, if you're lucky you might find one, but beside that you need some basic PHP skills!
Here is code I use in a custom template for image block.
Be aware that I use it for a specific functionality but you can get the idea on how to use a custom template for the image block.
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); $im = Loader::helper('image'); $f = File::getByID($fID); $fv = $f->getApprovedVersion(); $path = $fv->getURL(); $linkURL = $controller->getLinkURL(); $title = $fv->getTitle(); $width= $maxWidth; $height = $maxHeight; ?> <?php if($width == 0):?> <a class="modal-trigger-<?php echo $bID;?>" title="<?php echo $title;?>" href="#"> <img src="<?php echo $path;?>" title="<?php echo $title;?>"/> </a> <div class="modal-box modal-box-<?php echo $bID;?>">
Viewing 15 lines of 37 lines. View entire code block.
Be aware that I use it for a specific functionality but you can get the idea on how to use a custom template for the image block.
Awesome mate - works great!
For my future reference, here's the final code I used to generate a thumbnail as well. I couldn't get the if/else statement from your original code to work - it just rendered the first option even though I'd entered a dimension. Anyway, it works great for my needs.
Cheers
Ben
For my future reference, here's the final code I used to generate a thumbnail as well. I couldn't get the if/else statement from your original code to work - it just rendered the first option even though I'd entered a dimension. Anyway, it works great for my needs.
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); $ih = Loader::helper('image'); // load the c5 image helper $img = File::getByID($fID); // get the image $imagePath = $img->getURL(); // get the image path $imageTitle = $img->getTitle(); // get the image title $imgaeThumb = $ih->getThumbnail($img, $maxWidth, $maxHeight); // render thumb using the size entered in the block edit screen $linkURL = $controller->getLinkURL(); // get the link url $linkTitle = $controller->getaltText(); // get the link text ?> <?php // output with markup ?> <p> <a href="<?php echo $linkURL ?>" title="<?php echo $linkTitle ?>" class="upperspace"> <img
Viewing 15 lines of 24 lines. View entire code block.
Cheers
Ben
Can you tell us where you actually put this lot then?
Ta
Ta
You'll want to read up on custom block templates which live here:
In this case, the template goes here:
If you name your template "view.php" and put it at the top-level of the /blocks/image/ directory (out of the templates directory) then your custom template will automatically override the built-in template without having to choose it in the dashboard.
Hope that makes sense - here's some info:
http://www.concrete5.org/documentation/how-tos/designers/custom-blo...
http://www.concrete5.org/documentation/general-topics/custom-templa...
/blocks/block-name/templates/template-name.php
In this case, the template goes here:
/bocks/image/templates/image.php
If you name your template "view.php" and put it at the top-level of the /blocks/image/ directory (out of the templates directory) then your custom template will automatically override the built-in template without having to choose it in the dashboard.
Hope that makes sense - here's some info:
http://www.concrete5.org/documentation/how-tos/designers/custom-blo...
http://www.concrete5.org/documentation/general-topics/custom-templa...
Thanks for the info Ben
I am actually familiar with overrides etc. ... the way
I normally do this is to make a structure like so:
/blocks/<block-name>/templates/<template-name>/view.php
or
/blocks/<block-name>/templates/<template-name>.php
or
/blocks/<block-name>/view.php
The last option actually works fine to automatically override the Image block as you suggest, but the other two options don't seem to work. It does give the option to select the option in the dash which is what I need) but it doesn't get picked up. I will try naming it image.php to see if that makes a difference.
The workaround I used is to create a new Image block with a different name and added that to the Block Types but it was hardly an elegant solution!
I am actually familiar with overrides etc. ... the way
I normally do this is to make a structure like so:
/blocks/<block-name>/templates/<template-name>/view.php
or
/blocks/<block-name>/templates/<template-name>.php
or
/blocks/<block-name>/view.php
The last option actually works fine to automatically override the Image block as you suggest, but the other two options don't seem to work. It does give the option to select the option in the dash which is what I need) but it doesn't get picked up. I will try naming it image.php to see if that makes a difference.
The workaround I used is to create a new Image block with a different name and added that to the Block Types but it was hardly an elegant solution!
Thank you for the snippet!
I added a check, for when the user doesn't set maxWidht and maxHeight:
For just having an output with a responsive image (without link nor sizes) like this:
Here is the modified code:
I added a check, for when the user doesn't set maxWidht and maxHeight:
if(!$maxWidth){$maxWidth=1200;} if(!$maxHeight){$maxHeight=800;}
For just having an output with a responsive image (without link nor sizes) like this:
<div id="..." class="ccm-block-styles"> <img src="image.jpg" alt="text" class="img-responsive"> </div>
Here is the modified code:
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); if(!$maxWidth){$maxWidth=1200;} //set your max width if(!$maxHeight){$maxHeight=800;} //set your max height $ih = Loader::helper('image'); // load the c5 image helper $img = File::getByID($fID); // get the image $imagePath = $img->getURL(); // get the image path $imageTitle = $img->getTitle(); // get the image title $imgaeThumb = $ih->getThumbnail($img, $maxWidth, $maxHeight); // render thumb using the size entered in the block edit screen $linkURL = $controller->getLinkURL(); // get the link url $linkTitle = $controller->getaltText(); // get the link text ?> <?php // output with markup ?> <img src="<?php echo $imgaeThumb->src ?>" alt="<?php echo $linkTitle ?>" class="img-responsive" /> <?php ?>
Thanks for this. I used it to create a simple white border & box shadow style for a theme I'm developing.
You can create a customer template but it's not a 10seconds task I think.
You can still access all the properties you need from a template, you basically have to rewrite "getContentAndGenerate" and put it in your template!
In theory you could also parse the return value of "getContentAndGenerate" and replace those things you want to replace but that's silly, let's not go there...