ultraq / thymeleafjs

A basic implementation of the Thymeleaf templating engine in JavaScript
Apache License 2.0
52 stars 8 forks source link

Add template fragment support #14

Closed yubaoquan closed 6 years ago

yubaoquan commented 6 years ago

Template fragment is an import feature, it allow user to define a partial template and use it in some other places. For example, I have some stylesheet tags written in many template files, I can extract them into a template fragment, like this:

common.html

<head th:fragment="head">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="/static/css/userinfo/index.css"></link>
    <link rel="stylesheet" href="//unpkg.com/element-ui/lib/theme-default/index.css"></link>
</head>

and just write this in file which I want to add stylesheets.

<head th:include="common :: head">

See doc here

yubaoquan commented 6 years ago

An advice: there is no need to write a parser for the template, you can just write a java file which compile the template by thymeleaf module and return the result string. And then, include the jar files into node project, node just pass the template path and data to the java module, and receive the compiled result.

There is a freemarker node parser which uses the above approach.

I found thymeleaf offers the similar API with freemarker, it may not be hard to implement.

ultraq commented 6 years ago

Support for fragments has been added in version 0.9.0, although the work required by devs to get it working on the client side is less than ideal (a special directory, random webpack configuration). Closing for now as I've raised #16 covering that part of getting fragments to work.