I've tried many options and searched for many solutions but I just can't seem to get this to work. It's probably something minor that I'm missing.
I have a menu that is in a cfinclude file, the menu has the following code to trigger an active state dynamically:
<a href="../index.cfm" class="#iIF( thispage IS "index.cfm", DE( "nav-item nav-link px-3 active" ), DE( "nav-item nav-link px-3" ))#">Home</a>
When I call this menu from another page with <cfinclude> it works fine using the following code at the top of the page:
<cfset variables.thispage = GetFileFromPath(GetCurrentTemplatePath())/>
The template path would be 'index.cfm' and the active state would be triggered if I am on that page.
However, I have a template page that contains the header and footer of my site named front.cfm
Within the header portion of front.cfm, I call the menu with <cfinclude>. It works fine here.
In my index page, the template is called and here is where it breaks, I keep getting the following error code:
"Variable THISPAGE is undefined"
It is defined in the index.cfm page, but coldfusion keeps looking for it in the front.cfm page, so if I change the code on my menu to "#iIF( thispage IS "front.cfm..."
and set the variables.thispage on the front.cfm page, the active state is triggered in index.cfm.
After two days of experimenting and searching, I am still not able to get the variable 'thispage' to pass to front.cfm and from there to index.cfm where it is read.
Any help will bu much appreciated.
Efx, to be clear, your problem has nothing to do with use of iif.
Instead, it’s your use of getcurrenttemplatepath. It’s doing what it’s supposed to. You should instead be using use getbasetemplatepath. The docs for the function do in fact clarify this. From the doc for the function you’re using:
“If the function call is made from a page included with a cfinclude tag, this function returns the page path of an included page. Contrast this with the GetBaseTemplatePath function, which returns the path of the top-level page, even if it is called from an included page.”
That said, I appreciate that you may have just gotten this code (or code like it) from someone else, and maybe didn’t know about the other function. There are hundreds, of course, reflecting the evolution of the language over time. But let that be a lesson: that often you will find what you need in the docs :-)...despite most people throwing shade on them all the time.
Finally, as for your use of the iif function, I’ll say as well that is also a very old and rarely needed one, like the DE (that it relies on). In your case, given that you’re just using this to set attributes values in an HTML tag, there’s no apparent reason you couldn’t just use a CFIF instead. I suppose some may be averse to seeing CF tags within an HTML tag, but CF won’t care. There are, though, times when you may need to “output” something conditionally, and within a CF tag or script, and in those cases IIF can be helpful. I’m just sayig that in your case, it seems to be adding needless complexity (few understand what IIF and DE are doing and would be confused by the use here).
Hope that’s helpful.
getbasetemplatepath did the trick and its working, I have to set the variable both in the include and in the page it is included in.
It's true that I picked up the code from a google search on how to get this done...lesson learned as the docs were straight forward and easy to understand, what discourages me from them usually is that there is a lot of more complicated info with each function and I get lost on where to start.
I was initially using cfif but changed to IIF because there was less coding involved and figured that would make the pages run faster.
I guess it would be a good idea to revert now.
I will look into wolfshade's suggestion for further development.
To add to what Charlie has stated, you can also use cgi.script_name. For example, assuming you have an index.cfm in the root, with let's say three folders for separating sections of your site, so the directory looks like:
If you are in /contact/index.cfm, then cgi.script_name (without using getcurrenttemplatepath or getbasetemplatepath) will be "/contact/index.cfm", and can use a CFIF to set a variable to the class name values, and use the variable inside the class attribute of the anchor.
Just my two cents.
^ _ ^