2

I want to spit out slightly different html based on a filter condition passed to cfdirectory.

Here is my cfml:

<cfdirectory
    directory="#dirName#"
    name="fileList"
    action="list"
    type="file"
    filter="*.jpg|*.jpeg|*.png|*.gif|*.mp4"
>
<ul id="content">
    <cfoutput>
        <cfloop query="fileList">
            <cfif filter NEQ '*.mp4'> // I guess this is not possible and is throwing the error
                <li class="content image">
                    <img src="img/#name#" />
                </li>
            </cfif>
            <cfelse>
                <li class="video image">
                    <video controls="controls">
                        <source src="img/#name#" type="video/mp4">
                    </video>
                </li>
            </cfif>
        </cfloop>
    </cfoutput>
</ul>

I presume that I cannot simply access the filter inside the cfif, but I am not sure how to skin it. Does it need to be stored in a variable outside of the loop?

Any help much appreciated

lharby
  • 3,057
  • 5
  • 24
  • 56

3 Answers3

5

You could simply do the following.

<cfif listLast(fileList.name, '.') NEQ 'mp4'>
rrk
  • 15,677
  • 4
  • 29
  • 45
  • Hmm, doesn't seem to be working for me. I also tried `ListLast()` as per the docs: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-l/listlast.html I've not used this function before. But can you explain the method? I wonder if I can create an example here: https://cffiddle.org/app/file?filepath=06731cea-4975-4272-8f5b-27b79a3479b0/9999b55f-2eb0-4b54-aab5-636ea3071601/98f16611-afc1-4042-b22b-b566a08b1ed9.cfm – lharby May 21 '20 at 20:09
  • OK I have made this: https://cffiddle.org/app/file?filepath=80342660-f449-4c66-84c4-8e7fe026715a/3d875b54-03fd-4d48-8670-0a0fe429b74d/a0a1e286-72cf-41e5-bd9c-f17ba10fb5fd.cfm It seems to be working, but I am not accessing `list.name` in the same way. – lharby May 21 '20 at 20:50
4

You haven't told us what error you're actually getting, but if the code in the question really is the code you're using, then your problem is not the filter, it's this:

        </cfif>
        <cfelse>

Remove the spurious </cfif> and re-run, and if you still get an error after that then add it to the question.

Edit: You'll still find that reference to the filter won't work but RRK's listLast answer will do just fine - also in case you hadn't already, make sure your directory is actually a proper path, eg:

<cfset dirName = expandPath('img')>
<cfdirectory
    directory="#dirName#"
    name="fileList"
    action="list"
    type="file"
    filter="*.jpg|*.jpeg|*.png|*.gif|*.mp4"
>
<ul id="content">
    <cfoutput>
        <cfloop query="fileList">
            <cfif listLast(fileList.name, '.') NEQ 'mp4'>
                <li class="content image">
                    <img src="img/#EncodeForUrl(name)#" />
                </li>
            <cfelse>
                <li class="video image">
                    <video controls="controls">
                        <source src="img/#EncodeForURL(name)#" type="video/mp4">
                    </video>
                </li>
            </cfif>
        </cfloop>
    </cfoutput>
</ul>
Sev Roberts
  • 1,295
  • 6
  • 7
  • 1
    I like the `encodeforURL()` . It should be included. I have have files with spaces in their names and that needs to be taken care of. – James A Mohler May 22 '20 at 02:37
  • Ha good spot, luckily all my files are lowercase with no spaces, but I will update the script to capture this, thank you. – lharby May 22 '20 at 07:21
3

Lets take this step by step

<ul id="content">
   <!--- Normally you want to loop over query in a cfoutput. No need for both --->
    <cfoutput query="fileList">
            <cfif listlast(fileList.name, '.') NEQ 'mp4'>

                <li class="content image">
                    <img src="img/#name#" />
                </li>

            <cfelse>
                <li class="video image">
                    <video controls="controls">
                        <source src="img/#name#" type="video/mp4">
                    </video>
                </li>
            </cfif>

    </cfoutput>
</ul>

More details

 <cfif listLast(fileList.name, '.') NEQ 'mp4'>

fileList.name means: We want to look at name. But not just any name that could exist in any scope. We want name that is associated with fileList

listLast() means take the string variable, separate it by commas and tell me what the last item is. Return that as a string.

listLast(..., '.') You know that part about commas, yeah let's use periods instead. In other words, what is the last part of the file name after the last ..

If that is not mp4, then ...

James A Mohler
  • 11,060
  • 15
  • 46
  • 72