【问题标题】:How to emit the server path to the current file using Sightly?如何使用 Sightly 发出当前文件的服务器路径?
【发布时间】:2017-08-09 22:18:05
【问题描述】:

我有一些组件已分解为多个较小的文件。主文件使用data-sly-include 属性包含它们。

我想在主文件和包含文件中生成包含当前文件名的 HTML cmets,这样当我查看呈现的输出时,我可以很容易地分辨出哪个文件生成了输出。比如:

... 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 -->

我可以看到如何获取当前页面的路径,但那是包含我的组件的资源;我想要组成组件的文件的路径。有什么方法可以在 Sightly 中做到这一点?

【问题讨论】:

    标签: aem sightly htl


    【解决方案1】:

    sightly 脚本引擎将当前文件作为绑定变量与 sling,所以以下应该可以工作-

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

    你必须把它放在一个 js 文件中,然后从你所有漂亮的脚本中调用它。

    【讨论】:

    • 谢谢!这是一个相当简单的答案,但它给了我足够的继续。我将该代码与我的组件一起放入一个 .js 文件中:use( function() { return { "path" : sling.getScript().getScriptResource().getPath() }; }); 然后在我的组件标记中,我添加了&lt;sly data-sly-use.currentFile="../component-path.js"&gt;&lt;!-- begin: ${currentFile.path} --&gt;&lt;/sly&gt;,现在我在呈现的 HTML 的注释中看到了片段的路径。警告:标记需要访问 JS 脚本——如果它不在同一个目录中,您需要使用..//etc/... 等适当地找到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 2011-06-26
    • 2020-09-10
    • 2013-01-27
    相关资源
    最近更新 更多