how to combine cfif and cflnclude in Coldfusion - coldfusion

I have a ColdFusion program which needs to switch to another page, but which page depends on a condition. It would be nice to write code such as
<cfif a GT 5>
<cfinclude template = 'pageone.cfm'>
<cfelse>
<cfinclude template = 'pagetwo.cfm'>
</cfif>
The problem is, if I include pageone or pagetwo the final </cfif> disappears from ColdFusion's radar, and I get an error. Can anyone suggest a way to accomplish what I'm trying to do above?

Change your approach to something like this:
<cfset a = 6>
<cfif a GT 5>
<cfset templateTarget = 'pageone.cfm'>
<cfelse>
<cfset templateTarget = 'pageTwo.cfm'>
</cfif>
<cfinclude template = '#templateTarget#'>

Related

In CF, can I call a custom tag using a variable for its name?

I would like to call a custom tag using a variable in it's name. Like this
<cfset slist = 'product_categories'>
<cf_cu_show_#slist#>
This gives me an error on the #. The custom tag cu_show_product_categories is present and working when I call it the conventional way.
The idea is to build a list to loop through, calling several custom tags.
<cfset slist = 'product_categories'>
<cfif a = 'blogs'>
<cfset slist = listAppend(slist,"blogs")>
</cfif>
<cfif b = 'posts'>
<cfset s_list = listAppend(slist,"last_posts")>
</cfif>
<cfloop list="#slist#" index="i">
<cf_cu_show_#i#>
</cfloop>
I tried to google, but cannot find anything useful. Any help would be appreciated.
As you already discovered, using a variable name in calling the custom tag is invalid. The way around this is to call the custom tag using the <cfmodule> syntax instead. In your first scenario, you would call it like this.
<cfset slist = 'product_categories'>
<cfmodule template="cu_show_#slist#.cfm">
In the lower example, you would modify your code as such.
<cfset slist = 'product_categories'>
<cfif a = 'blogs'>
<cfset slist = listAppend(slist,"blogs")>
</cfif>
<cfif b = 'posts'>
<cfset s_list = listAppend(slist,"last_posts")>
</cfif>
<cfloop list="#slist#" index="i">
<cfmodule template="cu_show_#i#.cfm">
</cfloop>
Here's the documentation link on how to use <cfmodule>.
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-m-o/cfmodule.html
I also found another decent link where they demonstrate your scenario in which you need to supply the tag name dynamically as seen here at https://flylib.com/books/en/2.375.1.420/1/

Coldfusion set time out in .cfc page?

I have one .cfc that I use for all communication between client and server code. This cfc page has about 10 different function. Each function has different purpose and I have queries for Select, Insert, Update and Delete. I'm wondering if I should set timeout on the top of the .cfc page inside cfcomponent tag or this should be set inside of the each function or do I even need this? In our current system we have some many error messages like: The request has exceeded the allowable time limit Tag: CFQUERY.
I would like to prevent any similar error messages in my app. Here is example of my cfc page:
<cfcomponent>
<cfset currentDate = DateFormat(Now(),'mm/dd/yyyy')>
<cfset currentTime = TimeFormat(Now(),'hh:mm tt')>
<cfinvoke component="appEntry" method="getRecord" returnvariable="CHKAccess">
<cfinvokeargument name="user" value="userdata"/>
<cfinvokeargument name="app" value="myApp"/>
</cfinvoke>
<cfset adminAccess = false>
<cfset userAccess = false>
<cfif CHKAccess.RecordCount EQ 1>
<cfif CHKAccess.pd_hfmAccess EQ 'A'>
<cfset adminAccess = true>
</cfif>
<cfif CHKAccess.pd_hfmAccess EQ 'U'>
<cfset userAccess = true>
</cfif>
</cfif>
<cffunction name="getData" access="remote" output="true" returnformat="JSON">
<cfargument name="keyVal" type="string" required="true">
<cfset fnResults = structNew()>
<cfif userAccess>
<cfquery name="getRec" datasource="tes">
SELECT some columns
FROM Test
</cfquery>
<cfset fnResults.status = "200">
<cfelse>
<cfset fnResults.status = "400">
<cfset fnResults.message = "Invalid access attempt.">
</cfif>
<cfreturn fnResults>
</cffunction>
<!--- More functions below --->
</cfcomponents>
If anyone have suggestion what would be the best fix please let me know. Thank you.
You should set the requesttimeout in the method that contains that long-running cfquery.
You don't want to "punish" all methods for just one method. If you set it for all, how do you know which one is slow and which one is okay, unless you don't care?

