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

[MAJOR FEAT] Refactoring of the inlining mechanism #396

Closed
danielfernandez opened this Issue Sep 19, 2015 · 1 comment

Comments

Projects
None yet
1 participant
@danielfernandez
Member

danielfernandez commented Sep 19, 2015

In Thymeleaf versions earlier than 3.0, an inlining mechanism was available that allowed the insertion of javascript (th:inline="javascript") or dart code (th:inline="dart") inside HTML templates, and also for allowing the use of inlined output expressions like [[${...}]] in the bodies of HTML tags (th:inline="text").

Thymeleaf 3.0 merges this inline support with its new set of template modes (see #391) so that th:inline now specifies the template mode that should be applied to the body of the host HTML tag (or the bodies of any children tags if they exist). This means that Thymeleaf 3.0 matches inline modes with template modes.

The allowed values for th:inline vary depending on the host template mode (i.e. the template mode of the base template being processed):

  • Templates in HTML template mode allow:
    • th:inline="html", active by default, allowing inlined output expressions (see #394) in tag bodies.
    • th:inline="text" for processing tag bodies using the TEXT template mode.
    • th:inline="javascript" for processing tag bodies using the JAVASCRIPT template mode.
    • th:inline="css" for processing tag bodies using the CSS template mode.
    • th:inline="none" for disabling the inlining mechanism in the contained markup.
  • Templates in XML template mode allow:
    • th:inline="xml", active by default, allowing inlined output expressions (see #394) in tag bodies.
    • th:inline="text" for processing tag bodies using the TEXT template mode.
    • th:inline="none" for disabling the inlining mechanism in the contained markup.
  • Template modes TEXT, JAVASCRIPT and CSS do not support the th:inline attribute.

Note also that Dart inlining has been deprecated, and th:inline="dart" attributes will be automatically converted by the engine to th:inline="javascript", and the JAVASCRIPT template mode will be applied to them.

So this:

<!DOCTYPE html>
<html>
  <head>
    <script th:inline="javascript">
      var whoami = /*[['a working application']]*/ 'a prototype';
      alert('Hi, I\'m a ' + whoami + '!');
    </script>
  </head>
  <body>
    <div>
      <p>Some content</p>
    </div>
  </body>
</html>

...will actually be processed by the Thymeleaf engine as if it were two different templates, the base one in HTML template mode:

<!DOCTYPE html>
<html>
  <head>
    <script></script>
  </head>
  <body>
    <div>
      <p>Some content</p>
    </div>
  </body>
</html>

...and a second one, which execution result will be inserted as the body of that <script></script> above, in JAVASCRIPT template mode:

      var whoami = /*[['a working application']]*/ 'a prototype';
      alert('Hi, I\'m a ' + whoami + '!');

Migration notice

The changes in the inlining mechanism provoke that almost all th:inline="text" attributes in existing Thymeleaf 2.1 templates can be removed when migrating to Thymeleaf 3.0. This is because, as explained above, Thymeleaf 3.0 includes html-inlining and xml-inlining, enabled by default in HTML and XML template modes.

Note that the removal of these th:inline="text" attributes is not only allowed, but in fact recommended, especially if it is applied in elements very high in the document tree –such as <html> or <body> in HTML templates–, as this will avoid the need to process every tag body in a different template mode (TEXT), thus highly reducing the complexity associated with processing those templates.

@danielfernandez

This comment has been minimized.

Show comment
Hide comment
@danielfernandez

danielfernandez Sep 19, 2015

Member

Already in 3.0.0-SNAPSHOT

Member

danielfernandez commented Sep 19, 2015

Already in 3.0.0-SNAPSHOT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment