htmlcoldfusioncfmlcfloopcfdirectory

cfdirectory output different html based on filter


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


Solution

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