Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do I get the directory for the current template?

Use the two ColdFusion functions getCurrentTemplatePath() and getDirectoryFromPath().

<cfset currentPath = getCurrentTemplatePath()>
<cfset currentDirectory = getDirectoryFromPath(currentPath)>
<cfoutput>This directory is #currentDirectory#</cfoutput>

The function, getCurrentTemplatePath(), will return the full path of the current template. By path we mean the directory and file name. The function, getDirectoryFromPath(), will then get just the directory from the path.


This question was written by Stan Winchester.
It was last updated on January 26, 2006 at 6:57:12 AM EST.

CFML Referenced

<cfoutput>
GetDirectoryFromPath()
GetCurrentTemplatePath()

Categories

File and Directory Access

Comments

Comment made by Rick O on February 17, 2006 at 5:30 PM
You may want to explain how this differs from CGI.CF_TEMPLATE_PATH and the difference between the template that was requested (index.cfm) and the module you may currently be running in (Application.cfm).


Comment made by JC on February 3, 2009 at 11:12 AM
It should probably be noted that this functionality changed in CF 8.

Prior to CF8, CGI.CF_TEMPLATE_PATH and also getCurrentTemplatePath() would refer to the base template you were requesting; post CF8, they refer to the actual template the string of code is located in.

If you need the path for the file you made the request to, use getBaseTemplatePath().