Code Monkey home page Code Monkey logo

Comments (10)

danielfernandez avatar danielfernandez commented on May 20, 2024

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.

kreuzman avatar kreuzman commented on May 20, 2024

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

from thymeleaf.

ben3000 avatar ben3000 commented on May 20, 2024

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.

usethe4ce avatar usethe4ce commented on May 20, 2024

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.

ben3000 avatar ben3000 commented on May 20, 2024

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.

frandevel avatar frandevel commented on May 20, 2024

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.

ultraq avatar ultraq commented on May 20, 2024

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.

jirutka avatar jirutka commented on May 20, 2024

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

from thymeleaf.

danielfernandez avatar danielfernandez commented on May 20, 2024

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.

danielfernandez avatar danielfernandez commented on May 20, 2024

See #451 (Thymeleaf 3.0)

from thymeleaf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.