Steve Bryant
02/27/2023, 6:29 PMScott Bennett
02/27/2023, 7:05 PMAdam Cameron
callStackGet has some overhead to it, but:
.../callingContextPath# tree .
.
|-- lib
| `-- MyTag.cfm
|-- public
| `-- test.cfm
`-- views
`-- myView.cfm
<!--- test.cfm --->
<cfinclude template="../views/myView.cfm">
<!--- myView.cfm --->
<cfimport taglib="../lib" prefix="x">
<x:Mytag>
<!--- MyTag.cfm --->
<cfset callStack = callStackGet()>
<cfset callingFile = callStack[2].template>
<cfset callingDir = getDirectoryFromPath(callingFile)>
<cfset includeRoot = expandPath("/")>
<cfset includePath = callingDir.replace(includeRoot, "/")>
<cfdump var="#[callingFile,includeRoot,includePath]#">
Dump:
array
1 /app/cfml/cfmlLanguage/customtags/callingContextPath/views/myview.cfm
2 /app/
3 /cfml/cfmlLanguage/customtags/callingContextPath/views/
You'll like have to be less naive than this if there are mappings involved, etc.
---
TBH, I would perhaps also question why you want to do this, and think about it rather than settling on "this is what I want to do".Scott Bennett
02/27/2023, 7:40 PMSteve Bryant
02/27/2023, 7:58 PMcallStackGet looks like the answer. A single call to it seems to take 0-1 ms, so I don't think that will be a problem.
Thanks!Steve Bryant
02/27/2023, 8:24 PM/**
* I get the contents of a file included from the calling page.
*/
private string function getIncludedFileContents(required string include) {
var ThisPath = GetCurrentTemplatePath();
var aCallStack = callStackGet();//Get the calling stack (Thanks Adam Cameron!)
var callingFile = aCallStack.filter(function(item){
return item.Template NEQ ThisPath;
})[1]["Template"];//Get first matching template from the stack that isn't the current file.
var callingDir = getDirectoryFromPath(callingFile);
var includeRoot = expandPath("/");//Get root path
var includePath = callingDir.replace(includeRoot, "/") & Attributes.include;//Get root-relative path
var result = savecontent {
include includePath;
}//Get the contents of the file
return result;
}Adam Cameron
filter there. It's always gonna be the second one, innit? And there will always be at least two in there (custom tag file, and the file that calls it), so yer safe to just use the second one.
Also bear in mind that filter always traverses the entire collection, so it's not gonna get the "first" one, it's gonna get all of them except the first one. Not that the callstack will ever be very big, but unless there's something I'm missing, it's just not the right tool for the job in this case.Evil Ware
02/27/2023, 10:23 PMSteve Bryant
02/27/2023, 11:24 PMAdam Cameron
Steve Bryant
02/28/2023, 2:17 PMAdam Cameron
Steve Bryant
02/28/2023, 8:02 PM