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
Render template in method #330
Comments
Hi @replay111, I'm afraid I didn't got your question. Can you provide an example of what you're trying to achieve? |
Hi, some "example" code below:
I know that will not run but I am interested mostly in part:
to render some template and return it as string to some variable :D Maciej. PS: I am using Your JTwig library in version: 3.1.1 |
We are not maintaining version 3 anymore. But it should be possible to do. |
OK I cant switch to v5 - but generally I think that this kind of feature may work with any version :-) |
Ok, I think that I found a solution (http://stackoverflow.com/questions/22422411/how-to-manually-render-spring-mvc-view-to-html/22422874#22422874) so I created small class: @Service
public class JTwigRenderToStringService {
@Autowired
ViewResolver viewResolver;
public String renderTemplate(String tplName, Map<String, Object> tplParameters, HttpServletRequest request) {
String res = null;
try {
MockHttpServletResponse mockResp = new MockHttpServletResponse();
View view = viewResolver.resolveViewName(tplName, Locale.getDefault());
view.render(tplParameters, request, mockResp);
res = mockResp.getContentAsString();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
} The only problem is that I don't want to have Maciej. |
I'll leave here another example which doesn't make use of test support classes. @Service
public class JtwigRenderService {
private final JtwigViewResolver jtwigViewResolver;
@Autowired
public JtwigRenderService(JtwigViewResolver jtwigViewResolver) {
this.jtwigViewResolver = jtwigViewResolver;
}
public String renderTemplate(String tplName, Map<String, Object> tplParameters) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JtwigView view = (JtwigView) jtwigViewResolver.resolveViewName(tplName, Locale.getDefault());
jtwigViewResolver.getRenderer()
.dispatcherFor(view.getUrl())
.with(tplParameters)
.render(outputStream);
return outputStream.toString();
}
} |
Gr8! |
Ok I've got error: Error:(28, 26) java: cannot find symbol (I did copy&paste Your code) pom: <dependency>
<groupId>org.jtwig</groupId>
<artifactId>jtwig-spring</artifactId>
<version>5.85.0.RELEASE</version>
</dependency> |
Hi,
I want to return html as response in my json structure (one of the field), and I wanted to render this html in method using Your JTwig engine. Is it possible to do this?
The text was updated successfully, but these errors were encountered: