gotemplate-node

0.7.6 • Public • Published

gotemplate-transpiller

Makes it possible to convert Go Template to JavaScript code

gotemplate-node

Example

Go Template:

{{if eq .Exist true}}
        <div class="d1">{{$.Obj.Text | printf "%s %s" "dom" | printf "%s" (printf "%s" 10 | printf "%s %s" true)}}</div>
{{else if .Some}}
    {{if eq .Obj.Text "Hi"}}
        <div class="d3">{{.Exist}} Hello2.2</div>
    {{end}}
{{end}}

Use:

const { GoHtmlTranspiler, functions } = require("gotemplate-node/dist/index");

const result = new GoHtmlTranspiler(goTemplate).run();

Result JS:

(() => (__rd, __fs, __tmp) => {
    const __ld = __rd;
    const __ltmp = { ...__tmp };
    let r = '';
    r += ``;
    if (__fs.eq(__ld.Exist, true)) {
        r += `
    <div class="d1">`;
        r += __fs.printf("%s", (__fs.printf("%s %s", true, (__fs.printf("%s", 10)))), (__fs.printf("%s %s", "dom", __rd.Obj.Text)));
        r += `</div>
`;
    }
    else if (__ld.Some) {
        r += `
    `;
        if (__fs.eq(__ld.Obj.Text, "Hi")) {
            r += `
        <div class="d3">`;
            r += __ld.Exist;
            r += ` Hello2.2</div>
    `;
        }
        r += `
`;
    }
    r += `
`;
    return r;
})();

Run:

const templateFuction = eval(result);
const data = {
    Exist: true,
    Some: false,
    List: [
        {
            Id: 1,
            Name: 'Leo',
        },
        {
            Id: 2,
            Name: 'Den',
        },
    ],
    Obj: {
        Text: 'Hi',
    },
};

const renderResult = templateFuction(data, functions, {});
console.log(renderResult);

Readme

Keywords

none

Package Sidebar

Install

npm i gotemplate-node

Weekly Downloads

1

Version

0.7.6

License

MIT

Unpacked Size

61.4 kB

Total Files

34

Last publish

Collaborators

  • darkfoxs96