Download
We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

Access label from config.json select in Publii template

  • #3959
    Avatar photo[anonymous]

    I’m developing a sub-theme with an address state/province selector in the config.json:

    {
        "name": "state",
        "label": "State",
        "group": "Contact",
        "type": "select",
        "options": [
            {
                "label": "Alabama",
                "value": "AL"
            },
            {
                "label": "Alaska",
                "value": "AK"
            }
         ]
    }
    

    In my template, the state is in an `` element:

    <abbr>{{@config.custom.state.stateName}}</abbr>
    

    Is there a way to access the `label` from the json file? If so I could do something like this:

    <abbr title="{{@config.custom.state.stateNameLabel}}">{{@config.custom.state.stateName}}</abbr>
    

    with the result:

    <abbr title="Alabama">AL</abbr>

    Or, is there another way to send both an abbreviation and full name to the template? I was thinking something like:

    {
        "name": "state",
        "label": "State/territory",
        "group": "Contact info",
        "type": "select",
        "options": [
            {
                "label": "Alabama",
                "value": [
                    "AL",
                    "Alabama"
                ]
            },
            {
                "label": "Alaska",
                "value": [
                    "AK",
                    "Alaska"
                ]
            }
        ]
    }

    But there’s no way to access the value. Using <code class=”EnlighterJSRAW” data-enlighter-language=”generic”>@config.custom.state.stateName  produces no output, while <code class=”EnlighterJSRAW” data-enlighter-language=”generic”>@config.custom.state.stateName.value[0] produces an error.

    Meta: I’m putting this in General Inquiry, maybe it belongs in Feature Suggestions? (Or maybe in the bit bucket!)

    #3962
    Avatar photoBob

    Your field name is “state” not “stateName”, so the @config variable should contain “state, and finally looks like this:

    <abbr>{{@config.custom.state}}</abbr>
    

    The “title” attribute phrases should be placed in the language file.

    #3975
    Avatar photo[anonymous]
    [anonymous] wrote:

    Your field name is “state” not “stateName”, so the @config variable should contain “state

    That was a typo. In my template, I’m using “state”, and it’s working fine. The question is can I get the label of the drop down from config.json.

    In addition to my typo, I think I didn’t explain my question clearly. I now see that the forum software kind of garbled my original post. (It appears to really muck things up with inline code. Has anyone else had problems with that?)

    [anonymous] wrote:

    <abbr>{{@config.custom.state}}</abbr> The “title” attribute phrases should be placed in the language file.

    I don’t understand this. Might be because of the problems I had writing the original post. What language file are you referring to?

    #3976
    Avatar photoBob
    #4038
    Avatar photo[anonymous]

    Ok, so I create theme.lang.json with this:

    {
        "states": {
            "AL": "Alabama",
            "AK": "Alaska",
            "AZ": "Arizona",
            "WY": "Wyoming"
        }
    }
    

    So my theme has a drop down where the user selects a state, say “Alabama” (see the original post in this thread), and the template puts in “AL”. Now, what handlebars expression do I use to get “Alabama”?