Variables in fragments

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Variables in fragments

Alexandre Roger
Hello,
I've made some progress on my previous question but while working on it I've come across a problem with fragments. I think I might just be misunderstanding what is accessible from within a fragment's context.

I can use literals or variables when calling the fragment, but I can't find a way to pass a variable from a "th:each" loop. What am I missing?

In the following, the two first examples will go through but the third one will give an error.

Code :
<a href="http://"></a>
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-3.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8" /> 
		<script type="text/javascript" src="bower_components/jquery/dist/jquery.min.js"></script>
		<script type="text/javascript" src="bower_components/thymol/dist/thymol.js"></script>
		
		<script type="text/javascript">
		$(document).ready(function() {
			thDebug = true;
			thVars = [["var", [{name: 'Alex', role: 'User'}, {name: 'Jim', role: 'Dev'}]]];
		})
		</script>
		
	</head>
	<body>
		<!-- fragments -->
		<th:block th:fragment="test (value)">
			<span th:text="${value}"></span>
		</th:block>

		Literal : <span th:replace=":: test(value='Literal')">literal</span><br/>
		Variable : <span th:replace=":: test(value=${var[0].name})">variable</span><br/>
		<th:block th:each="person : ${var}">
		Object : <span th:replace=":: test(value=${person.name})">variable</span><br/>
		</th:block>
	</body>
</html>

Error:
column: 76
element: span
line: 3261
message: "while evaluating expression: person: null, name: name"
name: "ThError"
Reply | Threaded
Open this post in threaded view
|

Re: Variables in fragments

jjbenson
Administrator
Hi Alex,

  I'm afraid to say that what you've identified is a serious defect. What's worse is that there is no work-around for it, a code change will be required.

  I've opened a github issue to track this problem (here). I will give the fix top priority.

  Kind Regards,

    Jim

 
Reply | Threaded
Open this post in threaded view
|

Re: Variables in fragments

jjbenson
Administrator
In reply to this post by Alexandre Roger
Hi Alex,

  I've just delivered a fix for this to github.

  You can find the new versions of the libraries at https://github.com/thymol/thymol.js/tree/2.x-master/dist

  I'm afraid the changes in these new files have probably broken the fragment processing in your dynamic template processing code (e.g. the thymol.processChildren function now takes the fragment root element as an argument). But hopefully, you'll be able to work these out easily enough.

  Please let me know how the new build works for you.

  Kind Regards,

    Jim