Anonymous template can't find child template also anonymous



  • Hi! I'm using jsreport.render() to rendering two anonymous templates, the main template and a child template for toc (table of contents). However, the main template does not find the child template, and only works when I add the child in jsreport studio. I tried to put the full and relative path on child declaration in main template, without success. How can I make the main template find the child template without having to add it in jsreport studio?

    This is an abstract of the code:

    jsreport.render({
                template: {
                    name: "main-template",
                    content: fs.readFileSync('path/to/main-template').toString('utf-8'),
                    engine: "handlebars",
                    recipe: "chrome-pdf",
                    chrome: {
                        printBackground: true
                    },
                    pdfOperations: [
                        {
                            type: "merge",
                            template: {
                                name: "child-template",
                                content: fs.readFileSync('path/to/child-template').toString('utf-8'),
                                recipe: "chrome-pdf",
                                engine: "handlebars",
                                chrome: {
                                    printBackground: true
                                },
                            },
                            data: {
                                toc
                            },
                            mergeWholeDocument: true,
                            renderForEveryPage: false
                        }
                    ]
                },
                data: {
                   mainData
                }
            }).then((out) => {
                return out.stream.pipe(res);
            }).catch((e) => {
                throw e;
            });
    


  • I see you send template.content and pdfOperations[0].template.content. This means both are processed as anonymous. The same code works for me and doesn't throw.

    I tried to put the full and relative path on child declaration in main template

    Where do you set the path on child declaration? Can you paste also the error? Thank you.



  • Sorry for delay. I set path inside content of main template:

    {#child ../path/to/child-template @template.recipe=html}
    

    When I run jsreport.render, I get this message:

    Child template "../path/to/child-template" was not found, skipping.
    


  • Child templates need to be stored in the jsreport templates store.
    You can't use them if you don't use a template store for persisting templates.

    You can choose to don't use template store and prepare template.content own your own.
    In your case in fs.readFileSync('path/to/main-template').toString('utf-8'),

    Or start using templates store, prepare everything in the studio, and then benefit from features like child templates and others.



  • Ok... so, I want keep using anonymous template, but I don't see another way in this case...

    Actually, my child template is a TOC, and its implementation is like this example: https://playground.jsreport.net/w/admin/akYBA4rS

    Is there another way to use TOC without child template?



  • Yes, replace the {#child ...} call with the content in the TOC template and merge the helpers with the helpers in your main.

    Child templates are there primary for readability and reuse. You should be able to reach the same with some work on your side.



  • Ok! Thank you!


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.