CMS Development

paladinodeimari
Contributor | Partner
Contributor | Partner

Cannot access theme.header.background.color

SOLVE

This is a piece of my fields.json:

{
    "label": "Website header",
    "name": "header",
    "type": "group",
    "children": [
      {
        "label": "Background",
        "name": "background",
        "type": "group",
        "children": [
          {
            "label": "Color",
            "name": "color",
            "type": "color",
            "required" : true
          }
        ]
      }
    ]
  }

 

I'm trying to get the value of backround color but I get a wrong content.

 

Inside my css:

.header {
  background-color: {{ theme.header.background.color }}
}

 

In Chrome inspector I get:

.header {
    background-color: {color=null,opacity=null,show_opacity=null,css=};
}

 

What's wrong?

0 Upvotes
1 Accepted solution
Oezcan
Solution
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Cannot access theme.header.background.color

SOLVE

What I just noticed is that if the color_field is packed in a group, the group must also be addressed. So not

{% set bg_color = theme.color_field.color %}

but

{% set bg_color = theme.background.color_field.color %}

 

Oezcan_1-1658397872521.png

 

 

Oezcan Eser Signature

View solution in original post

5 Replies 5
Oezcan
Solution
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Cannot access theme.header.background.color

SOLVE

What I just noticed is that if the color_field is packed in a group, the group must also be addressed. So not

{% set bg_color = theme.color_field.color %}

but

{% set bg_color = theme.background.color_field.color %}

 

Oezcan_1-1658397872521.png

 

 

Oezcan Eser Signature
Oezcan
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Cannot access theme.header.background.color

SOLVE

Ah,

then you need something like this:

{%- set bg_color = 'rgba(' + theme.color_field.color|convert_rgb + ", " + theme.color_field.opacity/100 + ')' %}

 

 

Oezcan Eser Signature
paladinodeimari
Contributor | Partner
Contributor | Partner

Cannot access theme.header.background.color

SOLVE

Done but still doesn't work.

Cannot understand why!

 

paladinodeimari_0-1658394184239.png

paladinodeimari_1-1658394223429.png

 

The point is that I'm using color for other elements and in that case it works.

paladinodeimari_2-1658394267805.png

 

0 Upvotes
Oezcan
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Cannot access theme.header.background.color

SOLVE

Hello  @paladinodeimari ,

 

The color value is missing in your object:

 

Here is a simple example:

{
"name": "color_field",
"label": "Color field",
"required": false,
"locked": false,
"inline_help_text": "",
"help_text": "",
"type": "color",
"default": {
  "color": "#ffffff",
  "opacity": 100 }
}

 

This is how you get the color and can "shorten" it

{% set bg_color = theme.color_field.color %}

 

And this is how you can then assign the color to it.

header {
    background-color: {{ bg_color }}
}

 

That's all 🙂

 

I hope I could help you.

 

Best regards

Özcan

Oezcan Eser Signature
0 Upvotes
paladinodeimari
Contributor | Partner
Contributor | Partner

Cannot access theme.header.background.color

SOLVE

Thank You Özcan.

I try what you suggested but I've still problem.

 

I put in fields.json the cut and paste of your code:

{
        "label": "Background",
        "name": "background",
        "type": "group",
        "children": [
          { "name": "color_field", "label": "Color field", "required": false, "locked": false, "inline_help_text": "", "help_text": "", "type": "color", "default": { "color": "#ffffff", "opacity": 100 } }
        ]
      }

 

Then in my css:

{% set bg_color = theme.color_field.color %}

.header { background-color: {{ bg_color }} }

 

But now this is what I get in inspector:

.header {
    background-color: rgba(#null,1);
}

 

And if I open the css within the browser it doesn't contain the code .header!

I have published all the modified files.

0 Upvotes