返回介绍

9 Local Variables

发布于 2023-06-28 19:47:13 字数 3936 浏览 0 评论 0 收藏 0

Thymeleaf calls local variables the variables that are defined for a specific fragment of a template, and are only available for evaluation inside that fragment.

An example we have already seen is the prod iter variable in our product list page:

<tr th:each="prod : ${prods}">
    ...
</tr>

That prod variable will be available only within the bounds of the <tr> tag. Specifically:

  • It will be available for any other th:* attributes executing in that tag with less precedence than th:each (which means they will execute after th:each).
  • It will be available for any child element of the <tr> tag, such as any <td> elements.

Thymeleaf offers you a way to declare local variables without iteration, using the th:with attribute, and its syntax is like that of attribute value assignments:

<div th:with="firstPer=${persons[0]}">
  <p>
    The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.
  </p>
</div>

When th:with is processed, that firstPer variable is created as a local variable and added to the variables map coming from the context, so that it is available for evaluation along with any other variables declared in the context, but only within the bounds of the containing <div> tag.

You can define several variables at the same time using the usual multiple assignment syntax:

<div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
  <p>
    The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>.
  </p>
  <p>
    But the name of the second person is 
    <span th:text="${secondPer.name}">Marcus Antonius</span>.
  </p>
</div>

The th:with attribute allows reusing variables defined in the same attribute:

<div th:with="company=${user.company + ' Co.'},account=${accounts[company]}">...</div>

Let’s use this in our Grocery’s home page! Remember the code we wrote for outputting a formatted date?

<p>
  Today is: 
  <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 february 2011</span>
</p>

Well, what if we wanted that "dd MMMM yyyy" to actually depend on the locale? For example, we might want to add the following message to our home_en.properties:

date.format=MMMM dd'','' yyyy

…and an equivalent one to our home_es.properties:

date.format=dd ''de'' MMMM'','' yyyy

Now, let’s use th:with to get the localized date format into a variable, and then use it in our th:text expression:

<p th:with="df=#{date.format}">
  Today is: <span th:text="${#calendars.format(today,df)}">13 February 2011</span>
</p>

That was clean and easy. In fact, given the fact that th:with has a higher precedence than th:text, we could have solved this all in the span tag:

<p>
  Today is: 
  <span th:with="df=#{date.format}" 
        th:text="${#calendars.format(today,df)}">13 February 2011</span>
</p>

You might be thinking: Precedence? We haven’t talked about that yet! Well, don’t worry because that is exactly what the next chapter is about.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文