getParentTemplatePath() function for Railo

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/

del.icio.us Digg StumbleUpon Facebook Technorati Fav reddit Google Bookmarks
| Viewed 3314 times

No comments yet.

(will not be published)
Leave this field empty

pentavalent-vertebrate