Any way to get multiple CFIFs displaying inside CFSET?

At the moment I have this code which is working great -
<CFIF (DailyCount MOD 2) EQ 0>
<CFSET via = '<td style="background-color: DBEFB6;">#src#<br><font color="blue">#get_info.csuseragent#</font></td>'>
<CFELSE>
<CFSET via = '<td>#src#<br><font color="blue">#get_info.csuseragent#</font></td>'>
</CFIF>
In the #get_info.csuseragent# field, at the moment it is displaying the whole UA string eg - Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20130406 Firefox/23.0
I want to put the code below where the #get_info.csuseragent# tag is so it just displays the broswer/device. This works normally out of this setup but I cannot work out how to get it to display in that cell. Can anyone shed any light on how to get these CFIF statements to appear within this CFSET?
<CFIF '#get_info.csuseragent#' contains "blackberry">Blackberry</CFIF>
<CFIF '#get_info.csuseragent#' contains "iphone">iPhone</CFIF>
<CFIF '#get_info.csuseragent#' contains "ipad">iPad</CFIF>
<CFIF '#get_info.csuseragent#' contains "android">Android</CFIF>
<CFIF '#get_info.csuseragent#' contains "msie">IE</CFIF>
<CFIF '#get_info.csuseragent#' contains "firefox">Firefox</CFIF>
<CFIF '#get_info.csuseragent#' contains "chrome">Chrome</CFIF>
<CFIF '#get_info.csuseragent#' contains "opera">Opera</CFIF>
<CFIF '#get_info.csuseragent#' contains "Safari/534.53.10">Safari</CFIF>
<CFIF '#get_info.csuseragent#' contains "Safari/534.57.2">Safari</CFIF>
<CFIF '#get_info.csuseragent#' contains "Safari/533.21.1">Safari</CFIF>
<CFIF '#get_info.csuseragent#' contains "Safari/533.19.4">Safari</CFIF>
<CFIF '#get_info.csuseragent#' contains "Safari/533.18.5">Safari</CFIF>
<CFIF '#get_info.csuseragent#' contains "Safari/534.50">Safari</CFIF>
Once you upgrade to CF 9+ you can use ternary operators. I know you can't use this with CF8 so consider this a tip for the future.
ternary works best for simple if-else conditions and is very elegant in my opinion. The syntax is condition ? true : false
<cfset myVar = findNoCase('blackberry', agent) ? "Blackberry" : "unknown">
You can do nesting like this too but if it gets complicated to follow / maintain you're better off using <cfscript>if(){}elseif(){}else{};</cfscript> or <cfif><cfelseif><cfelse></cfif> for readability, not to mention I've never tested nested ternary efficiency. Personally I find this easy to read but to each his/her own...
<cfset myVar = findNoCase('blackberry', agent) ? "Blackberry" :
findNoCase('iPhone', agent) ? "iPhone" :
findNoCase('android', agent) ? "Android" :
findNoCase('msie', agent) ? "Kill me now" :
findNoCase('firefox', agent) ? "Firefox" :
findNoCase('chrome', agent) ? "Chrome" :
findNoCase('opera', agent) ? "Opera" :
findNoCase('safari', agent)? "Safari" : "unknown">
You can't embed tags within each other, so this is invalid syntax:
<cfset myVar = <cfif something>"fred"<cfelse>"barney"</cfif>>
What will work in your situation is to first set a variable:
<cfif get_info.csuseragent contains "blackberry">
<cfset agent = "Blackberry">
<cfelseif get_info.csuseragent contains "iphone">
<cfset agent = "iPhone">
etc
<cfelse>
<cfset agent = "Unknown">
</cfif>
and then use it:
<cfset via = '<td style="background-color: DBEFB6;">#src#
<br><font color="blue">#agent#</font></td>'>
Use cfsavecontent instead of cfset.
please see the following docs showing how to do it.
http://cfquickdocs.com/?getDoc=cfsavecontent

