Drupal Twig Integer conversion not working - drupal

I an trying to loop x times from a integer field value.
But no matter how i format the twig-filter the field value 6 is converted to 1.
The string '6' is converted to int 1.
I have module twig tools enabled.
{{content.body|raw|integer}} // always 1
{% for i in range(1, content.body|raw|integer) %}
{{ i }}
<div class="stars"><i class="fa fa-star"></div>
{% endfor %}

Because the body is a textarea and it's raw version may include HTML, it's probably not returning a simple string value of "6", but likely something like:
<p>6</p>
The integer twig filter uses PHP's intval function, which will return 1 on an object or non-empty array.
I would suggest using an Integer field to store your number instead of the body field.

Related

How to change filter in date parameter to range in Looker

I am trying to see if there is a way to have the filter on a date parameter be a date range instead of a single date value. Does anyone have any creative ideas on how to do this.
You probably want to replace your parameter with a templated filter. See the docs for more info, but in short:
In situations where you want to offer users more flexible input (such as with various kinds of date ranges or string searches), try to use templated filters when possible.
Your code will look something like this:
view: customer_facts {
derived_table: {
sql:
SELECT
customer_id,
SUM(sale_price) AS lifetime_spend
FROM
order
WHERE
{% condition order_date %} order.dt {% endcondition %}
GROUP BY 1
;;
}
dimension: order_date {
type: date
sql: ${TABLE}.dt ;;
}

Error : The "defined" test only works with simple variables

I'm trying to display prices in a specific format with twig/symfony.
I would like to consider the null case (there is no price) but I get this Symfony error:
The "defined" test only works with simple variables.
Here is my code in twig :
<td>{{ _self.formatRemuneration(remunerationValues.variable_monthly_remuneration|number_format(2, ',', ' ') ?? null) }}</td>
Without this ?? null part it only works if a price exists in the database. I don't take the null case into account.
I also tried to move the filter (|number_format(2, ',', ' ')) inside the macro but I get errors or 0.00 values instead of nothing.
{% macro formatRemuneration(remuneration) %}
{{ remuneration is not null ? 'profile.remuneration.formated'|trans({'remuneration': remuneration}, 'company_position') : '' }}
{% endmacro %}
Can you help me write this condition?
To start, I'm not sure why you are doing something called formatRemuneration and trying to apply the the filter number_format both at the same time.
If I were you I would move the filter inside your macro, in which you then could test for a null value when the price isn't set and return whatever you want, e.g. Not applicable
To fix the error you would only need to evaluate the variable, not the whole line, see below. This is because the filter number_format return an object of the instance Twig\Markup
<td>{{ _self.formatRemuneration(remunerationValues.variable_monthly_remuneration ?? null) }}</td>
If you can't move the filter number_format for whatever reason, you would need to change the snippet to the following:
<td>{{ _self.formatRemuneration((remunerationValues.variable_monthly_remuneration ?? null)|number_format(2, ',', ' ')) }}</td>
Keep in mind null will get converted to 0.00

Flask: Iterating through a dictionary whose vlaues are lists of tuples

I have a dict whose values are lists of tuples. I want to build a table for every key.
mydict = {'Western Division': [(0, 1, 'Oakland'), (0, 2, 'San Jose')], 'Eastern Division': [(1, 1, 'Boston'), (1, 2, 'Buffalo')]}
My template is:
{% for key, value in mydict %}
<table>
<tr>
<th> {{ key }} </th>
</tr>
{% for team in value %}
<tr>
<td>{{ team[2] }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
This gives me a ValueError: too many values to unpack (expected 2)
I tried changing the first for-loop to for key, value, team, thinking that I want to call each tuple in each list in each key, but got the same error (expected 3).
Lastly, I tried for key, value in mydict.items and got TypeError: 'builtin_function-or_method' object is not iterable.
It's definitely possible that I made a mistake further upstream in creating the dict, but I suspect I am just not building my template correctly.
#mechanical_meat provided the answer in their comment. items needs parens in jinja2.

Doctrine subtract on-the-fly two columns as a third

Expressed in MySQL, what I wish:
SELECT article.sellprice - article.cost AS margin
FROM article
My attempt in DQL (note that margin does not exist in my entity):
SELECT a, a.sellprice - a.cost AS a.margin
FROM ArticleBundle:Article a"
Results in an exception [Syntax Error] line 0, col 47: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '.'. Attempt to get it outside of the entity:
SELECT a, a.sellprice - a.cost AS margin
FROM ArticleBundle:Article a"
And some variations of that code; that messes up with my result, causing a twig error Key "nom" for array with keys "0, margin" does not exist.. In fact, a.nom exists; when reverting to:
SELECT a
FROM ArticleBundle:Article a
No error at all. Any clues to get my calculated on-the-fly result?
Because that value must bubble up to the twig, according to the comment of Yoshi (many thanks by the way), I ended up with the second try:
SELECT a, a.sellprice - a.cost AS margin
FROM ArticleBundle:Article a"
Tuning the twig to parse that mixed content:
{% for mixed_content in pagination %}
<tr>
{% set article = mixed_content|first %}
{% set margin = mixed_content.margin %}
<td>{{ article.nom }}</td>
<td>{{ article.margin}}</td>
Hoping that may help anyone.

How to retrieve a value of an attribute from the XML stored across multiple rows using Oracle-Xquery?

We are using Oracle 11g database with XMLDB installation. We are having table with XMLType columns. The structure of the XML will be same for all the rows in a table. The table will have other fiedls also.
Now I want to retrieve only the values of the particular node's attribute values from all the rows as a string with some other relational fields. The table columns retrieved can be like TemplateId, TemplateVid,TemplatepartId.
The structure of the XML can be as follows:
<Template ID=1000 VID=1>
<TemplateParts>
<Template ID="4000" VID="1"/>
<Template ID="4001" VID="1"/>
</TemplateParts>
</Template>
So the table will have data for Template with TemplateId,Vid and TemplateXML. The TemplateXML field is an XMLType field. Now I want to retrieve all the TemplateId,Vid and its refereced template partIds as an XML table. The output should be as follows:
TemplateId - TemplateVid - TemplatePartId - TemplatepartVid
1000 1 4000 1
1000 1 4001 1
So anybody comes up with a correct Xquery for the above requirement.
Your requirement is not clear but to start you off and hopefully get you some additional comment from the wider XQuery community on StackOverflow here is a quick example. Hope this helps :)
xquery version "1.0";
<html>
<head>
<title>Sample</title>
</head>
<body>
<div align="center">
<table>
<tr><th>TemplateId</th><th>TemplateVid</th><th>TemplatePartId</th><th>TemplatepartVid</th></tr>
{
let $sample as element()* := (<root><Template ID="1000" VID="1"><TemplateParts><Template ID="4000" VID="1" /><Template ID="4001" VID="1" /></TemplateParts></Template></root>)
for $e in $sample/Template
return
for $tp in $e/TemplateParts/Template
return
(<tr><td>{data($e/#ID)}</td><td>{data($e/#VID)}</td><td>{data($tp/#ID)}</td><td>{data($tp/#VID)}</td></tr>)
}
</table>
</div>
</body>
</html>
As I have mentioned in my earlier comment, I have managed to get the IDs and VIDs which is stored under the Node /Template/TemplateParts/Template from xmltype column of all the rows. The query is as follows:
select distinct x.value as TemplatePartId,Y.Value as Vid from
TempVersion t ,
xmltable('/Template/TemplateParts/Template' passing t.CONTENT columns value varchar2(10) path '#ID' ) x,
xmltable('/Template/TemplateParts/Template' passing t.CONTENT columns value varchar2(10) path '#VID' ) y
order by TemplatePartId;
If sombody know better format,please post your sample query. I need as a normal query as the above format is not supported by my ORM tool. If you look at the above query you can notice that the XMLTable expression is to be placed after the From clause. This gives trouble when I try to form this query through my LLBLGen ORM tool.

Resources