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

Render template in method #330

Closed
replay111 opened this issue Feb 20, 2017 · 8 comments
Closed

Render template in method #330

replay111 opened this issue Feb 20, 2017 · 8 comments
Labels

Comments

@replay111
Copy link

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?

@joao-de-melo
Copy link
Member

Hi @replay111, I'm afraid I didn't got your question. Can you provide an example of what you're trying to achieve?

@replay111
Copy link
Author

replay111 commented Feb 21, 2017

Hi,

some "example" code below:

@RequestMapping(value = {"/items"}, method = RequestMethod.POST)
@ResponseBody
public JSONResponse getDictionaryItems(@RequestBody long pm) {


    // I do not know if this is possible:
    Jtwig jtwig = new JTwig.setTemplate("dictionary/dic_items.html.twig");
    jtwig.model.put("dictionaryData",dicData);

    // This is part of the json object I want to send back
    String renderedTempalte = jtwig.renderTemplate();

    // RequestResponse is my custom class to build json for response
    return RequestResponse.setOK("html",renderedTempalte);
}

I know that will not run but I am interested mostly in part:

    // I do not know if this is possible:
    Jtwig jtwig = new JTwig.setTemplate("dictionary/dic_items.html.twig");
    jtwig.model.put("dictionaryData",dicData);

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

@joao-de-melo
Copy link
Member

We are not maintaining version 3 anymore. But it should be possible to do.

@replay111
Copy link
Author

replay111 commented Feb 22, 2017

OK I cant switch to v5 - but generally I think that this kind of feature may work with any version :-)

@replay111
Copy link
Author

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 HttpServletRequest request ;-) but all in all it works :D

Maciej.

@joao-de-melo
Copy link
Member

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();
    }
}

@replay111
Copy link
Author

Gr8!
thanx for help ;-)

@replay111
Copy link
Author

Ok I've got error:

Error:(28, 26) java: cannot find symbol
symbol: method getRenderer()
location: variable jtwigViewResolver of type org.jtwig.spring.JtwigViewResolver

(I did copy&paste Your code)

pom:

        <dependency>
            <groupId>org.jtwig</groupId>
            <artifactId>jtwig-spring</artifactId>
            <version>5.85.0.RELEASE</version>
        </dependency>

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

2 participants