![]() Inside that create a file called first_view.pug, and enter the following data in it. Add the following code to your index.js file. Now that Pug is installed, set it as the templating engine for your app. To use Pug with Express, we need to install it, There is a lot of ground to cover on this. Pug is a very powerful templating engine which has a variety of features including filters, includes, inheritance, interpolation, etc. Templating engines are used to remove the cluttering of our server code with HTML, concatenating strings wildly to existing HTML templates. The innerHTML of #welcome-user becomes equal to Leeroy Jenkins.Pug is a templating engine for Express. "address": "123 Wilson Way, New York NY, 10165" If you were to do output the escaped variable ( var user = #), you would receive an escaped version of the object (where quotation marks and apostrophes are converted to '), which is not what we want in order for JSON.stringify to work on it.Īddress: "123 Wilson Way, New York NY, 10165"ĭocument.getElementById("welcome-user").innerHTML = setUsername(user.name) It's also necessary to output the raw contents of the variable, instead of the evaluated form of it. all the information about a user), you must stringify the output in Pug for it to be treated as a JavaScript object. If you need to interpolate the value of a JavaScript object (e.g. Sets the username of the current user to be displayed site-wide ![]() ![]() This is not necessary for number, where JavaScript will interpret our number as it we intend it to (as a number). 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. ![]() ![]() In the case of variables, numbers, strings, and the like, you can pass these types of variables directly into your JavaScript with bracket syntax plus an explanation point (so that the code inside the brackets is not evaluated.) This is useful for parametrizing JavaScript code that require something from your server. Interpolating values is helpful if you need to pass a server-side variable to client-side JavaScript (or other languages that require it). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |