Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to save LiquidJS output to variable?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading