How to get the value of a select in a template?

I have a custom blueprint that allows the user to create content in the backend with a I followed this guide, but how do I get the value created in the blueprint without reimplementing in my template?

e.g., given

options:
        default: PLUGIN_ADMIN.DEFAULT_ORDERING_DEFAULT
        folder: PLUGIN_ADMIN.DEFAULT_ORDERING_FOLDER
        title: PLUGIN_ADMIN.DEFAULT_ORDERING_TITLE
        date: PLUGIN_ADMIN.DEFAULT_ORDERING_DATE

I only get “default” or “folder” where I would want “PLUGIN_ADMIN.DEFAULT_ORDERING_DEFAULT”, “PLUGIN_ADMIN.DEFAULT_ORDERING_FOLDER” when calling the variable in my template. in the grav admin it shows the display text, so how can I pull this info from the blueprint in my template?

Did you try to print it out using the developer toolbar?
Only a guess but is it an array you get? Maybe something like:

default[0]

or

default|first
---

Hi, no you do not receive an array…

This is a modified version of my blueprint for this page type:

           header.sector:
              type: select
              label: Choose the sector
              default: food
              options:
                food: Food Is Good!
                health: Be Healthy
              validate:
                required: true

An actual data element which is based on this blueprint


title: Event
startdate: '2017-01-29'
enddate: '2017-02-01'
sector: food

In my template I am calling the value similar to this:

{% for p in events %}
{{ p.header.sector }}
{% endfor %}

p.header.sector will give you “food” which is the actual value submited in the form, and saved in the content. However, the blueprint is where you define the display value. How can I get this display value back from the blueprint in the template?

I think what i want to do it somehow access the page blueprint from the template and get and object which includes an options array from which I can do a key lookup (?)

I assume in the blueprint I could just set the stored value and the display value the same, but I imagine this would be ideal for multilingual. I feel like its a good practice to store a simple value rather than the literal or that the key value should not have spaces?

No idea. I guess that’s not possible. Which is a pitty. Why you’d have to provide a key-value pair, if you only can access the key?

Setting value_only: true on the blueprint field should save the field value instead of the key.

If that’s not what you want, and you want to fetch the value from the blueprint directly, I just fixed a thing in Grav (https://github.com/getgrav/grav/commit/d3b654bdb06d527124308ffab6a627580c6426db) and this will let you call page.blueprints in the Twig, and get the page blueprint.

You can use {{ dump(page.blueprints) }} (currently raising an exception, I fixed that in develop) to check all it contains, and then access a specific property of the form, for example (very long)

{{dump(page.blueprints.form.fields.tabs.fields.options.fields.publishing.fields['header.published'].options)}}

which will dump

array:2 [ 1 => "PLUGIN_ADMIN.YES" 0 => "PLUGIN_ADMIN.NO" ]

And then access that array keys / values.

Is value_only: true already in 1.1.9? If so, do you mean to use like this:

header.myselect:
   type: select
   value_only: true
   label: Choose the sector

If so, this did not work for me, can you please clarify, thanks!

I have also tried accessing page.blueprints to find the headers. This does not seem to get me the blueprint I custom created in ../user/blueprints/pages/ None of the customer header.myfield values appear here. I would imagine that this blueprint should be associated with the page type. I tried dumping the entire {{ dump(page.blueprints) }}.

I realized my problem. I am display a list page.children() so I was returning the blueprint of the parent page, which is a different type than the children! It seems that {{ dump(page.children()|first.blueprints) }} is giving me the information that I want instead.

Is there a way to get the blueprint by specifying the blueprint manually e.g., something like

{{ blueprint.('user/blueprints/pages/myblueprint.md') }}

I had this issue, but just used the replace Twig function. Not ideal but I could not get anything else to work to correct it. This seemed the fastest way of getting around it at least.

header.property_type:
                  type: select
                  size: medium
                  classes: fancy
                  label: Property Type
                  options:
                      town_house: Town house
page.header.property_type|replace({'_': " "})

town_house the becomes town house

You could then use capitalize or title to further format the value

"town house"|capitalize > Town house
"town house"|title > Town House