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

Passing variables to decorator #32

Closed
igo opened this issue Jan 4, 2014 · 8 comments
Closed

Passing variables to decorator #32

igo opened this issue Jan 4, 2014 · 8 comments
Assignees
Labels

Comments

@igo
Copy link

igo commented Jan 4, 2014

Please allow passing variables to parent decorator with th:with like this:

<body layout:decorator="templates/main-page" th:with="hello='world'">
    <div layout:fragment="content>...</div>
</body>
@ultraq
Copy link
Owner

ultraq commented Jan 6, 2014

That should already be possible. I haven't documented it, but I use th:with to pass variables to decorator pages in my own projects. Did you come across a case that didn't work for you?

@igo
Copy link
Author

igo commented Jan 6, 2014

It doesn't work for me. Try the view above with this decorator templates/main-page:

<body>
    <h1 th:text="${hello}">Hello</h1>
    <div layout:fragment="content"></div>
</body>

Result is

<body>
    <h1></h1>
    <div>...</div>
</body>

Thymeleaf version 2.1.0, layout 1.2

@ghost ghost assigned ultraq Jan 9, 2014
@ultraq
Copy link
Owner

ultraq commented Jan 9, 2014

Cool, I'm able to make a test case that duplicates this behaviour. It looks like without the <html> root element in the decorator template, the dialect defaults to a basic XML decorator, which doesn't pull attributes from the content document to the decorator document. Why it isn't doing this is probably an oversight on my part.

@ultraq
Copy link
Owner

ultraq commented Jan 9, 2014

Fixed for the next version of the layout dialect. Test file: FragmentVariablePassing.thtest

@ultraq ultraq closed this as completed Jan 9, 2014
@ultraq
Copy link
Owner

ultraq commented Jan 9, 2014

If you (or anybody watching this issue) need to use this fix right now, I've just uploaded a 1.2.2-SNAPSHOT version with it.

@kalgon
Copy link

kalgon commented Feb 22, 2015

Is it possible to declare the variables in the decorator and giving them default values?

layout

<html layout:attributes="hello = 'Hello World!', showMenu = true">

page

<html layout:decorator="layout (showMenu = false)">

or

<html layout:decorator="layout" th:with="showMenu = false">

@ultraq
Copy link
Owner

ultraq commented Feb 23, 2015

Been a while since I tried to see how merged attribute behaviour would work, but you should be able to declare values in the decorator with plain old th:with, then set them again in your pages. The one with the nearest scope should win out.

@kalgon
Copy link

kalgon commented Feb 23, 2015

I tried that and the value declared in the decorator always wins. If I don't declare it, the one in the decorated page is used. So it's not possible to have default values in the decorator. Another problem I have with not being able to declare them in the decorator is that you have to search for them in the HTML code before you can use them. If they were declared at the top of the decorator, it would be easier to know what variables can be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants