meteor template compare/passing a variable - compare

I want to do what should be a simple compare but I have been stuck for the longest time on this:
I have a template that calls a list template, each list is a new column.
For the first column, I do NOT want certain fields displayed. Each column has a variable {{type}}
I want to do either:
a) pass type to the list template so that list can render based on type equals something
or
b) do an compare on type before I call list (in an 'each' loop) - e.g.
{{#if $eq myVar "test"}}
Show this text
{{/if}}
Neither approach works.
This seems to be the simplest thing but I am scratching head and unable to find examples.
thank you
Max

Unsure if this does what you want to achieve, it would help if you post some of your HTML. But to do what you want in b) couldn't you do:
{{#if isEqual myVar "test"}}
...
{{/if}}
and
Template.example.helpers({
isEqual: function(var1, var2) {
return var1 === var2;
}
});

It is probably easier to use collection.find({type: someType}) to just give your template what it should show. Make someType a session variable and you are done.
This sort example shows this pattern.

Related

How to list only item containing string X in a string Y from a dictionnary in Jinja2

I’m trying to obtain the count of all title containing a specific string from a dictionary in a jinja template.
I have tried many variant with select and other test and haven’t found the right way to do so.
This is not for ansible for which I have found many solutions to fix this issue.
Also the test equalto does not work since I do not have an exact match on the string and doesn’t seem to take regex.
{{ Dictionnary | selectattr("title", "in","MyString") | list | count }}
It seems that the string never gets properly evaluated because it always returns 0.
The selectattr without test return the right number of total titles.
I wonder if we could evaluate it with an if statement somehow.

How to return a value when a cell matches something without a certain set of characters? Using regexmatch

I'm using IFS and REGEXMATCH to assign values to each cell. The desired outcome is to return 'Info' if a cell contains 'how' 'where' 'what' 'who' but at the same time does not contain 'buy' 'coupon' 'discount' and 'deals'. If the cell contains 'buy' 'coupon' 'discount' and 'deals', it should return 'Commercial'.
For example: cell containing 'where to go' should be Info,
cell containing 'how to buy' should be 'Commercial' because of the 'buy' keyword
My current formula looks like this:
=IFS(REGEXMATCH(J9,"**how|where|what|who**<>buy<>coupon<>discount<>deals"),"Info",REGEXMATCH(J9,"buy|coupon|discount|deals"),"Commercial")
The problem here is it is returning 'Info' for cells like 'how to buy' when it should return 'Commercial'. It only works for 'who', but not for 'how' 'where' and 'what'.
Any thoughts what could be wrong?
I'm still new to regex, would appreciate it if anyone could help me fix this!
You can use the following formula instead
=IF(AND(REGEXMATCH(J8,"how|where|what|who"),NOT(REGEXMATCH(J8,"buy|coupon|discount|deals"))),"Info","Commercial")
You cannot use <> in a regex. We use the NOT instead.
Functions used:
REGEXMATCH
IF
AND
NOT

How do I get the last element of a slice in a golang template

I can get the size of a slice in a template like this
{{$size := len .Things}}
And I can index a slice in a template like this:
{{index .Things 4}}
But
{{index .Things $size}}
gives an "out of range" error because indexing a slice is zero-based.
Do I have do all the function defining things or is there arithmetic available I can use?
I.e. how do I do this https://stackoverflow.com/a/22535888 but in a golang template.
Defining a minus function: https://stackoverflow.com/a/24838050/10245
There is no arithmetic available by default, but you can add this functionality using a FuncMap.
Here's how to add an "add" function that covers this case and other scenarios:
t := template.Must(template.New("").Funcs(template.FuncMap{
"add": func(a, b int) int { return a + b },
}).Parse(theTemplate)
Use it in the template like this:
{{index .Things (add $size -1)}}
In go templates could be viewed as more "passive" than in other languages or some frameworks. The idea of passive views is that they do not include much logic but get passed all their data in.
So you could pass "LastThing" to your template, which has the last Thing assigned.
This does not answer your question directly but this is just an alternative you could consider.
Define variables out of the loop (range):
{{$i:=""}}
{{$el:=""}}
{{range $i,$el = $.Sorteados}}{{end}}
<span >{{$i}} - {{$el}} </span>
So,use them after the loop.

How to use variables defined with assign in list elements in freemarker

i have a freemarker problem. I have one hash map called nodes and i iterate trough it like this:
<#list hashmap.collection as nodes>
.....some displaying
<#assign nodeName>
${nodes.name}
</#assign>
<#list hashmap2.nodeName.collection as nodes2>
.......some more displaying
And this code is not because freemarker is trying to find nodeName key inside the hashmap2...
Is there a way to do this in freemarker?
Thanks for your answers!
That should be hasmap2[nodeName].collection. What you put after . is always seen as literally the sub-variable name, while inside [] you can give an arbitrary expression as far as it evaluates to a string. Thus, you may don't need that #assign at all, and you could write hashmap2[nodes.name].collection.
Also, instead of <#assign nodeName>${nodes.name}</#assign> you should just write <#assign nodeName = nodes.name>. Again, if you need the assignment at all.
Also since nodes store a single node, your code would be more readable if you call it node.

Django tag: more than 2 parameters

Is there a way to provide more than two parameters to a template tag?
I can do:
val1|my_function:val2
But where does val3 go?
I tried
val1|my_function:val2,val3
Edit: val1, val2, val3 are dynamic, meaning that I can access them like so {{val2}}
The templatetag only accept one argument.
There are a few ways to work around this, but I would consider if it is worth it or if you can perhaps change your logic.
The argument needs to be a digit or string.
So this will NOT work:
val1|func:['a','b']
val1[func:{'a':'b'}
val1|func:('a','b')
This will work:
val1|func:10
val1|func:10.2
val1|func:"10"
What you can do is to send a string that is formatted in a structured way:
val1|func:"['a','b']"
And then you can create the datatype you need to parse this, but this would be a very ugly solution.
A more clean solution would be with multiple tags:
val1|func:'a'|func:'b'
That is a filter, and a filter can only take one or two arguments. Writing custom template filters. If you want a tag that can take multiple arguments, check out Writing custom template tags.
For strings you can do:
val1|my_function:"val2,val3"
and then split:
#register.filter()
def my_funtion(val1, args):
args = [x.strip() for x in args.split(',')]
#do something
return value