Creating a Dynamic Section for a Shopify theme

Dynamic sections are a neat bit of functionality that you can leverage in a Shopify theme. You can place them statically in page templates that you create just as you would static ones. Dynamic sections, however, can be added to your store’s homepage when you customize it through the admin dashboard. When you add one of these sections to your homepage, not only can you configure it as you would a normal static one, you can also decide in which order it appears with respect to other dynamic ones.

The combination of allowing people to configure and arrange these types of sections on the homepage through the admin dashboard is quite powerful and helps reduce dev maintenance costs. There is a rather complete tutorial on them here though the code samples have a lot to them. In this tutorial, I am going to show how to create a bare bones one that can be used as a starting point for developing others.

Step 1 – Setup your dev environment

If you want to follow along with this tutorial. You should setup a Shopify development store. They are free and you can make as many as you like. You should also install Theme Kit. It is a great little command line tool that will let you download and upload your theme to your store. After you install Theme Kit, configure it to use your development store as its development environment. The github.io link I posted includes a very good step by step instructions on how to do that.

Once you have Theme Kit configured, download the starter theme that your development store comes with:

> theme download

You can use an existing theme that you are already working with too but in this post I am just going to keep things simple. If you do use an existing one, then just deploy it to your development store in order to overwrite the starter one.

Turn on watch mode. When Theme Kit is running in that mode, any changes that it sees will automatically get pushed to your store.

> theme watch

In a browser, you should have the Shopify admin dashboard open for your development store as part of the Theme Kit setup. If you already closed it, you should reopen it since you will use it to view the dynamic section you create.

Step 2 – Sanity check your theme

The template for your homepage is in templates/index.liquid. In order for your homepage to display dynamic sections, your template needs to contain this bit of code:

{{ content_for_index }}

Not every theme you are going to work with will have the above code in its index.liquid. This is especially true for themes that you inherit. You can put it anywhere in your template so you are in full control of where your dynamic sections are rendered.

Step 3 – Create a basic dynamic section

Create the following file sections/hello_dynamic_section.liquid:

<h1>Hello Dynamic Section</h1>

{% schema %}
{
    "name": "Hello Dynamic",
    "class": "hello-dynamic",
    "presets": [
        {
            "category": "Hello",
            "name": "Example"
        }
    ]
}
{% endschema %}

That is all you need to have for a very simple dynamic section. In the admin dashboard, select to customize your theme. The editor will show the sections that are being displayed on the page in the left panel; make sure you are viewing the homepage. If this is a brand new store, you might only see an Add section button. Click it and you will see the dynamic sections that are available in your theme. You will see the simple Hello Dynamic one you just created. Click the Add button next to Example in the Hello category and Hello Dynamic Section will be placed on your homepage.

A section in Shopify has two components: a template and a schema. The template is the layout for the section and the schema is the data that can be applied to it. In the above example, the template consists of an h1 tag. The schema contains the name of the section and the CSS class that is applied to the div tag that wraps it when it is added to the homepage. The presets array is what makes the section dynamic. In the example, there is a single object in this array that has a category and a name that are displayed when the section is added via the theme editor. It isn’t entirely useful yet but the next step is to extend this preset object to support configurations.

Step 4 – Enable customizations

So this is where some of the magic starts to happen. The first extension you are going to add are settings. Update hello_dynamic_section.liquid to look like the following:

<h1>{{ section.settings.heading }}</h1>

{% schema %}
{
    "name": "Hello Dynamic",
    "class": "hello-dynamic",
    "settings": [
        {
            "type": "text",
            "id": "heading",
            "label": "Section Heading"
        }
    ],
    "presets": [
        {
            "category": "Hello",
            "name": "Example",
            "settings": {
                "heading": "hello heading"
            }
        }
    ]
}
{% endschema %}

The schema has been extended to have a settings array with a single member object. It has an id set to heading. The member of the presets array has also been extended to have a settings object with the property heading. It value hello heading is essentially its default value.

Refresh the browser that you are using to view the theme editor to see the updates that have been made to your dynamic section. When you now add it to your store’s homepage, you will have an option to set a field called Section Heading. You can add this section multiple times and give each one a different heading value.

Step 5 – Customizable blocks

The settings array allows you to have configurable properties in your section. It is pretty common to need a repeatable block in a section. Fortunately that is supported by Shopify with this other piece of magic. Update hello_dynamic_section.liquid to look like the following:

<h1>{{ section.settings.heading }}</h1>

{% for block in section.blocks %}
    <h2>{{ block.settings.subheading }}</h2>
{% endfor %}

{% schema %}
{
    "name": "Hello Dynamic",
    "class": "hello-dynamic",
    "settings": [
        {
            "type": "text",
            "id": "heading",
            "label": "Section Heading"
        }
    ],
    "blocks": [
        {
            "type": "subheadings",
            "name": "Subheadings",
            "settings": [
                {
                    "id": "subheading",
                    "type": "text",
                    "label": "Subheading"
                }
            ]
        }
    ],
    "presets": [
        {
            "category": "Hello",
            "name": "Example",
            "settings": {
                "heading": "hello heading"
            },
            "blocks": [
                {
                    "type": "subheadings",
                    "settings": {
                        "subheading": "hello subheading"
                    }
                }
            ]
        }
    ]
}
{% endschema %}

Refresh your browser again. Now when you add the dynamic section to the homepage, you should see in addition to the Section Heading field a Subheading one that you can add repeatedly. Each one will cause an h2 tag to be added.

The schema has been extended with a blocks array which has also been added to the presets object. A block object contains settings that behave just like the one previously defined in the schema. It allows you to specify different fields that can be set in the theme editor. I have added a for loop to the template that iterates over these blocks displaying all configured subheadings.

Closing thoughts

The key to creating themes with low maintenance costs is to enable as much customization through the admin dashboard as possible. It enables anyone to make UX updates to the store, not just people that know how to work with templates. That is why I really like Shopify’s dynamic sections so much; it lets anyone add a section to the homepage.

Nocking Point Coffee Club Membership

Well, I decided to treat myself to a little Christmas present this year with a membership to the Nocking Point Coffee Club. In case you didn’t know, Nocking Point is a winery in Walla Walla, Washington, co-founded by Stephen Amell, the actor that plays Oliver Queen, a.k.a. the Green Arrow, in the TV series Arrow.

I’ve really enjoyed Arrow over the years and when I found out that the lead actor had started a winery, in the PNW, with a wine club, I was excited to join but I knew I wouldn’t be able to drink as much as the membership would send me. Last month, however, I discovered that they also have a coffee club; I signed up to get two bags a month. They also throw in one to two lifestyle items with each package. In December’s, it was Christmas socks, a cock screw, and a wine bottle stopper.

For small batch roasted coffee, the price is pretty good so the extra swag items you get are basically free. On top of all that, you get free shipping and 10% off all other purchases you make which means you can order the wine you want when you want which makes a lot more sense for my lifestyle than the wine club. In fact, I’m already planning on ordering something interesting for after my first half marathon of the year in March.

2020 New Year’s Resolution

I started this year by heading to an OrangeTheory class at 5:00 am. My gym has a high quality body scanner which I use once every 3 months so I can get a percent body fat (PBF) reading. When I first started Noom at the beginning of September, my PBF was 20.6. This morning it read 13.5. I have to give Noom credit; it let me drop my weight without sacrificing my muscle mass or sanity.

I’m absolutely ecstatic about the impact that Noom has had on me and for the first time in a long time, I feel like I am properly in control of my body and fitness. So my resolution this year is to stick with Noom and maintain its results that I achieved.

Cookpad – I’ve donated 4 trees

In my post Noom life, I have discovered that a lot of recipes that I used to like to make result in food that just isn’t that good for me. That shouldn’t be a shocker since junkie food is tasty food but it is frustrating. So I’ve started looking for new healthy recipes and updating old favorites. For ones that I really like, and by that I mean my kids like, I’ve been saving them in Cookpad. Right now they have an incentive program where for each recipe you post, they will donate a tree. I only started in December but I have managed to donate 4 trees.

AWS Secrets Manager names are suffixed!

Secrets Manager in AWS is a great little service for stashing credentials and other sensitive materials that an application you develop might want to use. Classic examples are database passwords. There is a gotcha that you need to be careful about when using it though.

When you create a secret, you have to give it a name. Secrets Manager then creates an ARN using this name and adds a suffix consisting of a hyphen plus six random characters. That means if your name also ends with a hyphen and six characters, you are going to break searching for secrets by name. Secrets Manager will think your name is actually the complete ARN. You would think this problem wouldn’t come up that frequently but there are a lot of common six letter terms that people like to use as suffixes. For example:

  • -config
  • -secret
  • -passwd

As a best practice, just don’t use hyphens in secret names.

Noom worked for me: Owning What You Eat

The title sums it up for me; Noom really did work. From September 1st to the beginning of December, I’ve lost over 30 lbs. I’m not going to lie. Dropping that kind of weight comfortably has been life altering for me. My running has improved dramatically. Last week, I had a personal best of 20 minutes for a 3 mile run. As for my clothes, well they don’t just fit better, I’ve had to buy all new jeans and shirts.

I did subscribe to the paid program for 3 months and I still use the free program everyday to track what I eat. I do that for a couple reasons. First, I am not very good at picking healthy options so I will often plan out my meals by looking up different foods. Second, it forces me to own what I choose to eat.

It might sound silly but Noom really forced me to be accountable for what I eat. In fact, the only way Noom will work for anyone is if they fully commit to owning their food decisions and that means tracking everything. Food tracking is all done manually so that means it is on you to not cheat or skip. This level of ownership was hard for me at first because it meant I had to face the hard truth that I was really making some terrible food choices.

Noom is probably going to be apart of my life for a good long time. In fact, I have a hard time imagining my day to day without it. Reflecting and writing about your successes and failures is something they suggest which is another reason for me rebooting my blog. So this definitely won’t be the last time I write about my eating lifestyle.

A fresh start

Well, it has been basically years since I’ve had a meaningful Website and I’ve decided that it is time that I try again. I’ve given up facebook and scrapped my tumblr. In some respects, I think social networking really distracted me away from public writing. It isn’t that I’ve always had a lot to say but something about posting on facebook was just too easy to do but also too hard to do well. So I’m back to blogging and I’ll see where it takes me. I’m intending on sticking to topics that I’m interested in which is primarily tech, food, and running.