I am just starting to use thymeleaf for templating, and I am attempting to create a custom Processor for links
I want to be able to use a tag such as the below example to create custom links in my template:
<link rel="stylesheet" type="text/css" custom:href="/styles/main.css" />
The idea being that I have custom URLs on the site, so my custom processor will generate the correct URL for the relative link provided.
The above works fine - however, now I want to be able to include variables insite the value being passed to the processor - so have followed the example in the thymeleaf docs, and in the Processor, where I generate the replacement URL I have added the StandardExpression stuff:
@Override protected String getTargetAttributeValue( Arguments arguments, Element element, String attributeName ){
need this to see if it can be factored out
final Configuration configuration = arguments.getConfiguration()
final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration)
final String attributeValue = element.getAttributeValue(attributeName)
final IStandardExpression expression = parser.parseExpression(configuration, arguments, attributeValue)
final String relativeUrl = (String) expression.execute(configuration, arguments)
urlService.generateUrl( relativeUrl )
}
I was hoping that with this addition I would be able to also add links as follows:
<link rel="stylesheet" type="text/css" custom:href="/styles/${user.name}/main.css" />
(example not real, I'm not actually serving user based css :) - but you get the idea)
But that then breaks my first simple example, as the literal URL string is not an expression, to get around this I seem to have to also add single quotes to my URL so the expression parser knows its a string not a variable.
<link rel="stylesheet" type="text/css" custom:href="'/styles/main.css'" />
This seems really ugly, and probably error prone to have to repeat the single quotes in the simple cases (which will be the majority of the cases) - Is there a nicer way to do this? I'm hoping that as the expressions stuff is all curly braces based, there should be a way for the parser to recognise that anything outside of an of the valid expressions can be treated as strings?
(honestly, it has been a bit of a struggle to get my head around what is going on - the javadocs seem pretty sparse and there don't seem to be many examples - been a lot of trying to read the original source code to work out which classes I should use etc)