If file type = “.x”

#1

I would like to show or a hide a div if a certain filetype exist on my page.

I was looking at the function “file” without success. Any ideas how I can make this work?

if($file = $page->file(’*.woff’)):
// hide/show div
endif;

0 Likes

#2

I think thats looking for a file literally called *.woff, rather then respecting the wild card.

I think you need to loop through all the files and filterby() to find the woff’s. Then react accordingly.

extension() might be useful, too.

0 Likes

#3

Totally off the top of my head but something like this should do it…

Find the woff files (I would probably use a collection for this, since it sounds like you might need to reused it).

<?php $woffhunt = $page->files()->filterBy('filename', '*=', '.woff'); ?>

Then you can add the class to the div you want to show hide…

<div class="<?= isset($woffhunt) ? 'showme' : ''?>">

or slightly more readable with the echo helper:

<div class="<?php e(isset($woffhunt), 'showme') ?>">
0 Likes

#4

Using isset() won’t give you the desired result here, because it would only return false if the variable is not set or returns NULL, which will not be the case in case of an empty collection.

<?php $woffExists = $page->files()->filterBy('filename', '*=', '.woff')->count(); ?>

<div class="<?= $woffExists ? 'show' : ''  ?>">

0 Likes

#5

Ah… i used isset() because thought the first bit need some kind of test, didn’t realise the variable alone was enough. Every day is a school day :slight_smile: :school::school_satchel:

0 Likes

#6

Note that I also changed what I stored in the variable (count()).

If you want to be more explicit:

<div class="<?= $woffExists > 0 ? 'show' : ''  ?>">
0 Likes

#7

fantastic thank you!

0 Likes