How to use Smarty templates in Node.js

Larger projects can lead you to use multiple technologies. Say you are using Smarty templates in PHP and want to use/share same templates in Node.js. jSmart is way to go. jSmart is a port of the Smarty Template Engine to Javascript. jSmart can work in browsers and CommonJS environments like Node.js. I will demonstrate how we can use jSmart in Node.js

1. Install jSmart from NPM Registry.

2. First create template with some data we are supposed to render. Sa
y demo.tpl and it contents are:-

3. Create demo.js in the same folder which includes jsmart and fs module.

4. Now we will read the content of template file and create jSmart object which compiles our template.

Note:- I’m reading file synchronously for demo purpose. Best is to read file asynchronously. That’s is what the + point of Node.js.

5. We are ready assign data to the template and print output of the template.

6. Now save the file i.e demo.js. After that you are ready to execute the file and see the output.

7. You would see string “Hello World“.

On the similar lines you can use all the features of Smarty. You can find documentation at

If you clone the jSmart repo from Github, you would find various examples which shows how jSmart works on browser and using require.js.

I have demoed a quick tutorial. If you are expecting something more from tutorial / demo, do let me know in the comments. I will happy to update.



3 Comment

  1. How we can use it in expressjs webframework?

    1. Umakant says: Reply

      I have actually never worked with expressjs. If I find time to explore, I’ll definitely add another blog for expressjs.

  2. akshay says: Reply

    Well, in express all you have to do is to create your smarty variable and pass the template name to be rendered as you route to your files in express

Leave a Reply