简体   繁体   中英

How to get the value from controller to handlebar (handlebar.js javascript templating)?

I have set value in model like this

        model.val = 'myval'
    render(view: 'index', model: model)

& in gsp is there some way by which I could directly get the value of model something like this?

<script id="myTemplate" type="text/x-handlebars-template">
  {{val}}
</script>

if I try with {{${val}}} then I get the value but is that right way to do it? I mean expression inside handlebars

server.js

const body_data = {
        nodes: [
          {data: {id: 'j', name: 'Jerry'}},
          {data: {id: 'e', name: 'Elaine'}},
          {data: {id: 'k', name: 'Kramer'}},
          {data: {id: 'g', name: 'George'}},
          {data: {id: 'd', name: 'Daniel'}},
          {data: {id: 'r', name: 'Rea'}}
        ],
        edges: [
          {data: {source: 'j', target: 'e'}},
          {data: {source: 'j', target: 'k'}},
          {data: {source: 'j', target: 'g'}},
          {data: {source: 'e', target: 'k'}},
          {data: {source: 'k', target: 'g'}},
          {data: {source: 'd', target: 'g'}},
          {data: {source: 'g', target: 'r'}},
        ]
      };

    res.render('home', {
        nombre: 'Leonardo',
        body_data,
    });

HELPER

const hbs = require('hbs');
hbs.registerHelper('muestraData', ( data = {} ) => {
    return JSON.stringify(data);
});

home.hbs

<script>
    const data = {{{ muestraData body_data }}};
    console.log(data);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM