Set temporary variable and reuse it further down in pug template - node.js

I am trying to pass along some values from my nodejs/express backend into the template (using res.render({...})), store it in some variables, then pass it further up until the front end (The usecase is that I need to do some mild calculation on the passed values and stored them in some intermediate variables)
I know I can do this in the pug file
block append script
script.
const foo = parseInt(#{stat.get('fooStat')}, 10);
But then later it seems that I can not refer to this later in my pug template, say here
span #{foo}
The value is empty, I guess because it is undefined... What's the usual way to accomplish that.

Adding the period after the script tag signals to pug that its contents should be treated as plain text. This is handy when you want to write javascript for client-side consumption, but any variables declared are inaccessible to pug.
script.
const foo = 10;
span #{foo} // foo is undefined here
If you want to define a variable that pug can use later in the template, do so using unbuffered code.
- const foo = 10
span #{foo} // renders <span>10</span>

Related

Svelte store not triggered when value chages, only when $: is prefixed

As I understand, when using Svelte store with $ prefixed it's automatically subscribed to value changes.
Having the following code, whenever users.name store value is changed it should trigger one of the two statements.
<script>
if (!$userS.name) {
list = Promise.reject({message:'You are not allowed to view this section!'});
} else {
list = getList('api/carousel/' + escape(term));
}
</script>
But the only way the previous code is working is when the if statement it's prefixed by "$:".
$: if (!$userS.name) { ...
So why is need the extra reactive dollar sign if it's already subscribed to store value changes?
Your component init script, that is the code inside the<script> tag, is not reactive. It runs once when your component is created and that's all. Think class constructor.
Only the code in reactive blocks $: is kept in sync and reruns when (reactive) variables they contain change.
The $ prefix gives you the value inside the store. In your example, the user variable is the store itself. console.log it, you'll see it is an object with a subscribe method etc. The prefixed variable gives you the value inside the store. Behind the scene, Svelte makes a subscription to the store (i.e. calls its subscribe method) when your component is created, and unsubscribes when it is destroyed.
Meanwhile, if the value changes, the $ prefixed variable will be reactive. But it will only have an effect if it sits in a reactive context. That is, either a reactive expression/statement $:, or in the markup. This is true for any reactive source (props, top level variables...).

how to define user defined constant globally in nodejs

Here, my question is, we use define function in php to define the
contant globally but how can i achive same thing in nodejs
in php:
define(nameofthecontant, value);
in nodejs how to?
var dataBaseInfo = result.databaseInfo;
// global.DB_HOST = dataBaseInfo['DB_HOST'];
for(var keys in dataBaseInfo) {
global.keys = dataBaseInfo[keys];
}
i have data in result.databaseInfo from database xml file and m trying to create global contant using for loop, its not working
This is how you can define a variable in the global namespace:
global.nameofthecontant = value
This is something that you don't want to do in node.js though. Your question possibly is a duplicate of this question: node.js global variables?
Change your code from this:
global.keys = dataBaseInfo[keys];
to this:
global[keys] = dataBaseInfo[keys];
When you want to access or assign a property and the property name is in a variable, you use the obj[variableName] syntax.
You could also use Object.assign here if you just want to copy a bunch of properties from one object to another:
Object.assign(global, databaseInfo);
As others have said, it is usually frowned upon to use globals in this way in node.js. Instead, you would typically expose these constants in a module and then just require in the module in any other module that wants to access the constants. This makes your code more modular and makes each module more self-contained.

I need to assign a value to a variable in some method as beforeRender, as I do?

I need to assign a value to a variable in some method as beforeRender, as I do?
(Sails.js v0.11.0)
Example CakePHP:
public function beforeRender(){
...
}
Example Rails:
before_render : ....
You can set locals in many places. It depends on your use case.
If the variable is different for each action, then you can place it inside the action calling the view.
If the variable is different for each request, then you can place it in a policy to set locals.
If the variable is static for a single route, you can put it in your routes.
If the variable is static for an application, then you can place in many places.
You can use Locals or Globals in your rendered view, which means any services you creates (even static objects) can be used inside your renderedViews
http://sailsjs.org/#!/documentation/concepts/Views/Locals.html
http://sailsjs.org/#!/documentation/concepts/Globals

Passing variable from jade to ng-init not working

I'm trying to pass an object from jade to ng-init in angular
This: doesn't work:
ng-init='tables=!{JSON.stringify(tables)}'
This: expands but,
ng-init='tables=#{JSON.stringify(tables)}'
the output is unescaped and filled with "s
ng-init="tables={"12":{"id":....
and the view isn't updated in either of the cases. This article implies that first one should work, but like I said, it doesn't even expand,
ng-init='tables=!{JSON.stringify(tables)}'
in source code shows up exactly the same in the HTML source
ng-init='tables=!{JSON.stringify(tables)}'
Actually, the #{...} approach seems to work fine.
It is probably the way console.log prints attributes' values that confused you.
ng-init="tables=#{JSON.stringify(tables)}"
Take a look at this short demo.
In what use-case you want to pass data directly from Jade to angular? I think you could to this job in controller like this :
$scope.init = function () {
// init stuff
}
...and in your view :
ng-init = init()

Naming Vars with strings

Can a variable be named with a string or character array, in any language? Basically I want something like:
Var_String = "varname"
Var_String as double
And then I could fill the double varname.
If it helps im trying to make a program that can declare variables on the fly, while running. Even if thats not possible, I am open to workarounds even if they're impractical, although I would prefer that workarounds be in VB6, C++, or PHP, because I know those languages already, but they dont have to be.
Javascript is completely capable of declaring variable names on the fly. A javascript object can be treated "associatively" as a dictionary. Observe:
var testyObject = function()
{
Awesome = "hello";
};
var myObject = new testyObject();
alert(myObject.Awesome); // creates an alert window that says hello
alert(myObject['Awesome']); // the same as above
myObject[myObject.Awesome] = "woo!"; // We just created a property on the object with the name "hello"
alert(myObject.hello); // creates an alert window that says "woo!"
I also believe you can add them to your immediate scope rather than as properties on other objects by using this["whatever you want it named"] = "woo!"; but I'm not certain, someone can correct me on that if such does not work.
You can read more about associative arrays at http://www.quirksmode.org/js/associative.html
The usual way to do something like this is called a hash. You store name/value pairs and given the name, can look up its value. You can generally define them to store any sort of object. In fact, in some languages, objects themselves are essentially hashes with a few extra properties.
You can find more information on wikipedia: http://en.wikipedia.org/wiki/Hash_table

Resources