ColdFusion = OnRequest Error

Looking through the logs, we're getting hundreds of the following
"Error","jrpp-185","08/21/12","10:05:43","PATH","www.domain.com
Agent:Mozilla/4.0 (compatible; Synapse)
Error: An exception occurred when invoking a event handler method from Application.cfc.
The method name is: onRequest.
They seem to be mostly search bots. The on place on APplication.cfc that I can see reference to the function is below
<cffunction name="onRequest" returnType="void">
<cfargument name="targetPage" type="String" required=true/>
<cfsetting enablecfoutputonly="yes" requesttimeout="20">
<cfparam name="url.refresh" default="0">
<cfset request.strMember = Duplicate(session.strMember)/>
<cfset request.tNow = GetTickCount()>
<cfif url.refresh EQ 0>
<cfset request.iCacheHr = 12/>
<cfelse>
<cfset request.iCacheHr = 0/>
</cfif>
<cflogin>
<cfif IsDefined("session.strMember.sRoles")>
<cfloginuser name="#session.strMember.sFirstName##session.strMember.sLastName#"
password="12345"
roles="#session.strMember.sRoles#"/>
</cfif>
</cflogin>
<cfinclude template="core/incl/SessionLogger.cfm">
<cfinclude template="core/incl/LinkTranslator.cfm">
<cfinclude template="core/incl/udf.cfm">
<cfinclude template="urlcheck.cfm"/>
<cfinclude template="#Arguments.targetPage#">
</cffunction>
From that, can anyone please advise on what's wrong and how to fix it? I'm fairly new to CF and this is making me pull out what little hair I have left
1) You use two different coding styles
<cfparam name="url.refresh" default="0">
<cfset request.strMember = Duplicate(session.strMember)/>
Invalid/left open XML tags in first line and valid (closed) XML tags in the second line.
Try to stick to one (preferably the last one).
2) You are using old way of checking variable being defined
IsDefined("session.strMember.sRoles")
read about newer (and better and faster)
StructKeyExists(session.strMember, "sRoles")
3) Most likely your code is calling
<cfloginuser ... >
at every page request
4) Make sure that paths for all includes are correct and they themselves don't have any errors.
Simplify your method until you stop getting an error and then investigate what exactly is causing it
Are the bots hitting a page that doesn't exist?
Maybe try changing the last line to:
<cfif fileExists(expandPath(Arguments.targetPage))>
<cfinclude template="#Arguments.targetPage#">
<cfelse>
<cfabort>
</cfif>
Maybe you could detect if they are a bot and server them something else? depends on how search friendly you want your site to be:
http://www.bennadel.com/blog/1083-ColdFusion-Session-Management-And-Spiders-Bots.htm

Using a Custom Tag Variable from Outside of a Custom Tag

Ok, I thought this would be simple but I'm getting an error telling me the variable does not exist.
Here is my custom tag code:
<cfset isBot = false>
<cfif find("bot", CGI.HTTP_USER_AGENT)>
<cfset isBot = true>
</cfif>
Here is my page calling the custom tag:
<cf_checkBot>
<cfif isBot>
Yes This Is A Bot!
</cfif>
So how do I use a variable outside of a customtag that was set inside of a custom tag?
Thanks :)
You want the Caller scope:
<cfset isBot = false>
<cfif find("bot", CGI.HTTP_USER_AGENT)>
<cfset Caller.isBot = true>
</cfif>
<cfset isBot = false>
<cfif find("bot", CGI.HTTP_USER_AGENT)>
<cfset **caller.**isBot = true>
</cfif>
You use the caller scope.
It might be better to use a function instead of a custom tag though.