![pug template output json pug template output json](https://i0.wp.com/techtutorialsx.com/wp-content/uploads/2019/05/image-2.png)
My issue is that my title and description variables live inside my axios function, and when I try to assign these values to homeCards, which is declared outside its scope, it can't find them. We’ll make use of axios as our HTTP client for the requests to the Random User API.
#Pug template output json install
The first is going to initialize our folder with a package.json, and the second will install the npm dependencies needed. I store the fields I want in the title and description variables and pass these variables to the homeCards. In a folder of your choice, run the following commands: npm init -y npm install express pug axios. In the below example, we have to wrap username in quotation marks in order for JavaScript to interpret it as a string Pug will output the content of the variable as-is, so we need to put it in quotation marks for it to be a proper JavaScript string. I'm using gulp to build the final output files. These templates are very basic, like replacing h1 and p strings or image paths. I want to write templates in pug, that are populated with data from an external json file.
![pug template output json pug template output json](https://media.geeksforgeeks.org/wp-content/uploads/20200703142426/pug-cli-install.jpg)
#Pug template output json how to
However, the template function will be re-compiled every time render is called. I don't understand how to use json as a simple database for my templates. I use Axios to request the JSON data and have a for loop to iterate through it. render() family of functions that combine compiling and rendering into one step. This project has full Pug templating support however there are a few small caveats to be aware of. We can now pass this file into gulp-pug like we did before, and use it to render our Pug templates. data directory, sets the filename as the namespace for the inner JSON, and the merges it into a single JSON object in /temp/data.json. I am trying to parse some JSON data into some elements called homeCards. Our pug:data task iterates through our JSON files in the.