Entries Tagged as "Railo"
getParentTemplatePath() function for Railo
Posted by Paul Klinkenberg in Railo on February 1, 2011
Railo has the cool ability to use relative file paths in all it's main directory and file functions. For example <cfset fileRead("myFile.txt") />.
Now, I am writing a new custom tag CFCSV for Railo, and also wanted to include this ability: <cfcsv action="parse" file="myFile.csv" />. Therefor, my custom tag needed to know the caller's template path, and calculate the full file path from there. Finding the caller's template path took me longer then expected. I thought I could easily get it from the reference to the "caller" within my custom tag. I didn't find it in that object unfortunately. So in the end, I chose to just get the current stack trace for the request, and loop through there:
<cffunction name="getParentTemplatePath" returntype="string" output="no">
<cfset var stackTraceArr = getPageContext().getThread().getStackTrace() />
<cfset var i = -1 />
<cfset var thisFncFound = false />
<cfset var currentFilePath = "" />
<cfloop collection="#stackTraceArr#" item="i">
<cfif right(stackTraceArr[i].getClassName(), 3) == "$cf">
<cfif not thisFncFound>
<cfset thisFncFound = true />
<cfelseif len(currentFilePath) and stackTraceArr[i].getFileName() neq currentFilePath>
<cfreturn stackTraceArr[i].getFileName() />
<cfelse>
<cfset currentFilePath = stackTraceArr[i].getFileName() />
</cfif>
</cfif>
</cfloop>
<cfreturn "" />
</cffunction>
You can use this function inline within your current template, and also called as an external cfc function.
In case it's not working, let me know. It's not really thoroughly investigated, so it might explode at a moment you least expect it ;-)
For Adobe Coldfusion, you might want to check out this blog post by Elliott Sprehn: http://www.elliottsprehn.com/blog/2007/07/17/getting-the-expected-results-for-getcurrenttemplatepath-in-a-custom-tag/
Railo custom tag CFDNS
I was actually surprised it was not in Coldfusion yet: the ability to do dns lookups. Since I'm the official Railo Extension Manager®™, I saw it as an excellent chance to create a new custom tag: CFDNS.
But, as seems to happen a lot with good ideas, someone else already thought of it. Just check www.cfdns.org :-/
The thing I could still do, was create the code to use it as a custom tag in Railo. I used the existing cfdns code with a few changes, and made a custom tag wrapper for it, and here it is: <CFDNS> for Railo!
Railo tip: using conditional assignments!
Posted by Paul Klinkenberg in Railo on December 7, 2010
I saw this type of code popping up in some Railo source code, but for some reason did not use it before.
In javascript, I was already using it a long time: var a = (b>100) ? 100 : b, but in Railo? Not yet.
Well, that has changed now! It is much simpler then using IIF(), and has great power:
<cfset testValue = 107 />
<cfset nothigherThen100 = testValue gt 100 ? 100 : testValue />
And you can also use it inline:
<cfset testValue = 107 />
<cfoutput>#testValue# is #(testValue gt 100 ? 'greater then' : 'smaller or equal to')# hundred</cfoutput>
Off course, you can nest these statements:
<cfoutput>#testValue# is #(testValue gt 100 ? 'greater then' : (testValue eq 100 ? 'exactly' : 'smaller then'))# hundred</cfoutput>
If you are new to this concept, the syntax is really simple, once you get the hang of it: (also see Wikipedia)
[statement, evaluated to true or false] ? [value if statement is true] : [value if statement is false]
Pretty nifty eeh? [edit] And it works in Adobe Coldfusion 9 too. [/edit]
Railo tip: get a query's columnlist case-sensitive
Posted by Paul Klinkenberg in Railo on November 16, 2010
A member of the Railo mailing list asked if he could get the columnlist of a query object case-sensitive. Because #queryObject.columnlist# always returns it uppercase.
One good answer was to look at #getMetaData(queryObject)#, which returns an array with structs with keys isCaseSensitive, name, typeName.
So that's an option, to just loop over that array.
But I knew it must be easier, so I looked in the Railo source code, and found this simple solution:
<cfset caseSensitiveColumnList = queryObject.getColumnlist(false) />
<cfset upperCaseColumnList = queryObject.getColumnlist() />
Pretty cool eeh? Start using Railo today!
NOTE: see the comments underneath; if you typed the actual column names in the SELECT statement, like in "SELECT userID, userName from users", then the case you used there will be returned. But if you used "SELECT * from users", then the actual table column names are returned.
Railo tip: use a custom function for the cfdirectory "filter" attribute
Posted by Paul Klinkenberg in Railo on November 14, 2010
While searching for something else which had to do with cfdirectory, I saw a lot of questions about the filter attribute of cfdirectory. With this attribute, you can filter the results by extensions or part of the file/directory name. For example "*.gif".
The first question that a lot of people have, is whether they can use multiple file filters. The answer is: Yes. You just need to delimit the file filters by a pipe character, like this: "*.gif|*.jpg|*.png".
Another question was whether you could disallow some files or directories from the listing. Now that's much more complicated, or at least you need to write some extra lines of code to remove these from the original directory listing...
Except on Railo! Because with Railo, you can use a custom function as the filter argument, like this:
<cfdirectory action="list" directory="/test/" recurse="true" name="qFiles" filter="#theFilter#" />
<cfdump var="#qFiles#" />
<cffunction name="theFilter" returntype="boolean">
<cfargument name="fullpath" type="string" />
<!--- disallow a certain directory --->
<cfif refindNoCase("[/\\]DisallowedDirectory[/\\]", arguments.fullPath)>
<cfreturn false />
<!--- allow certain extensions --->
<cfelseif refindNoCase("\.(swf|jpe?g|gif|png)$", arguments.fullPath)>
<cfreturn true />
<cfelse>
<cfreturn false />
</cfif>
</cffunction>
As you can see, the function returns a boolean value "true" or "false". True means that the file or directory will be included into the listing, and when False, it will not.
Pretty cool eeh! Start using Railo today ;-)
By the way, when you test this code on ACF 8 or 9, it does not throw an error, but instead just returns zero records.
Recent Comments