skip to Main Content

I’m a newbie to coding and learning hands on as I go. I am working on my company’s website and trying to set up the collection pages a certain way to not show text over the image.

This is the code I am trying to edit in Shopify:

{
“type”: “text”,
“id”: “custom_name”,
“label”: “Custom Name”,
“info”: “If “Custom Name” is empty, title = default custom name”
},

I want to change it so that if the field “Custom Name” is empty, then the title will also be blank or not display anything at all over the image.

I’ve been tinkering with different commands etc but I am a novice and could use some help.

Thanks 🙂

2

Answers


  1. That’s how the variable should look like:

    var array = { "type": "text", "id": "custom_name", "label": "  Custom Name ", "info": "If "Custom Name" is empty, title = default custom name" },
    { "type": "text", "id": "custom_name", "label": " ", "info": "If "Custom Name" is empty, title = default custom name" },
    { "type": "text", "id": "custom_name", "label": "", "info": "If "Custom Name" is empty, title = default custom name" };
    

    Now you can do a for loop for each one of those but for the example, it’s going to be for the second one:

    if ( array[1].custom_name != "" || array[1].custom_name != null ) {
        //Do whatever you want, show the text
    } else {
        //Don't show the text
    }
    

    As a small tip, when showing the text add a .trim() so if there is any whitespace, it will be removed!

    Login or Signup to reply.
  2. It’s not clear if you like to set the default name to the JSON object or to the output, since the first one is not possible.

    You can set a default option to the JSON object using "default": "something", but once that is removed/deleted it will be blank and the default value won’t return.

    If you like for the output to fallback to a default one, than it’ simple as this:

    {{ settings.custom_name | default: 'Something Default' }} ( if this is a JSON
    settings_schema.json file )

    {{ section.settings.custom_name | default: 'Something Default' }} ( if it’s a section )

    In case it’s a block in a section, then:

    {%- for block in section.blocks -%}
      {{ block.settings.custom_name | default: 'Something Default' }}
    {%- endfor -%}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search