When it comes to Shopify Schema, the section settings are functional within the <style> tag, whereas the block settings do not seem to have the same

While working on updating a section with multiple blocks, I encountered an unusual issue. My goal was to incorporate a new block into an existing section. The schema has been set up correctly, and everything is functioning as anticipated. However, the problem arises when trying to utilize block settings within a < style> or {% style %} tag. Surprisingly, these settings function properly in the HTML lines but not within the specified tags. It's worth noting that section settings work within the style tag, whereas block settings do not.

For instance (does not work):

<style>
    .custom_box {
        background: {{block.settings.box_color_from_block}};
    }
</style>

Alternatively (works):

<div class="custom_box" style="background: {{block.settings.box_color_from_block}};"> </div>

Another example (works):

<style>
    .custom_box {
        background: {{section.settings.box_color_from_section}};
    }
</style>

In my setup, there is a color picker embedded within the block I am creating with the identifier "box_color_from_block." When this failed to work, I experimented by introducing a color picker within the section labeled "box_color_from_section," yielding the outcomes described above.

Hunting for solutions online, I stumbled upon a related StackOverflow thread from two years back. A suggestion was made to relocate the input to the section instead of the block, which did indeed solve the issue but isn't feasible in my case. Specifically, Example 1 must be functional for my requirements.

The scenario entails generating a < div> using a {% for %} loop for each product within a collection. Consequently, the approach in Example 2 is inadequate as every < div> necessitates a distinct background color. With approximately 5/6 color pickers (variable based on the collection), integrating them into the section would overcrowd it further. Hence, the preference is to include the color pickers within the block settings, which are presently underutilized.

I extend my gratitude in advance for taking the time to read through this query, and I would greatly appreciate any insights provided. My primary challenge lies in deciphering why this situation is occurring and how to address it effectively.

Answer №1

Based on the input you've given, example 2 works because block.settings.box_color_from_block is used within a div inside a liquid for loop.

Within your for loop, you iterate over section.blocks to access data for each block :

{% for block in section.blocks %}
  <div class="custom_box" style="background: {{block.settings.box_color_from_block}};"></div>
{% endfor %}

In contrast, in your Example 1, you do not have a liquid for loop in your CSS, hence {{block}} remains undefined.


Solution :

You should utilize two separate for loops, one for HTML and one for CSS.

In the HTML loop, generate unique divs with IDs for each block using {{block.id}}.

In the second for loop within CSS, customize the divs based on the div ID and block settings.

<style>
{% for block in section.blocks %}
  #custom_box_{{block.id}}{
    background: {{block.settings.box_color_from_block}}
  }
{% endfor %}
</style>

{% for block in section.blocks %}
 <div id="custom_box_{{block.id}}" class="custom_box"></div>
{% endfor %}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What exactly do discrete animations entail?

In the MDN animation documentation, it mentions a type of animation known as "discrete". Can you explain what this term signifies? ...

ever-evolving background-image with dynamic CSS styling

