how to conditionally assign variable - nunjucks - html

I want to assign the variable that's exists and not undefined like how the short-circuit evaluation is done in Javascript
<label for="dummy-xyz">{{ data.dummy.id || dummy.id }}</label>
I'm unable to do it with the || operator however

Related

Check variable conditions in template [or,and]

I need to make if statement in html template.
Working:
{% if field == 'var_1' or field == 'var_2' %}
but i can't compare rendered variable with None or ' '
{% if field == 'var_1' or field == 'var_2' and 'var_3' != '' %}
last statement is not working.
var_3 in mysql is NULL or contains varchar(200)
What is proper solution to handle this?
You may looking for the isnull() function.
This function returns True if passed a value set to NULL
.....AND NOT IsNull(var3) AND....

Use function return in if statement or variable in go template

I'm using a go template to filter out improperly named services with consul-template. I am provided a function regexMatch from consul-template that works like this:
{{"foo.bar" | regexMatch "foo([.a-z]+)"}}
It returns true or false depending on the string. I'd like to use this conditionally in an if statement, similar to other code I have that filters out services with the name "consul". That works like this:
{{range services}} {{$service:=.Name}} {{if not (eq $service "consul")}}
The problem is that I can't nest the function call inside that if statement or a variable declaration. I have tried
{{if {$service | regexMatch "^[-a-z0-9]{1,}$"}}}}
and
{{$syntax= {$service | regexMatch "^[-a-z0-9]{1,}$"}}}}
but I get these errors:
unexpected "{" in if
unexpected bad character U+003D '='
How can I use the regexMatch function in an if statement in this go template?
If anyone ever needs it,
{{if $service | regexMatch "^[-a-z0-9]{1,}$"}}
from https://golang.org/pkg/text/template/.
specifically:
{{pipeline}}
The default textual representation of the value of the pipeline
is copied to the output.
{{if pipeline}} T1 {{end}}
If the value of the pipeline is empty, no output is generated;
otherwise, T1 is executed. The empty values are false, 0, any
nil pointer or interface value, and any array, slice, map, or
string of length zero.
Dot is unaffected.

passing constant values to ng-model

i am trying to pass a constant value to a label and trying to access it somewhere else.This is what i have tried
<label ng-model="alertID"> 1 </label>
and i am trying to access it in a controller by passing alertID to a function in controller.but i am getting "undefined" as alertID's value.Following is the function in controller
$scope.saveEventInfo = function(alertID){
var id = alertID;
}
How do i get its value?I dont want to use ng-init
ng-model on a label doesn't make sense because it's not an input and therefore doesn't need two-way binding.
I'm guessing you just want to initialise the value like this:
<label ng-init="alertID = 1">{{ alertID }}</label>
$scope.alertID will now be 1 on your controller.

How do I create a SWIG descriptor for an typedef?

I'm using SWIG 2.0. I have a C++ smart pointer class which I'd like to avoid exposing in my target language (in this case Python). A few typemaps should allow me to do this. Here's my first attempt at an "out" typemap for example:
%typemap(out) smart_ptr
%{
// Get the object from the internal pointer.
if ($1.get() == NULL)
$result = Py_None;
else
$result = SWIG_NewPointerObj($1.get(), $descriptor($1_type::target_type),
0);
%}
Using this typemap, if it worked, I should be able to expose smart_ptr< Foo > getAFoo(); as a function returning a Foo, not a SmartPtrFoo.
The problem is that SWIG generates the descriptor as "swigt__1_type__target_type" instead of expanding smart_ptr< T >::target_type to Foo to get, for example, "swigt_Foo".
I could do something like this:
#define SMART_PTR_TYPEMAPS(VALUE_TYPE)
%typemap(out) smart_ptr< VALUE_TYPE >
%{
// Get the object from the internal pointer.
if ($1.get() == NULL)
$result = Py_None;
else
$result = SWIG_NewPointerObj($1.get(), $descriptor(VALUE_TYPE),
0);
%}
#enddef
but this forces me to expand a macro for every type for which I use a smart_ptr. I'd rather let the SWIG typemap search mechanism do the work.
Is there a way to generate a descriptor for an expanded typedef?

Using variable within a literal

In my code I need to declare notationArr1 but I'm getting this error: Error #1010: A term is undefined and has no properties.
if ((notationArr[1].length == 2) && ((notationArr[1].charCodeAt(0) >= 97) && notationArr[1].charCodeAt(0) <= 104) && ((notationArr[1].charCodeAt(1) >= 49) && notationArr[1].charCodeAt(1) <= 56)) {
if (pieces.d3.man == "") {
pieces.notationArr[1].man.y = pieces.d4.y;
}
}
Here, pieces is an object.
Edit: More code: http://sudrap.org/paste/text/44915/
One of the many variables in your little code piece was not properly declared and/or initialized. You can only access properties or methods (every time you write something.something, that's the part after the .) on existing Objects, but not if the variable you are trying to access contains null.
EDIT
Having read your longer code piece, there could be several null variables, but your problem is probably what #AsTheWormTurns mentioned in his comment above:
pieces.notationArr[1].man
will try to access an array called notationArr that is a member of pieces, instead of using an evaluation of the content of notationArr[1] to find out which member of pieces to access. It should be:
pieces[notationArr[1]].man