cfdirectory output different html based on filter

134 views Asked by At

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

3

There are 3 answers

2
James A Mohler On BEST ANSWER

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

2
rrk On

You could simply do the following.

<cfif listLast(fileList.name, '.') NEQ 'mp4'>
2
Sev Roberts On

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>