Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Head include/substituteby improvement #3

Closed
kreuzman opened this issue May 31, 2012 · 10 comments
Closed

Head include/substituteby improvement #3

kreuzman opened this issue May 31, 2012 · 10 comments

Comments

@kreuzman
Copy link
Contributor

Hi it would be nice to have better way how to include/substituteby meta, link, script etc. in head element then one by one.

@danielfernandez
Copy link
Member

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

@ghost ghost assigned danielfernandez Jun 11, 2012
@kreuzman
Copy link
Contributor Author

Great. Anyway thank you very much for Thymeleaf. It's really good template engine.

@ben3000
Copy link

ben3000 commented Jul 12, 2012

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!

@usethe4ce
Copy link

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>

@ben3000
Copy link

ben3000 commented Sep 25, 2012

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.

@frandevel
Copy link

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

@ultraq
Copy link
Member

ultraq commented Dec 9, 2012

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

@jirutka
Copy link

jirutka commented Mar 16, 2013

@ultraq Your dialect is very useful, thanks for sharing!

@danielfernandez
Copy link
Member

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.

@danielfernandez
Copy link
Member

See #451 (Thymeleaf 3.0)

@danielfernandez danielfernandez removed this from the Thymeleaf 2.1 milestone Apr 25, 2021
danielfernandez added a commit that referenced this issue Mar 30, 2022
danielfernandez pushed a commit that referenced this issue Apr 13, 2022
Switch configuration to java base fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants