Sign in

Access label from config.json select in Publii template

  • This topic has 4 replies, 2 voices, and was last updated 2 weeks, 5 days ago by .
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #3959
    btrem

    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
    Bob
    Support

    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.

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

    #3975
    btrem

    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?)

    <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
    Bob
    Support

    HI,

    This one https://getpublii.com/dev/translations-api/

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

    #4038
    btrem

    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”?

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.