Search code examples
node.jsajaxpug

Pass variable from ajax to jade/pug


Am obtaining the values from nodejs via ajax/fetch and need to pass the same to Pug. Any help here would be much appreciated

sample.pug
      button#searchVal Search
      script(type='text/javascript', src='/lib/onClick.js')
      br
      br
      table#table(div='')
        each row in slaJobs
         tr
          th#cbs-tab-header(div='') !{row.jobname}
          th#cbs-tab-header(div='') !{row.job_type}
          th#cbs-tab-header(div='') !{row.autosys_instance}

dummy.js

    .then(function(data) {
      console.log(data.jobsHeader)
      console.log(data.slaJobs)
       })

Need to set value of data.slaJobs from js to slaJobs of Pug


Solution

  • if you can afford to refresh the pug page for the result:

    1. make a request with ajax to some route in node.js.
    2. in the route handler - where you render the pug view - you can pass arguments to your view template. so when the pug page will be rendered - It will already contain your data.

    if you can't afford to refresh: 1. script tag in your template. handle the response and inject the data to the HTML page like any other AJAX