How to print thymelaf variable value from generated text

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

How to print thymelaf variable value from generated text

anil1712
Dear All,

I have my HTML like this.
    <span th:inline="text">[[${description}]]</span>

the `description` variable has HTML text with some other thymeleaf variables so the output will be some thing like this.
   <span>
       <p>Static Text</p>
      <p>[[${foo}]]<p>
      <p>[[${bar}]]</p> 
   </span> 

I want to render the value of `foo` and `bar` while HTML generating process, but its printing as it is what I inserted into my DB.
Please give me some solution for this.
Reply | Threaded
Open this post in threaded view
|

Re: How to print thymelaf variable value from generated text

anil1712
Can anyone please give me solution for this.
Reply | Threaded
Open this post in threaded view
|

Re: How to print thymelaf variable value from generated text

sunil0791
Have it as a separate fragment i.e th:fragment and include the fragment passing the object

On Sat, Sep 27, 2014 at 2:36 PM, anil1712 [via Thymeleaf - User Forum] <[hidden email]> wrote:
Can anyone please give me solution for this.


If you reply to this email, your message will be added to the discussion below:
http://forum.thymeleaf.org/How-to-print-thymelaf-variable-value-from-generated-text-tp4028409p4028412.html
To start a new topic under General Usage, email [hidden email]
To unsubscribe from Thymeleaf - User Forum, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How to print thymelaf variable value from generated text

anil1712
Sorry, but I didn't get you. You can see the `[[${description}]]` renders the HTML like this `<p>[[${foo}]]<p>` and just I want to render `foo` value inside paragraph tag.
Reply | Threaded
Open this post in threaded view
|

Re: How to print thymelaf variable value from generated text

Emanuel
Administrator
In reply to this post by anil1712
If you want to print out HTML, then you need to use the th:utext processor:

<span th:utext="${description}">Description</span>

If your description contains stuff that you want processed however, then I think you'll need to do something else like come up with your own utility object that can pick out Thymeleaf expressions and call Thymeleaf to process them.  I'm not sure if outputting Thymeleaf templates _inside_ a Thymeleaf template will work without some custom code.

Otherwise, I'd do what the other poster above mentioned, and extract the HTML text to another Thymeleaf template that can be processed directly.
Reply | Threaded
Open this post in threaded view
|

Re: How to print thymelaf variable value from generated text

anil1712
Thanks for the suggestion... So I have created my custom `dialect` and `processor` but it still doesn't seems to work. Please give me some solution for this...
 
In spring controller
    String html = "<p th:inline='text'>[[{$foo}]]</p> <br /> <p th:inline='text'>[[{$bar}]]</p>";
    mav.addObject("foo", "Helloooooooo Foo");
    mav.addObject("bar", "Helloooooooo Bar");
    mav.addObject("htmlText", html);

Inside HTML Template
    <div an:evalcontent="${htmlText}">HTML content should be generate here with `foo` and `bar` actual values.
    </div>

Custom Processor class
    public class EvaluateHTMLContent extends AbstractStandardSingleAttributeModifierAttrProcessor {
	
	protected EvaluateHTMLContent() {
		super("evalcontent");		
	}
	
	@Override
	protected String getTargetAttributeValue(final Arguments arguments, final Element element,
            final String attributeName) {				
		return ExpressionUtil.evaluate(arguments, attributeName).toString();	
	}
	
	@Override
	protected String getTargetAttributeName(Arguments arguments,
			Element element, String attributeName) {
		// TODO Auto-generated method stub
		return "inline";
	}

	@Override
	protected ModificationType getModificationType(Arguments arguments,
			Element element, String attributeName, String newAttributeName) {
		// TODO Auto-generated method stub
		return ModificationType.SUBSTITUTION;
	}

	@Override
	protected boolean removeAttributeIfEmpty(Arguments arguments,
			Element element, String attributeName, String newAttributeName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getPrecedence() {
		// TODO Auto-generated method stub
		return 10002;
	}
}	

Please get me out of it. Give me any useful resource or link.