How to declare variables in DUST? - node.js

I have a javascript code to declare a variable. i need a way to do the same in DUST??
<script type="text/javascript">
String flag = "ON";
</script>
how to do this DUST???

Dust is logicless template, so can't use logic like javascript. But if you want to using a variable as a flag, and you are using partials, you can add like this
{>partials flag='ON' /}
And in that partials you can use that variable.

Dust templates are meant to be logicless, so there would be no adding new variables from inside it. Ofcourse you can add properties to the object you pass to be rendered as variables.

Related

How to populate a dropdown in pug

Switching from handlebars to pug I don't know how to populate a dropdown in pug. In handlebars I could do
<script type='text/javascript'>
$('.ui.dropdown').dropdown('set selected', [{{#each trip.tags}}'{{this}}',{{/each}}]);
</script>
Anyone got a clue what's the best practice in pug?
It looks like you're trying to generate JavaScript values as part of your template rendering. The approach you have taken in markdown could cause an XSS attack if this is not properly escaped (or it might just cause values like " to appear as ").
In pug, we recommend using js-stringify when you need to embed template values in a script. To do this, you need to install js-stringify using npm. You then need to include it in your locals. e.g.
pug.renderFile('my-template.pug', {stringify: require('js-stringify')});
Then you can use it as:
script(type='text/javascript').
$('.ui.dropdown').dropdown('set selected', !{stringify(trip.tags)});
N.B. It is only safe to use the !{...} syntax because js-stringify properly escapes the values before rendering.

can you set is_safe for a global in a twig extension

Twigs documentation for extensions show that it is possible to use "is_safe" with both simple_filters and and simple_functions, to prevent escaping of html tags in returned values, but I can see any examples of using is_safe with globals. Is there a way to do this?
If your global is pure HTML that needs to be rendered like HTML you could mark it as safe by using
$twig->addGlobal('my_html', new Twig_Markup($html, 'UTF-8'));
If the global is an object and returns the HTML you wrap your return value with a new Twig_Markup

How check if attributes is in declaration Polymer

Lets say I have a custom element defined as
<polymer-element name="my-elem" >
<template if={{show_is_in_my_declaration?}}>
....
</template>
<script>
Polymer('my-elem', {});
</script>
To use it I would like to declare it as...
<my-elem show></my-elem>
Where including 'show', makes the template appear; similar to how things like 'flex' or 'fit' effect the element. What is this called and how do I implement it.
note: I don't want to write something like show="{{true}}"
It's called Conditional Templates.
Here is the docs:
https://www.polymer-project.org/docs/polymer/template.html#if
and here are some examples on how to use them:
https://github.com/Polymer/TemplateBinding/tree/master/examples/how_to

Newbie Node: referencing variables in javascript passed by res.render

If I have a module that returns runs this:
res.render('index', {
title:'Hello World'
});
I can access the title in jade by using #{title}.
How would I access it in separate .js javascript file included in the jade file?
To access a jade variable in an external JavaScript file, the variable must be declared in a script tag before you link to the relevant JavaScript file.
For example, if you have a script.js file that needs to access the title variable in jade, you could use the following code in your jade file:
script.
var jadeType = #{type}
script(type='text/javascript', src='./script.js')
The jade variable type can now be accessed in script.js using the JavaScript variable jadeType.
Hope this helps, if anyone is still looking for an answer to this question.
I'm not familiar with jade, but assuming you mean another javascript file included on the rendered page, then you can probably do something like:
<script type="text/javascript">
var title = '#{title}';
</script>
You can't reference those thingie in external js files, included in your jade template.
Please, have a look here: Accessing Express.js local variables in client side JavaScript

Variable scope in aspx page

Why is that the scriptPath variable is out of scope in the bottom of the code?
Shouldn't it be in scope throughout this page? In MVC, if I mark this on top of the page like
#{
string scriptPath = "assets/scripts/",
gkoConfig = "GkoConfig.js";
}
it is available throughout the current View. What am I missing now that I'm back to WebForms for a while?
If I change the code position, It get's weirder as inside the <head> I no longer have access to teh variable, but I do have, inside the <body> now... :-/
When you declare a variable in a Web Forms .aspx file, you’re actually declaring a local variable inside an auto-generated rendering method. ASP.NET generates separate rendering methods for all tags marked runat="server", so you actually get a separate method for your head element. Now, the variable you declare can only exist in one of these methods - hence the 'weird' behavior.
You can see how this works if you pre-compile your application using aspnet_compiler.exe. You will get compiled DLL files for each of your web forms pages; just open one of those up in Reflector to see the generated code. I wrote a minimal equivalent of your code with the variable declared outside the head tag, and here’s the top-level render method that I got:
private void __Render__control1(HtmlTextWriter __w, Control parameterContainer)
{
string str = "scripts/";
__w.Write("\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
parameterContainer.Controls[0].RenderControl(__w);
__w.Write("\r\n<body>\r\n ");
parameterContainer.Controls[1].RenderControl(__w);
__w.Write("\r\n <script type=\"text/javascript\" src=\"");
__w.Write(str);
__w.Write("jquery-1.4.1.min.js\"></script>\r\n</body>\r\n</html>\r\n");
}
You see that the variable that I declared (here named str) is scoped to this method, and it's calling other methods to render the head (and a form element marked runat="server".)
A quick and dirty solution might be to simply remove the runat="server" from your head tag; however, I’d recommend that you declare a protected variable in your code-behind class for this. Adding a line like this to your code-behind file would work:
protected string scriptPath, gkoConfig;
You can then use these variables anywhere in your Web Forms code.
You could also declare your constants like this:
<script runat="server">
private const string scriptPath = "assets/scripts/";
private const string gkoConfig = "GkoConfig.js";
</script>
I suspect the head of the ASPX page gets processed separately from the body.
This problem is easily solved - all you need to do is use a class field in the code-behind with the access level set to protected.

Resources