0

I have some components that I've broken into multiple smaller files. The main file includes them using data-sly-include attributes.

I'd like to generate HTML comments containing the current filename in both the main file and the included files, so that when I view the rendered output I can easily tell which file generated the output. Something like:

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

I can see how to get the path to the current page, but that's the resource that contains my component; I want the paths to the files that make up the components. Any way to do this in Sightly?

Val
  • 2,291
  • 7
  • 34
  • 63

1 Answers1

0

the sightly script engine has the current file as a binding variable with sling, so the following should work-

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

you will have to put this in a js file and call it from all your sightly scripts.

awd
  • 2,302
  • 1
  • 22
  • 29
  • 1
    Thanks! That's a rather bare-bones answer, but it gave me enough to go on. I put that code into a .js file along with my component: `use( function() { return { "path" : sling.getScript().getScriptResource().getPath() }; });` Then in my component markup, I added `` and I now see the path to the fragment in a comment in the rendered HTML. Warning: the markup needs access to the JS script -- if it's not in the same directory, you need to locate it as appropriate with `../`, `/etc/...` etc. – Val Aug 14 '17 at 18:06