Comments (10)
A more general implementation of this could be to be able to define "insertions" to be injected into inserted fragments, for example by declaring placeholders for these insertions in the fragment, or even by simple appending/prepending contents.
Queued for Thymeleaf 2.1
from thymeleaf.
Great. Anyway thank you very much for Thymeleaf. It's really good template engine.
from thymeleaf.
I use a fragment such as
<head>
<title th:text="${applicationName + ' : ' + title}">
...
</head>
in my layout.html page, that I then include by way of
<head th:substituteby="layout :: [//head]">head</head>
but there is no way (that I can find) to specify the value of ${title}
prior to including the fragment other than adding it to the model. I find it far more useful to specify the title of a page in the page's template, rather than as part of the model code, for code maintainability reasons. I tried using th:with
but this didn't seem to work. I vote for this issue too!
from thymeleaf.
Variable substitution in a fragment works if you define the variable at a higher level. So, for your title example, you can do:
<html xmlns=... th:with="title='hello'">
<head th:substituteby="layout::[//head]" />
I'm trying to include the contents of the head from another template and then add some additional scripts and links. I was trying something like:
<head>
<link th:substituteby="layout::[//head]" th:remove="tag" />
<script src="@{/js/another.js}" />
</head>
But then I find out the th:remove
is lower precedence than th:substituteby
, so the former is ignored and I end up with a <head>
within my <head>
.
I also tried "layout::[//head/*]"
, but it seems the wildcard is not supported.
There is a way that works, but it's a little more cumbersome. In the template you're pulling from, tag the fragment multiple times:
<head>
<link th:fragment="headcontent" ... />
<script th:fragment="headcontent" .../>
</head>
Then, in the main template, just substitute the fragment once:
<head>
<link th:substituteby="layout::headcontent" />
<script src="@{/js/another.js}" />
</head>
from thymeleaf.
Thanks @usethe4ce, that worked perfectly. I was using th:with
in the <head>
element. Moving it to the <html>
element makes it work as advertised. Thanks heaps for the hint.
from thymeleaf.
Still it would be really nice to have something more straight forward like the "insertions" described above, appending or prepending content to a tag element having a "th:include" or a "th:substituteby".
In my particular case I have a "head" tag within which I have linked all the external resources (JS, CSS, etc...) but if for a specific view I want to add a script, for instance to modify the DOM via jquery, after the view is rendered ("onready"), this script is not included in the rendering.
Thanks for the work and the time invested
from thymeleaf.
I've written a dialect that currently performs these "insertions": create a parent 'layout' page, define points in the layout which will be overridden/filled-in by child/content pages, then reference the layout page in the child/content one (the one being processed by Thymeleaf). It also does a bunch of automatic insertions for the <head>
element without having to write any additional processors since it's such a common use case.
I don't know how far off Thymeleaf 2.1 would be, but for those looking for a solution now, would my dialect suffice? https://github.com/ultraq/thymeleaf-layout-dialect
from thymeleaf.
@ultraq Your dialect is very useful, thanks for sharing!
from thymeleaf.
Closing this issue for 2.1, as this is already offered by the LayoutDialect and 3.0 will include #180, which should solve this scenario for those not wanting to use this dialect for some reason.
from thymeleaf.
See #451 (Thymeleaf 3.0)
from thymeleaf.
Related Issues (20)
- Restricted types no longer usable with th:with in 3.1.2 HOT 2
- Unable to select direct child (using /) HOT 1
- Thymeleaf generated text in TEXT mode replaces CRLF with LF?
- Outputting a JS array outside th:inline script tag HOT 4
- Block to add attributes to included fragments HOT 1
- Do you support CSS3 styles? Like flexbox layout HOT 1
- How to prevent SSTI injection when using hypoleaf in inheriting DispatcherServlet?
- [BUG] using th:field does not work in th:each HOT 1
- GraalVM potential missing reachability metadata
- th:replace by POST response view HOT 1
- BUG: problem with passing Number and String data type to javascript function in thymeleaf looping th:each HOT 3
- Error "Cannot build an application for a request which servlet context does not match with the application that it is being built for." HOT 4
- Cannot use java.util.Currency in expressions
- Migration issue Thymeleaf 3.0.10RELEASE to 3.1.2RELEASE
- when using java 9 modules it doesn't find the template resolver
- Any solution for BDSA-2023-1804? HOT 3
- HTMX integration section in Thymeleaf documentation
- Request for posting of Maven signing key(s) HOT 1
- Replace Variables with issue as empty String HOT 1
- IPostProcessorDialect implementation to minify/remove comments from output html HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from thymeleaf.