Search code examples
csscoldfusioncfml

Setting a CSS class by URL with ColdFusion


In the code below, I'm looking to set the li class as "active" pro grammatically with ColdFusion based on the url that I'm currently on. So in this example, I've landed at main.cfm and need to set that specific li class to active. If I'm at www.mysite.com/review.cfm I'd like to set that class as "active".

<div id="menu">
    <ul>
        <li class="active"><a href="main.cfm">Main</a></li>
        <li class=""><a href="review.cfm" title="">Review</a></li>
        <li class=""><a href="https://www.mysite.com">My Site</a></li>
        <li class=""><a href="reporting">Reporting</a></li>
        <li class=""><a href="#">Logout</a></li>
    </ul>
</div>

Solution

  • ColdFusion 9:

    <cfset class = ListLast(cgi.Script_Name, '/') EQ "main.cfm" ? "active" : "">
    <li class="#class#"><a href="main.cfm">Main</a></li>
    

    ColdFusion 8 or below, use good old <cfif>

    <cfset class = "">
    <cfif ListLast(cgi.Script_Name, '/') EQ "main.cfm">
       <cfset class = "active">
    </cfif>
    <li class="#class#"><a href="main.cfm">Main</a></li>
    

    UPDATE: OR as KRC suggested in the comment (ColdFusion 8 or below):

    <li IIF(ListLast(cgi.Script_Name, '/') EQ "main.cfm", DE('class="active"'), '')>
      <a href="main.cfm">Main</a>
    </li>