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:
<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.

View Replies: View Best Answer
Remo replied on at Permalink Reply
Remo
Unfortunately the image block doesn't follow the MVC concept very well...

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...
cmscss replied on at Permalink Reply
Thanks mate,

So there's no hope if you don't know PHP?

Cheers

Ben
Remo replied on at Permalink Reply
Remo
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!
PauloCarvalhoDesign replied on at Permalink Best Answer Reply
PauloCarvalhoDesign
Here is code I use in a custom template for 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;?>">

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.
cmscss replied on at Permalink Reply
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.
<?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

Cheers

Ben
keeasti replied on at Permalink Reply
keeasti
Can you tell us where you actually put this lot then?
Ta
cmscss replied on at Permalink Reply
You'll want to read up on custom block templates which live here:
/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...
keeasti replied on at Permalink Reply
keeasti
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!
frenx replied on at Permalink Reply
Thank you for the snippet!
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 ?>
C5ThemeTeam replied on at Permalink Reply
C5ThemeTeam
Thanks for this. I used it to create a simple white border & box shadow style for a theme I'm developing.