Looker conditional formatting using another field - looker

I'm trying to create a conditional format based on another field.
Concretely, if the score_label_column == "Green" then it give a green background to the score_value_column, but I'm not sure how to reference another column in the html.
Here is what I have thus far:
view: name_of_view {
derived_table: {
sql: SELECT * FROM MY_TABLE
;;
}
measure: count {
type: count
drill_fields: [detail*]
}
dimension: total_score_colored_looker{
sql: ${TABLE}."score_value_column" ;;
html:
{% if score_label_column._value == "Green"%}
[color green]
{% elsif score_label_column._value == "Yellow" %}
[color yellow]
{% else %}
[color red]
{% endif %} ;;
}
I tried doing it the way the documents reference which is by wrapping it around double brackets {{ }}, but when it rendered I got the following error
Liquid Syntax Error: Liquid parse exception: extraneous input '{{' (around the text "% if {{ CH") Note: you should use "{% if var %}" rather than "{% if {{ var }} %}" to reference variables.
I also tried prefixing it with the view name name_of_view.score_label_column._value with and without double brackets to no avail.
Any help appreciated.

You are almost right with the liquid syntax, it is also quite sensitive so make sure that there is spacing between the brackets:
{% if value%} - would not render correctly
{% if value %} - would render
To come back to your issue, you are referring to score_label_column which is not a dimension nor a measure that exist. If we want to refer to the current dimension, which is here total_score_colored_looker you can write as follow:
{% if total_score_colored_looker._value == "Green" %}
[color green]
{% elsif total_score_colored_looker._value == "Yellow" %}
[color yellow]
{% else %}
[color red]
{% endif %} ;;
or
{% if value == "Green" %}
[color green]
{% elsif value == "Yellow" %}
[color yellow]
{% else %}
[color red]
{% endif %} ;;

Related

Shopify / Liquid: Separate a group of tags, then placing them on a select component

I have a large set of tags, and I want to be able to sepparate them. For example, I have the Green, Blue, Red, Black, Colorless and White tags (as well as some other tags that aren't colors, so I won't put them here) and I want to place them in an array.
I tried with something like this:
{% assign colors = "" | split: '' %}
{% for tag in collection.all_tags %}
{% if tag == 'Black' or tag == 'Blue' or tag == 'Colorless' or tag == 'Red' or tag == 'White' %}
{% assign comma = "," %}
{% assign color = tag | split: '_' %}
{% assign colors = colors | concat: color %}
{% assign colors = colors | concat: comma %}
{% endif %}
{% endfor %}
However, that returns me BlackBlueGreenRedColorlessWhite, without any spaces whatsoever. Also, is there a way I can place those selected tags in a <select> component so I can filter my products by the selected tag? Like in the example provided in this page but using a <select> instead of a <ul>.
first, categorizes your tag by having a label;
i.e: color-blue, color-red, color-green, etc.
why? You will not need to add more if-else statement;
then, with code below, you will get an array of color;
{% assign color = "" %}
{% for tag in collections.all_tags %}
{% if tag contains 'color-' %}
{% assign trimmed = tag | split: "-" | last %}
{% assign color = color | append: trimmed | append: ", " %}
{% endif %}
{% endfor %}
{% assign color = color | split: ", " %}
<select name="color" id="color">
{% for x in color %}
<option value="{{x}}">{{x}}</option>
{% endfor %}
</select>

Twig3: How to migrate "for item in items if item.foo == 'bar'" with loop bariable

I use the following Twig 2 code:
{% for item in items if item.foo == 'bar' %}
<span class="{% if loop.index % 2 %}special-class{% endif %}">
{{ item.value }}
</span>
{% else %}
Nothing found
{% endfor %}
In the twig docs: https://twig.symfony.com/doc/2.x/deprecated.html
Adding an if condition on a for tag is deprecated in Twig 2.10. Use a filter filter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop)
I wonder how I migrate my Twig 2 code to Twig 3. As you see I use the loop variable and else in the for loop. I know that I can use a new parameter and increase it myself... but it that really the intention? How do I rewrite this code using filter?
You have two options to solve this
Place the if-tag inside the loop
{% set i = 0 %}
{% for item in items %}
{% if item.foo == 'foo' %}
<span class="{% if i % 2 %}special-class{% endif %}">
{{ item.value }}
</span>
{% set i = i + 1 %}
{% endif %}
{% else %}
Nothing found
{% endfor %}
With this solution you can't "rely" on the internal loop variable, as the counter keeps going up whether or not the condition was met
Use the filter - filter
{% for item in items | filter(item => item.foo == 'foo') %}
<span class="{% if loop.index % 2 %}special-class{% endif %}">
{{ item.value }}
</span>
{% else %}
Nothing found
{% endfor %}
updated demo
Using the filter filter your code would look something like this (see also https://twigfiddle.com/9hiayc and https://twigfiddle.com/9hiayc/2):
{% for item in items|filter(i => i.foo == 'bar') %}
<span class="{% if loop.index % 2 %}special-class{% endif %}">
{{ item.value }}
</span>
{% else %}
Nothing found
{% endfor %}

Float Comparison in twig

I'm trying to compare two floats in twig but I don't have the good result :
I have two 'equal' floats : float1 and float2
{% if float1 == float2 %}
<span>Floats are equal</span>
{% else %}
<span>Floats are different</span>
{% endif %}
{{ float1 == float2 }}
Display :
<span>Floats are different</span>
1
How can I compare two floats in Twig ?
I don't understand why the result of the comparison is true but the result of the if statement is false
Here is a working example that I tried on Twigfiddle:
{% set float1 = 1.0123456789012 %}
{% set float2 = 1.0123456789011 %}
{% if float1 == float2 %}
<span>Floats are equal</span>
{% else %}
<span>Floats are different</span>
{% endif %}
The limit (at least on Twigfiddle) is 13 decimal places. So 1.0123456789012 works; but if you increase to 1.01234567890123, the comparison won't work.
Here is the Twigfiddle for you to see it working: https://twigfiddle.com/r3hi48
Is your code any different than that? In other words in the above I use set to declare the two float variables. Have you also tried {{ dump(float1) }} to see what the value prints out as?

How to use capture cycle odd even and target specific collection item in Jekyll

Hi I'm working on a Jekyll site. I am using a capture cycle for odd and even displays of the collection. I would also like to specifically target which collections are shown on this page.
Here's the code so far:
{% assign sorted = (site.catalog | sort: 'date') | reverse %}
{% for project in sorted limit: 5 reversed %}
{% capture thecycle %}{% cycle 'odd', 'even' %}{% endcapture %}
{% if thecycle == 'odd' %}
{% if project.featured == "1" || project.featured == "3" || project.featured == "5" %}
<div>{{ project.title }} {{ project.subtitle }}</div>
{% endif %}
{% if thecycle == 'even' %}
<div>{{ project.title }} {{ project.subtitle }}</div>
{% endif %}
{% endfor %}
So I would like five items to be shown on this page in total. For the odd cycles, I would like those items to be (1,3,5) to display a certain format, and for the even cycles, I would like those items (2,4) to display a certain format.
The approach I started using would show this variable in the front matter of the collection item:
featured: "1"
Thanks in advance for any help

Trying to locate some objects in a space

I'm trying to locate some objects in a field using Twig given some conditions (yes, I know it s simple, but I'm having too much troubles to have results). It's all right, but I'm having troubles giving some distance between objects.
This is my code:
{% for key, positions in teams %}
{% for key1, position in positions %}
{% for key2, player in position %}
{% set x = 100 %}
{% set counter = player.positionId|length + 1%}
{% set d = x/counter %}
{% if player.positionId == 1 %}
{% set top = 0.4 %}
{% set xpos = 42 %} //That value is correct because it's a goalkeeper
{% elseif player.positionId == 2%}
{% set top = 9.5 %}
{% set xpos = %}
{% elseif player.positionId == 3%}
{% set top = 20.5 %}
{% set xpos = %}
{% elseif player.positionId == 4%}
{% set top = 32.5 %}
{% set xpos = %}
{% endif %}
In summary, I have some players that are located on different y-coodinates given their positions, but also I need each one to have some distance between them if they have the same position, being xpos its position in the x-axis.
If you don't know Twig, you can help me using another lannguage too. Thanks in advance
Ah, and well, I want to know how to separate the teams (because it renders all players at this moment)
There are multiple ways to pass values to twig, one way is to do this through routing,
# app/config/routing.yml
blog_show:
path: /blog/{YOUR_VARIABLE_OR_VALUE_HERE}
defaults: { _controller: AcmeBlogBundle:Blog:show }
see this reference: http://symfony.com/doc/current/book/routing.html
you can also set global variables: http://symfony.com/doc/current/cookbook/templating/global_variables.html
or create an array in controller using repository or on your on, then send this to the view through the params
ie)
$repository2 = $this->getDoctrine()->getRepository('YOUR_BUNDLE_NAME_Bundle:YOUR_REPOSITORY');
$YOUR_ARRAY_NAME = $repository2->REPOSITORY_METHOD_OR_GENERIC_METHOD($IDENTIFIER);
$params = array('form' => $form->createView(), 'YOUR_ARRAY_NAME' => $YOUR_ARRAY_NAME');
return $this->render('SystemCheckoutBundle:Checkout:checkout.html.twig', $params);
these params can then be accessed in the view via:
{% for YOUR_ARRAY_NAME in YOUR_ARRAY_NAME %}
Client ID: {{ YOUR_ARRAY_NAME.contactId }}<br>
First Name: {{ YOUR_ARRAY_NAME.firstName }}<br>
Last Name:{{ YOUR_ARRAY_NAME.lastName }}<br>
Email: {{ YOUR_ARRAY_NAME.email }}<br>
{% endfor %}

Resources