How to use the if condiation sentence in the docx template?



  • How to use the if condiation sentence in the docx template? example:

    if(condiation==1){
        {{displayA}}
    }else if(condiation==2){
        {{displayB}}
    }else{
        {{displayOthers}}
    }
    

  • administrators

    hi! this looks like a handlebars question, when in doubts always make sure to search the handlebars docs.

    to solve your issue you can define the following in your template helpers

    function compare (a, b) {
      return a === b
    }
    

    then the usage in the docx should be like this

    {{#if (compare condiation 1)}}
      {{displayA}}
    {{else}}
      {{#if (compare condiation 2)}}
        {{displayB}}
      {{else}}
        {{displayOthers}}
      {{/if}}
    {{/if}}
    

    http://handlebarsjs.com/guide/builtin-helpers.html#sub-expressions



  • @bjrmatos
    My template like this:
    {{#docxList Projects}}
    {{ClientName}} {{ClientAddress}}
    {{ZipCode}}, {{ClientCity}}
    {{ClientEmail}}
    {{ClientPhone}}

    {{#docxList CheckLists}}
    {{#if Level==1}}
    {{NodeNumber}} {{ChecklistName}} Utförd av: Datum:
    {{else}}
    {{NodeNumber}} {{ChecklistName}}
    {{/if}}
    {{/docxList}}
    {{/docxList}}

    I get a error as below:
    {"message":"Error while executing docx recipe. Error while executing templating engine. Parse error on line 12:\n...Pr><w:t>{{#if Level==1}}</w:t></w:r><w:b\n-----------------------^\nExpecting 'OPEN_SEXPR', 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EQUALS'","stack":"Error: Error while executing docx recipe. Error while executing templating engine. Parse error on line 12:\n...Pr><w:t>{{#if Level==1}}</w:t></w:r><w:b\n-----------------------^\nExpecting 'OPEN_SEXPR', 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EQUALS'\n at module.exports (C:\jsreportapp\node_modules\jsreport-core\lib\util\createError.js:11:13)\n at Reporter.createError (C:\jsreportapp\node_modules\jsreport-core\lib\reporter.js:328:12)\n at Object.execute (C:\jsreportapp\node_modules\jsreport-docx\lib\recipe.js:62:20)\n at <anonymous>\n at process._tickCallback (internal/process/next_tick.js:189:7)\ncaused by: Error: Error while executing templating engine. Parse error on line 12:\n...Pr><w:t>{{#if Level==1}}</w:t></w:r><w:b\n-----------------------^\nExpecting 'OPEN_SEXPR', 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EQUALS'\n at Object.callbackRequests.(anonymous function) (C:\jsreportapp\node_modules\script-manager\lib\worker-servers.js:250:31)\n at process.<anonymous> (C:\jsreportapp\node_modules\script-manager\lib\worker-servers.js:195:30)\n at emitTwo (events.js:131:20)\n at process.emit (events.js:214:7)\n at emit (internal/child_process.js:762:12)\n at _combinedTickCallback (internal/process/next_tick.js:142:11)\n at process._tickDomainCallback (internal/process/next_tick.js:219:9)"}

    How to fix this error?


  • administrators

    Hi!

    to be clear, in the previous reply i've tried to show you how to do condition checks in handlebars, conditions like this Level==1 does not work in handlebars, you need to replace that part with a helper, exactly like i was showing you in my last reply. the error is about trying to parse invalid handlebars syntax so you need to change the way you do the conditions checks.



  • @bjrmatos , thansks, works well for me now.


Log in to reply
 

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