Passing objects to child templates?



  • I have searched and read what I could find using the search term 'child' here on the forum. I saw a few old discussions about passing objects to child templates but they ended with "can't do that yet". I also found a ticket that is resolved that was supposed to solve this. But in reading the docs on child templates, I don't understand how to do it.

    I see I could use assets, but I think that I'd have the same question there too, wouldn't I? How to pass objects?

    Essentially, I have an {{#each patients}} loop and I want to call a child to render a header - because I'll reuse it for a number of reports. So I was hoping to just pass the current patient, which has elements and sub-objects as members.

    Is there an example in the playground that uses child templates? I looked through 6 or so and didn't find one. I wasn't clear on whether child templates need to include a full HTML/HEAD/BODY, or could just be various BODY content like DIVs and TABLES.



  • This is already implemented. See here how you can pass the object to the child templates evaluation
    https://jsreport.net/learn/child-templates#set-child-template-parameters

    I wasn't clear on whether child templates need to include a full HTML/HEAD/BODY, or could just be various BODY content like DIVs and TABLES.

    Child templates is basically just a string replace.
    We find {#child ...}. Run that tepmplate. And replace the tag declaration with the output.
    In other words it can produce any string. It doesn't need to produce full html.



  • Hmm. I suppose I didn't understand that page and how to apply it. In my case where I have:

    <div>
        {{#each patients}}
            <div>
                {#child myChild @data.currentPatient$={{{childTemplateSerializeData currentPatient}}} }
            </div>
        {{/each}}
    </div>
    

    How do I pass the currentPatient, where currentPatient is what is looked at this time around in the #each? Do I use "this"? I think I understand that I could pass an element of patient, but I don't see how to pass the whole patient. For example, given this structure:

    {patients:
    [{name: 'name1', location: 'departmentA', visits: [{many visits}]}, {name: 'name2', location: 'departmentA', visits: [{many visits}]}]
    }
    

    I think I could do this to pass the visits:

    {#child myChild @data.visits$={{{childTemplateSerializeData visits}}} }
    

    But like I said, I want to pass the whole current patient.

    Do I do this:

    {#child myChild @data.this$={{{childTemplateSerializeData this}}} }
    

    I'll try some things, but I still don't know. It might be good to add an example to that page you linked to and include a hypothetical JSON structure.


  • administrators

    try this {#child myChild @data.currentPatient$={{{childTemplateSerializeData ./this}}} }

    example: https://playground.jsreport.net/w/bjrmatos/EtZ31GDJ



  • That did the trick. Thanks a lot.


Log in to reply
 

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