2016-06-07 112 views
4

如何跟蹤ColdFusion中的模板路徑?在ColdFusion中調用的跟蹤模板

I.E. 我有以下文件夾和文件結構

  • index.cfm

    <cfset ArrayAppend(request.Trace, '/')> <cfdump var=#request.trace#>

    • index.cfm

      <cfset ArrayAppend(request.Trace, '/foo/')> <cfinclude template='../'>

    • 酒吧

      • index.cfm

        ArrayAppend(request.Trace,'/foo/bar/')> <cfinclude template='../'>

當我CA LL foo/bar/index.cfm, request.Trace等於:

  • '/富/酒吧/'
  • '/富/'
  • '/'

我怎麼能這樣做而無需特別聲明每個文件夾名稱?

回答

8

看一看:

  • expandPath(".")
  • getBaseTemplatePath()
  • getCurrentTemplatePath()
  • CGI.CF_TEMPLATE_PATH
  • CGI.PATH_TRANSLATED
  • CGI.SCRIPT_NAME

如果你想模板棧跟蹤,使用此:

<cfset templateTrace = []> 
<cfset tagTrace = createObject("java","java.lang.Exception").init().TagContext> 
<cfloop array="#tagTrace#" index="tagInfo"> 
    <cfset templateTrace.add(tagInfo.Template)> 
</cfloop> 
<cfdump var="#templateTrace#"> 

這將輸出向上傳遞到該調用的所有模板。

+0

謝謝!這是我正在尋找的。我更新了問題以更好地適應解決方案 –

+0

這太棒了!謝謝@Alex,它對調試非常有幫助。 – Anurag

2

不理想,但這對我有效。

<cfset currentFile = GetCurrentTemplatePath()> 
<cfset currentDir = GetDirectoryFromPath(currentFile)> 
<cfset webroot = expandPath("/")> 
<cfset m_Trace = Replace(currentDir, webroot , '\')> 
<cfset ArrayAppend (request.Trace, m_Trace)> 
+0

我編輯我的帖子,包括一種方法來檢索模板堆棧跟蹤,而不需要在每個模板中放置代碼。 – Alex