Being new to both PHP and Javascript, please excuse any mistakes in my explanation. I have information stored in a PHP array that I bring to my index page using the function below (located in a separate file called articles.php that is included in my index ...

Tips for breaking out of an infinite loop while loading HTML containing a carousel into a blank <div> within another HTML file using jQuery's $(document).ready() function

Currently, I am studying HTML, CSS, Bootstrap 4, jQuery3, and JavaScript (ES6+), and to enhance my skills, I have devised an exercise for practice and consolidation. I have created 4 HTML files, all featuring responsive design based on Bootstrap 4: inde ...

Using Javascript/JQuery to apply pixel values in CSS styling

Recently, I've come across a discrepancy between two different ways of accessing CSS properties in jQuery: <foo>.css('marginTop') (which I always assumed was the standard notation) and <foo>.css('margin-top') (which ...

Shiny Chrome, Flexible Flexbox, and plump div elements

Looking to enhance my understanding of flexbox. Encountering a display issue in Chrome where the right column is too wide and the left column is too skinny, while it displays correctly in Firefox. Any suggestions on how to fix this for Chrome? .child ...

Images in CSS not copied to the output directory when using Webpack

Using Webpack to bundle various javascript and css files on a website includes bundling bootstrap.css and chosen.css as part of the bundles. To achieve this, there is a main.js file serving as an entry point for importing all necessary files. The process i ...

Ways to increase the size of a div to match the maximum height of its parent container

My current project involves using an angular dialog layout where the API to open and manage the dialog comes from a separate library. The dialog's parent container has a max-height attribute, meaning the dialog's height is determined by its conte ...

Issues with WordPress's multiple menu functionality not functioning as expected

I've been working on setting up multiple menus in WordPress, and I've run into a little issue. I managed to create the menus, assign them to their respective locations, and save them successfully. However, when I try to call the footer menu, it e ...

Display Text Aligned in the Middle Beside Website's Logo

Newbie to HTML and CSS here! I'm attempting to achieve a design similar to this: Below is my code snippet: <div id="topbar"> <div class="image"> <img src="images/ghwlogo.png"> </div> <div class="text ...

Tips on adjusting the height of divs in a simple layout

I'm trying to make two divs fill the page with a ratio of 70% and 30%. However, I don't want to set the size of the "html" or "body" elements. How can I achieve this without affecting the height of the text within the divs? Currently, it only dis ...

Cannot see the box-shadow with :before and z-index in CSS

My main objective is to prevent the box-shadow from overlapping with adjacent elements by utilizing the :before pseudo-element and adjusting the z-index. However, I am facing an issue where the shadow appears beneath the container of the list item that is ...

The IntroJs step is only partially visible on the screen

Currently, I am incorporating introJS into my application and encountering an issue where one of the steps is only partially visible on the screen. Despite trying various position settings such as auto, left, right, etc., this particular item consistentl ...

How can Django implement dynamic CSS for a single item that changes its background dynamically?

I am looking to create a single CSS style that can dynamically change its background based on a Django variable. This becomes more challenging when dealing with a:hover. The initial static style.css code appears as follows: .titles-button h2 a { marg ...

Combine all the HTML, JavaScript, and CSS files into a single index.html file

So here's the situation - I am utilizing a C# console application to convert four XML files into an HTML document. This particular document will be circulated among a specific group of individuals and is not intended to be hosted or used as a web app; ...

Having difficulty navigating to a different page in Angular 4

I'm currently attempting to transition from a home page (localhost.com) to another page (localhost.com/listing). Although the app compiles correctly, I encounter an issue where nothing changes when I try to navigate to the new page. My approach has m ...

Navigation arrows for sliding`

Is there a way to add custom right/left arrows to the Ionic slider component? Demo: Check it out on Stackblitz Note: Make sure to refer to the home.html page for more details. https://i.sstatic.net/jQ62l.png .html <ion-slides [pager]="true" [slide ...

Position on the chart in the Highcharts

I have developed 2 highcharts in the code below: <div> <p id="container"> </p> <p id="cont"> </p> </div> I specified the width and height as follows: $('#cont').highcharts({ char ...

Are Half of the <a> Links Invisible due to a Possible Issue with Position:Fixed?

There seems to be an issue with some of the links in my navigation bar not working properly. It appears that they are being covered by an iframe element, preventing them from changing color when clicked. Although the iframe element is visibly below the na ...

Issue with 'backface-visibility' CSS3 property not functioning on any versions of Internet Explorer

I'm looking to implement a specific animation in Internet Explorer. The goal is to rotate an image like a coin, displaying a different image on the other side. I suspect that the issue lies with Backface-visibility in IE, but I'm not entirely sur ...

Having difficulty positioning the dropdown above the other elements in the body

Below, you'll notice that the dropdown menu isn't positioned correctly over other body elements like the timer. I'm utilizing bootstrap for the dropdown and redcountdown js for the timer. Here is the HTML: <div class="col-md-6 m-t-1 ...