The innerHTML of #welcome-user becomes equal to Leeroy Jenkins. "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. Its 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. Pug is a very powerful templating engine which has a variety of features including filters, includes, inheritance, interpolation, etc. 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. Templating engines are used to remove the cluttering of our server code with HTML, concatenating strings wildly to existing HTML templates. ![]() If you need to interpolate the value of a JavaScript object (e.g. 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. |