HtmlFlow icon indicating copy to clipboard operation
HtmlFlow copied to clipboard

Story for generating inline css or inline js

Open benzen opened this issue 5 years ago • 4 comments

Does this project plan to produce inline js or inline css.

Using thymleaf it happens from time to time that I need to produce in my inline js base on some variable extracted from the model.

benzen avatar Oct 03 '20 03:10 benzen

Can you give an example use case please, for that usage.

fmcarvalho avatar Oct 03 '20 08:10 fmcarvalho

I'll use the exemple provided in the thymleaf documentation

<script th:inline="javascript">
    ...
    var username = [[${session.user.name}]];
    ...
</script>

Here the value from session.user.name is used to generate the content of the script.

There is ways to workarround this as a missing feature, like adding data to the dom, that the JS code would pick up on initialisation.

But the way proposed by thymleaf feels quite natural to me. If we decide to not provide support for this feature in HtmlFlow, I think documentation should guide a developper toward alternatives.

benzen avatar Oct 03 '20 17:10 benzen

Maybe I am not entirely understanding that use case. But I am not figuring out the need of "adding data to the dom, that the JS code would pick up on initialisation".

If I am thinking well when you build a Javascript block in HtmlFlow such as: ....script().text(inlinejs), where the inlinejs local variable should be initialized before with the Js code such as String inlinejs = String.format("... var username = %s", session.user.name). Isn't it?

fmcarvalho avatar Oct 08 '20 08:10 fmcarvalho

This is perfect, especially with brand new textblock feature and the instance method formatted on string.

As I suspected it's just a matter of documentation. Thank you very much.

I propose we keep this issue open, I'll make a PR for enhancing documentation on this point

benzen avatar Oct 08 '20 12:10 benzen