Recognize image or video in a template

Hi,
I’m redesigning a photography magazine and my current template for displaying the photographers projects is listed here.

The template search inside a folder the project.txt and the images. If a project doesn’t have images, kirby fails and give me some errors.

Not all the photographer send us the images, and sometimes I need to publish a video instead of a gallery of images. I’m using fotorama and there is an option to insert videos but I don’t know how to bypass the template to avoid errors.

Do you have any suggestions on how to fix this issue?

Where does your code throw the error and what sort of error? If there are no images, nothing should happen.

You are right as usual :slight_smile:
I’ve tried now and the projects.php works even if in the folder there are no images.

The problem is on the template on top, the issue.php.

I get this error.

Well, yes, that is not surprising,if the subpage does not have an image, this code throws an error. You always have to check if an image exists before using a function that requires a certain type of object, in this case, the thumb() function wants an image object, and if it does not get it, it complains.

Solution: always check first:

<?php if($image = $subpage->image()): ?>
   <?php echo thumb($image, array('width' => 300, 'height' => 200, 'crop' => true)); ?>
<?php endif ?>

(I think I need a text expander for this text block :wink:, because I keep repeating myself)

1 Like

I will never stop thanking you :blush:

1 Like

Is there a way to show the thumbnails in the issue.php pages but don’t show it in the project.php?

Maybe I can use a video.png thumbnail and check the name in the project.php. If exist doesn’t show the image. Right?

Äh, yes, you are not using thumbnails in your project.php? Those templates are completely independant…

You mean, if you have a video thumbnail you don’t want it to show up in the image list? Yes, than you can filter it out:

$images = $page->images()->filterBy('filename', '!=', 'video.png');

I want to put a video.jpg placeholder for the issue template, where all the projects are displayed, like this:

but I don’t want to display the video.jpg in the project template, because i this case there is a video player (e.g. vimeo):

I wrapped the div that contains the fotorama loop with your suggestion, but doesn’t work:

 <?php if($images = $page->images()->filterBy('filename', '!=', 'video.jpg')): ?>

   <div class="text">

    <div class="fotorama"
    data-width="100%"
    data-ratio="4/3"
    data-allowfullscreen="native"
    data-arrows="true"
    data-click="true"
    data-swipe="true"
    data-keyboard="true"
    data-transition="crossfade"
    data-hash="true"
    >

    <?php foreach($page->images()->sortBy('sort', 'asc') as $image): ?>
      <img src="<?php echo $image->url() ?>" 
      data-caption="<?php echo html($image->caption()) ?> <?php echo html($image->photographer()) ?>" 
      data-full="<?php echo $site->url() . '/content/' . $page->diruri(). "/full/" . $image->filename(); ?>" >
    <?php endforeach ?>
    
  <?php endif ?> 

As I said before: I’m not a good developer :frowning:

Solved.

Just added and else…

<?php if($images = $page->images()->filterBy('filename', '!=', 'video.jpg')): ?>
  <?php else: ?>
     [...]
  <?php endif ?>

I must admit that I don’t quite see the purpose of your if-else statement…

Without the else doesn’t show the video, but the video.jpg.
Anyway, I put a VideoUrl: field with url to the Vimeo video and then this in the projects code:

  <?php if($images = $page->images()->filterBy('filename', '!=', 'video.jpg')): ?>
     
    <a href="<?php echo $page->VideoUrl() ?>">Video</a>

  <?php else: ?>
    <?php foreach($page->images()->sortBy('sort', 'asc') as $image): ?>
      <img src="<?php echo $image->url() ?>" 
      data-caption="<?php echo html($image->caption()) ?> <?php echo html($image->photographer()) ?>" 
      data-full="<?php echo $site->url() . '/content/' . $page->diruri(). "/full/" . $image->filename(); ?>" >
    <?php endforeach ?>

  <?php endif ?>

In this case I’m using fotorama to manage the video :slight_smile:

1 Like

Doesn’t work. It shows the video, but now projects that contains a gallery doesn’t show it :cry:

The first if-statement will always return true, no matter if there is a video.jpg or not, because $images will always be a collection object, even if the object does not contain any elements. But there is hope:

<?php 
$image = $page->image('video.jpg');
if($image) ?>
    <a href="<?php echo $page->VideoUrl() ?>">Video</a>
<?php else: ?>
    <?php foreach($page->images()->sortBy('sort', 'asc') as $image): ?>
      <img src="<?php echo $image->url() ?>" 
      data-caption="<?php echo html($image->caption()) ?> <?php echo html($image->photographer()) ?>" 
      data-full="<?php echo $site->url() . '/content/' . $page->diruri(). "/full/" . $image->filename(); ?>" >
    <?php endforeach ?>

 <?php endif ?>
2 Likes