Home > Back-end >  How to save LiquidJS output to variable?
How to save LiquidJS output to variable?

Time:08-26

On the frontpage of https://liquidjs.com/ they give this example

import { Liquid } from 'liquidjs'
const engine = new Liquid()
const tpl = engine.parse('Welcome to {{v}}!')
engine.render(tpl, {v: "Liquid"}).then(console.log)

Question

How can I save the output in a variable instead of printing it right away?

CodePudding user response:

First of all you should ask yourself if that is really what you need. From my point of view this is against how template engines should be used.

However, you can simply assign the result of the render function to a variable and later resolve the promise.

const engine = new Liquid()
const tpl = engine.parse('Welcome to {{v}}!')
const result = engine.render(tpl, {v: "Liquid"}) //returns promise

result.then((value) => {
  console.log(value);
});

  • Related