2017-08-09 81 views
0

我有一些組件已被分解爲多個較小的文件。主文件包括它們使用data-sly-include屬性。如何使用Sightly將服務器路徑發送到當前文件?

我想在主文件和包含文件中生成包含當前文件名的HTML註釋,這樣當我查看呈現的輸出時,我可以輕鬆地分辨出哪個文件生成了輸出。例如:

... some HTML from the template or other components... 
<!-- begin /path/to/main/file.html --> 
<div> 
    This is from the main file 
    <!-- begin /path/to/main/includes/first.html --> 
    <p>This is fromt the first include</p> 
    <!-- end /path/to/main/includes/first.html --> 
    <!-- begin /path/to/main/includes/second.html --> 
    <p>This is fromt the second include</p> 
    <!-- end /path/to/main/includes/second.html --> 
    now we're back in the main file 
<!-- end /path/to/main/file.html --> 

我可以看到如何獲取當前頁面的路徑,但這是包含我的組件的資源;我想要組成這些組件的文件的路徑。任何方式在視覺上做到這一點?

回答

0

的悅目腳本引擎具有當前文件與吊帶綁定變量,所以下面應該與工作

sling.getScript().getScriptResource().getPath() 

你將不得不把這個JS文件,並從所有悅目的腳本調用它。

+0

謝謝!這是一個相當簡單的答案,但它足以讓我繼續下去。我將該代碼與我的組件一起放入.js文件中:'use(function(){「path」:sling.getScript()。getScriptResource()。getPath()}; });' 然後在我的組件標記中,我添加了 '<! - begin:$ {currentFile.path} - >'和我現在在呈現的HTML中看到註釋片段的路徑。警告:標記需要訪問JS腳本 - 如果它不在同一個目錄中,則需要使用'../','/ etc/...'等來適當定位它。 – Val

相關問題