I have the following in models:
class Companies(models.Model):
ComName = models.CharField(max_length=255)
ComURL = models.CharField(max_length=1024,null=True)
class Products(models.Model):
PrName = models.CharField(max_length=255)
PrCompany = models.ForeignKey(Companies)
and the following in the template:
{% if products %}
var markers = [
{% for product in products %}{"url":"{{ product.PrCompany.ComURL }}","name":"{{ product.PrName }}"},{% endfor %}
]
{% endif %}
{% endblock %}
but the output i get is:
var markers = [
{"url":"None","name":"Samsung GT-S7350"},{"url":"None","name":"SonyEricsson W395"},{"url":"None","name":"Nokia E75"},
]
I look in the database, and each entry has a value in there, which is not empty.
Why does it say "None" ?
Something is not right in the relation?
you might want to try models.URLField() instead of a CharField for the ComURL.
Related
I'm creating 2 arrays with objects in a Twig template. I know it's best to handle this not in the templates but there is no other way to do it (SaaS platform).
I have 2 loops:
{% set opt_arr = {} %}
{% set var_arr = {} %}
{% for opt in options %}
{% set opt_arr = opt_arr | merge([{'title': opt.title, 'id':opt.id}]) %}
{% endfor %}
{% for var in vars%}
{% set var _arr = var _arr | merge([{'title': var.title, 'price':var.price}]) %}
{% endfor %}
/* result equals something like this =>
[
{"title":"used","id":123},
{"title":"good","id":213},
{"title":"new","id":321}
]
[
{"title":"used","price":199},
{"title":"good","price":299},
{"title":"new","price":399}
]
I want to join them to a single array like so:
{"title":"used","id":123,"price":199},
{"title":"good","id":123,"price":299},
{"title":"new","id":123,"price":399}
I tried several things but either it won't join on title or it removes some data.
{% set all_data = [] %}
{% set all_data = all_data | merge(opt_arr) | merge(var_arr) %}
Or
{% set opt_arr = opt_arr | merge(var_arr ) %}
What am I doing wrong?
I have a JSON payload shown below. I want to loop through and find the one with the highest value for "numUsed". Then, I want to display that number along with the associated "type". For the example below, I'd like to end up with:
"Your favourite benefit was Benefit C, you used it 18 times!"
In its final state, I'd like to disregard the "All Benefits" section.
"Benefits": [
{
"type": "Benefit A",
"dollarSavings": 0,
"timeSavings": 0,
"numUsed": 5
},
{
"type": "Benefit B",
"dollarSavings": 89.85,
"timeSavings": 47700,
"numUsed": 15
},
{
"type": "Benefit C",
"dollarSavings": 99.85,
"timeSavings": 46700,
"numUsed": 18
},
{
"type": "All Benefits",
"dollarSavings": 189.70,
"timeSavings": 94400,
"numUsed": 38
}
SOLVED:
{% assign numbers = data.Benefits | sort: 'numUsed' %}
{% for nums in numbers %}
{% endfor %}
{% assign benefits = data. data.Benefits | sort: 'numUsed' %}
{% for name in benefits %}
{% endfor %}
{% assign numsize = numbers | size %}
{% assign numsizeminustwo = numsize | minus: 2 %}
{% assign secondlasttype = benefits[{{numsizeminustwo}}].type %}
{% assign secondlastnumber = numbers[{{numsizeminustwo}}].numUsed | floor %}
Your favorite benefit this year was {{benefit_name}}, you used it {{secondlastnumber}} times
This is my group array:
array:4 [▼
0 => Fields {#7444 ▼
-id: 1
-name: "ID"
-unique_id: "6ab8c870ed"
-productgroup: PersistentCollection {#7448 ▶}
-type: Type {#7525 ▼
+__isInitialized__: true
-id: 2
-name: "hidden"
-unique_id: "5e1086c862"
-label: "hidden"
…2
}
}
1 => Fields {#7526 ▶}
2 => Fields {#7530 ▶}
3 => Fields {#7534 ▶}
]
This is my column array:
array:3 [▼
0 => "id"
1 => "name"
2 => "type"
]
I know that my id is 1:
$id = "1";
For each value in my column key I want to print out the according value from my group array.
So the result would be:
1
ID
hidden
I try to achieve this with twig
{% for key, value in column %}
{% for k, v in group %}
{{ v.[value] }};
{% endfor %}
{% endfor %}
The error is:
Expected name or number.
NOTE: This Symfony2 / Twig - getting array from dynamic array key is not helping me, because it only explains how to use the value like this v[value] or this v[key] but not in the second level like this v.[value].
You need to use two for-loops in your code to achieve what you want though,
{% for class in classes %}
if (optionValue == {{ class.id }}) {
{% for column in columns %}
var {{ column }} = '{{ attribute(class, column) }}';
{% endfor %}
}
{% endfor %}
demo
I want to assign my variable value based on following condition
var Result;
If(A==2)
Result = A;
else if(A>2)
Result = A+2;
How can I write this in dotLiquid syntax for Logic Apps
The Logic App Liquid map can be something like this:
{
{% assign my_variable = content.a %}
{% if my_variable == 2 %}
"Result" : "{{ my_variable }}"
{% elsif my_variable > 2 %}
"Result" : "{{ my_variable | Plus: 2 }}"
{% endif %}
}
In a twig Bolt CMS template, I am trying to create an array without duplicate values by fetching data from a table Pillars as follows:
{% set arr = [] %}
{% setcontent pillars = 'Pillars' %}
{% for pillar in pillars %}
{% if pillar.title not in arr %}
{% set arr = arr|merge([pillar.title]) %}
{% endif %}
{% endfor %}
{{ dump(arr) }}
The if condition does not seem to work because arr is not an indexed array. I would expect to get the following result (for example) from the {{ dump(arr) }}:
array:3 [▼
0 => "Cash Transfer"
1 => "Human Resource"
2 => "ICT Services"
]
But instead I get the following, with duplicate Human Resource values not removed:
array:5 [▼
0 => Twig_Markup {#4297 ▼
#content: "Cash Transfer"
#charset: "UTF-8"
}
1 => Twig_Markup {#4294 ▼
#content: "Human Resource"
#charset: "UTF-8"
}
2 => Twig_Markup {#4530 ▼
#content: "ICT Services"
#charset: "UTF-8"
}
3 => Twig_Markup {#4527 ▼
#content: "Human Resource"
#charset: "UTF-8"
}
4 => Twig_Markup {#4523 ▼
#content: "Human Resource"
#charset: "UTF-8"
}
]
What could be the problem or what could I be doing wrong?
After trying real extra hard, I managed to solve the problem by concanenating an empty string (two single quotes) before the value to 'force' it to become a string. Here is the solution, it might save someone some hours of trial in future.
{% set arr = [] %}
{% setcontent pillars = 'Pillars' %}
{% for pillar in pillars %}
{% set str = ''~pillar.title %}
{% if str not in arr %}
{% set arr = arr|merge([str]) %}
{% endif %}
{% endfor %}
{{ dump(arr) }}
Alternative solutions are welcome