Difficulties passing data to child template then retrieving it



  • Hi Jsreport team!

    Once again I come to you to get some more information, this time it's about passing data to child templates (using handlebars, wkhtmltopdf recipe).

    Original plan was to dynamicaly impact the header depending of the content of a #each loop but couldn't succed nor find anything on that specific case so I decided to go to child template.

    After reading your explanation in the Learn section, I tryed this call:

    {{#each students}}
      {#child child_langaj_company_student.header @data={{ @this }} }
    {{#each students}}
    

    But then it wouldn't accept @this as a valid parameter, so I've updated jsreport to 1.7 (was using 1.6.1 before), then the following error showed instead:

    Error occured - Error during rendering report: Error during rendering report: Unexpected token o in JSON at position 1
    ...
    

    At first it didn't work because it seems the {{ @this }} was no accepted (did try try then 0 still without success)

    then I though maybe I add to add a special parameter so the code would be like this:

    {{#each students}}
      {#child child_langaj_company_student.header @data.student={{ @this }} }
    {{#each students}}
    

    and the report did successfully generate BUT the data passed to the child template would be like none was passed. So all corresponding field stay empty.

    Any idea about what I could have done wrong? Or better, any suggestion of how I could pass directly the @this value into template native header, so I son't have to ressort on child template?

    Thank you very much in advance but also for that awesome product that is Jsreport!


  • administrators

    hi @MI53RE i'm afraid that we don't have support for passing objects to child templates yet. however we have an issue open for this feature https://github.com/jsreport/jsreport-child-templates/issues/1 so it will be probably implemented soon.

    in an scenario which we have support for passing objects to child templates the syntax would be something like the following:

    {{#each students}}
        {#child child_langaj_company_student.header @data.student={{{toJSON this}}} }
    {{/each}}
    

    or

    {{#each students}}
        {#child child_langaj_company_student.header @data={{{toJSON this}}} }
    {{/each}}
    

    Note: the {{{ }}} is just handlebars syntax to tell it that the content is safe and it should not be escaped

    you can workaround this limitation by passing each property that you need to the child template, something like this:

    {{#each students}}
      {#child child_langaj_company_student.header @data.student.name={{ name }} @data.student.age={{ age }} }
     <!--  student object in child template will have name and age properties-->
    {{#each students}}
    

    this is my best suggestion for now


  • administrators

    where toJSON is a helper defined like this:

    function toJSON(value) {
        return JSON.stringify(value)
    }
    

    but this will only work when we have support for passing objects to child templates



  • Sorry for the late answer, in the end I changed the template using assets instead of child_templates which even solved some other difficulties I had.
    But definitely thank you for taking some of your time and answering me!

    (by the way I cannot use my previous account and only facebook, twitter or github connections seems available now, is it normal?)



  • Yes, forum supports now only social logins. I apologize for inconvenience.



  • So has this been fixed?
    anytime I try to execute a child_template by passing it data
    {#child someSubReport @data.SessionID={{ this.SessionID }} @data.ClientID={{ this.ClientID }} @data.Query={{{ this.Query}}} }
    it either simply prints the text as seen above or crashes by not sending the right info.


  • administrators

    @Oshri-Cohen passing objects is still not supported. but according to your example i think it should be @this, also be sure that @this equal to your expected context (object), handlebars context is affected if you are inside an #each iteration. if you need more help create small example in playground and i will tell you why is not working as expected


  • administrators

    @Oshri-Cohen sorry, this should work just fine in template. forget the comment about @this 😃



  • how hard would it be to do this for the child-templates? Can i write my own extension for this?


  • administrators

    to be clear the snippet of code that you commented should work. if it is not working maybe is because some bad usage in the template or something else and i would need to see the full template to have an idea of the why is not working.

    what it is not supported is passing an object:

    {#child child_template @data.student={{@this}} }
    

    if you need that feature and want to try to add the support for it you can take a look at the source code and try to tweak it to add the support for the objects.



  • ok i understand, so simple items like a string can be passed, but referencing the entire object does not correct?


  • administrators

    ok i understand, so simple items like a string can be passed, but referencing the entire object does not correct?

    yes, exactly.


Log in to reply
 

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