Locally-defined variables not accessible by handlebars - but they were last week?



  • Take the below template for example:

    <html>
        <head>
            <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
            <style>
                {#asset facilityStyle.css @encoding=utf8}
            </style>
            <script language="javascript" type="text/javascript">
                var myFace = "ugly"
            </script>
        </head>
        <body>
            <h1>What the {{myFace}} heck?</h1>
            <table class="facTable">
                <tr>
                    <th>Abbreviation</th>
                    <th>Name</th>
                    <th>ID</th>
                </tr>
                {{#each facilities}}
                <tr class="facRow">
                    <td>{{location_abbr}}</td>
                    <td>{{loc_name}}</td>
                    <td>{{loc_id}}</td>
                </tr>
                <tr class="nsTableRow">
                    <td class="nsTableRowCol" colspan="3">
                        <table class="nsTable">
                            <tr>
                                <th>Abbreviation</th>
                                <th>Name</th>
                                <th>ID</th>
                            </tr>
                            {{#each nursing_stations}}
                            <tr>
                                <td>{{dept_abbreviation}}</td>
                                <td>{{department_name}}</td>
                                <td>{{department_id}}</td>
                            </tr>
                            {{/each}}
                        </table>
                    </td>
                </tr>
                {{/each}}
            </table>
        </body>
    </html>
    

    #each facilities, which is pulling from a script works just fine, but the local variable, "myFace" is not showing up in the h1 at the top. Am I missing something? I don't think I changed anything from last week. Maybe I should start using the built-in version control so I'd know for sure.



  • Any ideas why {{myFace}} isn't working?


  • administrators

    hi! hmmm actually i would be very surprised that your variable in the script tag worked.. the reason is that there are differences in the execution of the code when rendering

    • we first run the template engine and do the data replacement (handlebars is run) on server side (with node.js), at this point the script tag in your template is not executed still because that html was not executed in the browser engine, at this point we are only producing the final html with handlebars so if myFace was not in the data then that part will be empty

    • after the template html is ready we open that html in a browser engine (chrome when using chrome-pdf engine), here your script tag is processed but at this point you are no longer in handlebars context

    if you want to create some variables in the data then you had to do that in your jsreport script beforeRender function by assigning values to a property of req.data



  • OK. I must have been imagining things.


Log in to reply
 

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