Save color in parameter for reuse

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.

Solved Solved
0 6 468
1 ACCEPTED SOLUTION

David_P1
Participant V

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 solution in original post

6 REPLIES 6

Dawid
Participant V

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

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:
 

f2b1a130-90bc-477e-bc2a-f7a6d3ab6986.png

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.

​​​​

692b9474-0493-4744-9308-636d3766fa0d.png

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

David_P1
Participant V

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!

Dawid
Participant V

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

David_P1
Participant V

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 }}

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

Top Labels in this Space
Top Solution Authors