template fragments from http(s)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

template fragments from http(s)

dwallace
Thymeleaf supports using template fragments from http(s) uri's. E.g.
<div data-th-include="https://somecmsserver.example.com/fragments/header.html::header"></div>

When Thymol tries to process this statically via a browser accessing the html file directly, it ends up trying to load something like:
file:///path/to/root/path/to/templates/https://somecmsserver.example.com/fragments/header.html::header

If CORS were enabled on the sites hosting the external template fragments, would this be possible to support in Thymol?

For now, is there a best practice to emulating this in a static environment? Maybe downloading a temporary copy of the external resource to e.g.
file:///path/to/root/path/to/templates/https.somecmsserver.example.com/fragments/header.html
and configuring Thymol to do the URI transformation?
Reply | Threaded
Open this post in threaded view
|

Re: template fragments from http(s)

dwallace
I tried a simple example using a mapping like this:
var thMappings = [
    ["https://raw.githubusercontent.com/thymol/thymol.js/master/src/doc/WebContent/examples/thymol-example/basic/footer.html", "fragments/footer"]
];

I saved a copy of that url to fragments/footer.html in my templates directory. If I use the saved fragment normally, it works fine. e.g.
        <div data-th-include="fragments/footer :: copy"></div>

If I use the URL and rely on the mapping, like this:
        <div data-th-include="https://raw.githubusercontent.com/thymol/thymol.js/master/src/doc/WebContent/examples/thymol-example/basic/footer.html :: copy"></div>
It doesn't work and the debug message indicates it's looking for "file://fragments/footer fragment: copy". So, it's like it is doing the mapping properly but then for some reason tacking on the default protocol.
Reply | Threaded
Open this post in threaded view
|

Re: template fragments from http(s)

jjbenson
Administrator
In reply to this post by dwallace
Yes, I'm pretty sure that enabling CORS will solve this (although I haven't tested it), and for the moment at least, you will also need to override the values of (thymol.) protocol, root and path.
Reply | Threaded
Open this post in threaded view
|

Re: template fragments from http(s)

jjbenson
Administrator
In reply to this post by dwallace
Yes, the mapped value is being prefixed by the protocol value. Up to now this "feature" has been retained for the purpose of legacy support, but I think it's now safe for this to be dropped. Mappings were always intended to be static, hard-coded work-arounds, so if anyone needs a protocol prefix, they can add it to their mapping definition.

I will make the change in the source base ready for the forthcoming beta release, but in the meantime, if you would like to hack the code, you should remove the thymol.protocol reference from the line shown here:

  https://github.com/thymol/thymol.js/blob/2.x-master/dist/thymol-full.js#L1857