How to show only folder and documents (files), not images on front end

<%- if @topic_view.topic.tags.present? %>
<%= t 'js.tagging.tags' %>: <%- @topic_view.topic.tags.each do |t| %> <%= t %> <%- end %>
<% end %>

Silverstripe Version: 4.3

How can I list only documents - and NO images - in their folder on the front end?

_I am able to show all folders and their files (documents and images) on the front end. My difficulty is to filter the files to get only documents and not the images.
I searched the entire internet and tried every suggestion I could find. I guess I have overlooked something and the solution is really easy and elegant…

Something like this e.g. doesn’t work:

in template:

<% if $ClassName != "SilverStripe\Assets\Image" %>

Has anybody any idea?

Thanks

I’m surprised that doesn’t work. Maybe try printing out $ClassName in your loop and see what you’re getting? Here are some other things to try:

<% if $ClassName != "Image" %>
<% if $ClassName.ShortName != "Image" %>
<% if not $IsImage %>
<% if not $Width %>

You’d probably be better off filtering the list though so you’re only looping over non Images. Something like <% loop $Files.exclude('ClassName', 'SilverStripe\Assets\Image') %> although if that doesn’t work within a loop it might not work to filter the list either.

Thank you so much, JonoM!
I am one step closer, as it worked except for the first folder found.

I tried all of the above and even one I forgot to mention earlier (<% loop $Children.filter(‘ClassName:not’, ‘SilverStripe\Assets\Image’) %> which I think I found somewhere in the docus , but only the
<% loop $Files.exclude('ClassName', 'SilverStripe\Assets\Image') worked, but I needed the Children version of it:

<% loop $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>

worked. It even worked in the if block, so the folder wouldn’t show if if only contained of images:

<% if $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>

Any idea why the first folder still shows images and it worked for all the other?

Can you post your full template please? Hard to say why that would happen without more context.

Sure:
PageController:

public function DocumentFolderAndFiles(){
        
        $arrayList = ArrayList::create();
        $files = Folder::get()->filter(array(
            "ClassName" => "SilverStripe\Assets\Folder",
            "ParentID" => "0"
        )); 

        foreach($files as $file) {

            if($file->canView()) {
                $arrayList->push($file);
            }
        }

        return $arrayList;
       
    } 

template:

<% include HeaderContent %>
<div class="documents-list">
<% if DocumentFolderAndFiles %>
<% loop DocumentFolderAndFiles %>
    <% if $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
        <strong><% if $Link %><a href="$Link">$Title</a><% else %>$Title<% end_if %></strong>
        <ul>
        <% loop $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
            <li>
            <% if $Link %><a href="$Link">$Title</a><% else %>$Title<% end_if %>
            <% if $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                <ul>
                <% loop $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                    <li>
                        <% if $Link %><a href="$Link">$Title</a><% else %>$Title<% end_if %>
                        <% if $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                        <ul>
                        <% loop $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                            <li>
                                <% if $Link %><a href="$Link">$Title</a><% else %>$Title<% end_if %>
                                <% if $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                                    <ul>
                                    <% loop $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                                        <li> 
                                            <% if $Link %><a href="$Link">$Title</a><% else %>$Title<% end_if %>
                                            <% if $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                                                <ul>
                                                <% loop $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
                                                        <li>
                                                            <% if $Link %><a href="$Link">$Title</a><% else %>$Title<% end_if %>
                                                        </li>
                                                <% end_loop %>
                                                </ul>
                                            <% end_if %>
                                        </li>
                                    <% end_loop %>
                                    </ul>
                                <% end_if %>
                            </li> 
                        <% end_loop %>
                        </ul>
                        <% end_if %>
                    </li>      
                <% end_loop %>
                </ul>
            <% end_if %>
            </li>
        <% end_loop %>
        </ul>
    <% end_if %>
<% end_loop %>
<% else %>
<p>No documents found.</p>
<% end_if %>
</div>

It is a very document ‘rich’ historically grown website ;-), but the image folder are not nested as deep.

Thanks JonoM

I’m not seeing an obvious reason but I can see one improvement to make. Create an include called ChildFileList.ss and put this in it:

<% if $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
    <strong><% if $Link %><a href="$Link">$Title</a><% else %>$Title<% end_if %></strong>
    <ul>
        <% loop $Children.exclude('ClassName', 'SilverStripe\Assets\Image') %>
            <li>
                <% include ChildFileList %>
            </li>
        <% end_loop %>
    </ul>
<% end_if %>

That will include itself as many times as necessary to display the whole tree. Then you can just do:

<% if DocumentFolderAndFiles %>
    <% loop DocumentFolderAndFiles %>
        <% include ChildFileList %>
    <% end_loop %>
<% end_if %>

Then you only need to make changes to your code in one place and can be sure they’re all using identical markup and logic :wink:

Thanks JonoM :smile:,
That was a good point :wink:

This file/image problem is really weird…