I have two list and one variable returned from the Flask page to my HTML page.
a = [11121,11122,11123,11124,11130]
b = [11126,11127,11128,11129,11130]
var = 11121
In python code :
if var in a:
print("comes in a")
elif var in b:
print("comes in b")
else:
print("comes in both")
But I figured out that we need to iterate list first in jinja. (may be I'am wrong)
Now I want to check if var either comes in a, or in b or in both in jinja2.
If I understood you correctly, you don't need for loop for it. You can check it with in tester. Like this:
{{ "comes from a" if var in a }}
{{ "comes from b" if var in b }}
if var in a (or if var in b) check the existence of var in a (or b) arrays of values. It's a form of expression so you can freely use it in the expression context. For instance, you can assign result of checking to the variable:
{% set comes_from_a = var in a %}
{% set comes_from_b = var in b %}
Related
https://repl.it/repls/DeadlyRemarkableKeys
const test1 = {["123"]:{stuff: '123'}};
console.log(test1);
var test2 = [];
test2["123"] = {stuff: '123'};
console.log(test2);
I would like to get proper array with objects linked to keys, like in test2 variable, however written in style of test1 ? test1 results with test1[0][123], whereas I need test1[123].
Thank you
Using an array as key of an object looks weird.
Probably this is what you're looking for:
const test1 = {'123': {stuff: '123'}};
console.log(test1['123']); // prints {stuff: '123'};
Here is a way to get unique values. It doesn't work if i want to get unique attribute.
For example:
<a href = '11111'>sometext</a>
<a href = '11121'>sometext2</a>
<a href = '11111'>sometext3</a>
I want to get unique hrefs. Restricted by using xpath 1.0
page_src.xpath( '(//a[not(.=preceding::a)] )')
page_src.xpath( '//a/#href[not(.=preceding::a/#href)]' )
return duplicates.
Is it possible to resolve this nightmare with unique-values absence ?
UPD : it's not a solution like function i wanted, but i wrote python function, which iterates over parent elements and check if adding parent tag filters links to needed count.
Here is my example:
_x_item = (
'//a[starts-with(#href, "%s")'
'and (not(#href="%s"))'
'and (not (starts-with(#href, "%s"))) ]'
%(param1, param1, param2 ))
#rm double links
neededLinks = list(map(lambda vasa: vasa.get('href'), page_src.xpath(_x_item)))
if len(neededLinks)!=len(list(set(neededLinks))):
uniqLength = len(list(set(neededLinks)))
breakFlag = False
for linkk in neededLinks:
if neededLinks.count(linkk)>1:
dupLinks = page_src.xpath('//a[#href="%s"]'%(linkk))
dupLinkParents = list(map(lambda vasa: vasa.getparent(), dupLinks))
for dupParent in dupLinkParents:
tempLinks = page_src.xpath(_x_item.replace('//','//%s/'%(dupParent.tag)))
tempLinks = list(map(lambda vasa: vasa.get('href'), tempLinks))
if len(tempLinks)==len(set(neededLinks)):
breakFlag = True
_x_item = _x_item.replace('//','//%s/'%(dupParent.tag))
break
if breakFlag:
break
This WILL work if duplicate links has different parent, but same #href value.
As a result i will add parent.tag prefix like //div/my_prev_x_item
Plus, using python, i can update result to //div[#key1="val1" and #key2="val2"]/my_prev_x_item , iterating over dupParent.items(). But this is only working if items are not located in same parent object.
In result i need only x_path_expression, so i cant just use list(set(myItems)) .
I want easier solution ( like unique-values() ), if it exists. Plus my solution does not work if link's parent is same.
You can extract all the hrefs and then find the unique ones:
all_hrefs = page_src.xpath('//a/#href')
unique_hrefs = list(set(all_hrefs))
I have an entity reference field inside a (parent-)paragraph, which references multiple child-paragraphs.
Is it possible to access field values of the referencing paragraph in the children's (referenced paragraph's) twig templates?
Actually I'm just trying to count the total referenced items within one of the referenced item's twig templates itself. So I want to count its siblings + 1, if you want.
I'm aware of the fact that I could preprocess this in a module, but I would like to know if this is possible in twig.
In twig:
{% set paragraph_parent = paragraph.getParentEntity() %}
{% set width = paragraph_parent.field_width.0.value %}
<div class="{{ width }}">{{ content.field_images }}</div>
Since there is no response to this question I have to assume that this is not possible in Twig, but wanted to quick share the mentioned workaround via module...
getParentEntity()
is your friend.
A short example for making the referenced element's count available...
/* implements hook_preprocess_paragraph() (paragraph type product_teaser) */
function mymodule_preprocess_paragraph__product_teaser(&$variables) {
/* makes paragraph siblings count (+ 1/self) available to template */
$siblings_total = 1;
$paragraph = $variables['paragraph'];
$parent_paragraph = $paragraph->getParentEntity();
if ( ( isset($parent_paragraph) ) && ( $parent_paragraph->hasField('field_paragraph_reference') ) ) {
$field_content = $parent_paragraph->get('field_paragraph_reference')->getValue();
if ( isset($field_content[0]['target_id']) ) {
$siblings_total = count($field_content);
}
}
$variables['mymodule_theming_sources']['siblings_total'] = $siblings_total;
}
Another method using twig with 2 simple lines. It works fine.
{% set parent = paragraph._referringItem.parent.parent.entity %}
{{ parent.title.value }}
dump(item.url.options.query)
returns: array (size=0)
{% set item = item|merge({'ref': 'xyz'}) %}
returns: no error, item is now set
{% set item.url.options.query = item.url.options.query|merge({'ref': 'xyz'}) %}
errors: Twig_Error_Syntax: Unexpected token 'punctuation'; of value '.'
How do I set the array index item.url.options.query?
You have a deeply nested array so you need to use the merge filter many times:
{% set item = item|merge({
url: item.urls|merge({
options: item.url.options|merge({
query: item.url.options.query|merge({
ref: 'xyz'
})
})
})
}) %}
If the item variable or some of the array items are objects instead of arrays, you might get an error (because the merge filter only works with arrays and Traversable objects), or the objects might get converted into arrays. If that's the case, you might want to take a look at the question DarkBee linked to.
Is there any way i can get handle on value from views Column header??
I have one view and i have to display only selected column values inside a table with respect to the selected column header.
var colNames = new Array(); var Title=sessionScope.dbTitle;
var vwName = sessionScope.vwTitle;
print ("view name"+ vwName);
if ((vwName != null) & (vwName != "Select")) { //var vwName="vwDbConfig";
print (vwName);
var dbpath=#DbLookup("","vwDbConfig",Title,3);
var serName=#DbLookup("","vwDbConfig",Title,2);
var ArchDb:NotesDatabase = session.getDatabase(serName,dbpath);
//var ArchDb:NotesDatabase= database;
var Archvw:NotesView = ArchDb.getView(vwName);
var names:java.util.Vector = Archvw.getColumnNames();
Answer is: two repeats. Outermost repeat will iterate through view entries of binded view data source, variable "row". Inner repeat will iterate through collection of numbers - each represents index of selected column, variable "column". Repeat will render inside <td> tag computed text with binding row.getColumnValues().get(column). Adopt it to proper data type if necessary. Don't forget to add proper tags for header and rows.