Solved

Save color in parameter for reuse

  • 30 March 2021
  • 6 replies
  • 54 views

Userlevel 1

Is there a possibility in Looker to save colors or other stuff in a parameter or variable, to easily reuse it.

Color is just an example, it would be nice to have this possibility with other variables as well.

In our LookML code we have this kind of code:

  dimension: category {
type: string
sql: ${TABLE}.Category ;;
html:
{% if value == 'Severe Risk' %}

<p style="color: white; background-color:#ea5667; font-size:100%; text-align:center; border:3px; border-style:solid; border-color:#FFFFFF">{{ rendered_value }}</p>

{% elsif value == 'Risk Country' %}

<p style="color: white; background-color:#ed6e54; font-size:100%; text-align:center; border:3px; border-style:solid; border-color:#FFFFFF">{{ rendered_value }}</p>

{% elsif value == 'Low Risk Country' %}

<p style="color: white; background-color:#a0c969; font-size:100%; text-align:center; border:3px; border-style:solid; border-color:#FFFFFF">{{ rendered_value }}</p>

{% else %}

<p style="color: black; background-color:#e8e9ec; font-size:100%; text-align:center; border:3px; border-style:solid; border-color:#FFFFFF">{{ rendered_value }}</p>

{% endif %}

;;
}

The coloring used in this HTML is repeated at several places. 

It would be nice if this could be stored in a parameter, so when we update it to another color, it gets updated in all places where it’s used.

icon

Best answer by David_P 31 March 2021, 09:17

Hi @mathias.luyten,

I’d suggest making use of constants in your manifest file.

constant: negative_format {
value: "{% if value < 0 %}<p style=\"color:red; \">({{rendered_value}}){% else %} {{rendered_value}} {% endif %}"
}
measure: total_amount {
type: sum
value_format_name: usd
sql: ${amount} ;;
html: @{negative_format} ;;
}

Here’s the documentation

Hope it helps!

View original

6 replies

Userlevel 6
Badge

But the strings that you compare to value would be different in each place you want it to reuse, wouldn’t they?

Userlevel 1

Yess, but not in all cases. For example we have a lot of measures which are all ratings from 0 to 100, which also have this kind of HTML formatting added to them.

For example:
 

 

Besides that we also have ratings with a different HTML format, for which it would also be nice to store this kind of HTML in a single location and refer to it.

​​​​

When we would change this logic a bit, it would be nice if we only had to do his in one place, instead of applying the change at every single dimension that uses this kind of HTML code

Userlevel 2
Badge

Hi @mathias.luyten,

I’d suggest making use of constants in your manifest file.

constant: negative_format {
value: "{% if value < 0 %}<p style=\"color:red; \">({{rendered_value}}){% else %} {{rendered_value}} {% endif %}"
}
measure: total_amount {
type: sum
value_format_name: usd
sql: ${amount} ;;
html: @{negative_format} ;;
}

Here’s the documentation

Hope it helps!

Userlevel 6
Badge

@David_P  will the “value” in the string/liquid in the constant always refer to the context of the measure/dimension where constant is used?

Userlevel 2
Badge

yes, it would work fine for recognizing the context of each field. You can also reference another specific field for these liquid variables by adding the view name and field name, but this would cover a different use case.

  • {{ view_name.field_name._value }}
  • {{ view_name.field_name._rendered_value }}
  • {{ view_name.field_name._model._name }}
Userlevel 1

Creating a manifest file and storing it as a constant works out great as far as I tested, thanks! :D 

Reply