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
Related
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 ;;
}
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.
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.
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.
I am creating a report from a query where a field ABC is displayed as CAT if yes and as MOUSE if no. But unfortunately, when there are instances where the table inside the query does not contain field ABC, the report generates a error pop-up. Is there any way to by-pass this and run the report with other fields excluding the missing field?
I heard that IIF Exist function could help, but I am really blank here. I wrote the access query like below:
Iif (fieldExists(iif([ABC]=5, 'CAT', 'MOUSE'),iif([ABC]=5, 'CAT', 'MOUSE'), '')) AS TOMnJERRY
This function is maybe the shortest one to test if a field exists in Access:
Function FieldExists(ByVal Table As String, ByVal Field As String) As Boolean
On Error Resume Next
FieldExists = (DCount(Field, Table) >= 0)
End Function
How it works:
If the field exists, the expression (DCount(Field, Table) >= 0) is obviously always true, because DCount never returns a negative value. If the field doesn't exist, an error will occur and the program will jump to the next line without setting the return variable FieldExist, so this one will keep at default and this is false.
So the solution for your problem should look like this:
Iif (FieldExists('YourTable','ABC'), iif([ABC]=5, 'CAT', 'MOUSE'), '')