Might be a stupid question but working with a slightly complex structure field with some conditional fields (only visible when:…).
All fields are in the output in the .txt file. Even those that aren’t used depending on the selections you make when adding an item to the structure field. Was wondering if there is some magic to only output fields that are “visible”
Example:
blocks:
columns:
type:
width: 1/2
visibility:
width: 1/2
label: Blocks
type: structure
fields:
type:
default: text
label: Type
options:
master: Master
text: Text
required: true
type: select
text:
label: Text
size: small
type: textarea
files:
query: page.files.add(site.files)
buttons:
extends: fields/buttons
when:
type: master
visibility:
default: true
label: Visibility
text:
- Hide
- Show
type: toggle
Here, even though you select “text”, an empty “buttons” field will be in the output.
Well, you would usually check if a field has content or not before you output it anyway, won’t you (if only to avoid empty HTML tags)?
Having said that, it might actually happen that a field is not empty, even though it shouldn’t be rendered (this will happen if the value of the field on which the condition depends is later changed but the old content is not removed).
My question was more towards the .txt output. This is purely just to reduce the file size of my .txt files hehe.
In my example the buttons field is only visible when type: master. It’s basically the idea to not export data from conditional fields when they’re not “visible”.
Blocks:
-
type: text
text: This is some text.
buttons: [ ]
visibility: 'true'
Optimized output would be…
Blocks:
-
type: text
text: This is some text.
visibility: 'true'
How many entries is this field supposed to have? Asking because I wonder if it’s worth to go the extra mile and create a model that removes unused fields just to shave off a few extra bytes that don’t make a difference.
I’ve used the structure field to create a block system. There are several types: master, posts, slider, text…
When you add an item you start by selecting a block type. Then things below will change accordingly.
Here’s an output for two items. First one is a master block, second one is a text block. As you can see there are empty fields exported for each input.
Fields that are unique to a specific block type starts with the type name. Maybe there is a way to remove those fields when they’re not in use? For example when you select “text” as type, fields starting with master… posts… and slider… are not needed.
Blocks:
-
type: master
text: This is some text.
buttons:
-
text: Sign Up
style: primary
link: page
page:
- test
url: ""
target: 'false'
media:
-
source: custom
custom:
- banner.png
size: large
rounded: all
mastercolumns: two
mastercolumnwidth: equal
mastercolumnreverse: 'false'
posts: ""
postssortbydate: 'false'
postsauthor: 'false'
postsdate: 'true'
postscolumns: one
postsnumber: null
poststags: ""
slidermedia: [ ]
slidercenter: 'false'
slidercaption: 'false'
sliderheight: ""
sliderwidth: large
sliderwidthcustom: ""
align: align-left
width: large
paddingbottom: 'true'
background: [ ]
visibility: 'true'
layoutheadline: null
-
type: text
text: This is some text.
buttons: [ ]
media: [ ]
mastercolumns: one
mastercolumnwidth: equal
mastercolumnreverse: 'false'
posts: ""
postssortbydate: 'false'
postsauthor: 'false'
postsdate: 'true'
postscolumns: one
postsnumber: null
poststags: ""
slidermedia: [ ]
slidercenter: 'false'
slidercaption: 'false'
sliderheight: ""
sliderwidth: large
sliderwidthcustom: ""
align: align-left
width: large
paddingbottom: 'true'
background: [ ]
visibility: 'true'
layoutheadline: null
If you want my honest opinion, then the structure field is not the right choice for this use case. That’s exactly what the builder field was made for.
As I said, maybe you could exclude storing these fields in a page model, but then you would have to go through each item, check what type is set, unset all those fields… Doesn’t sound like a good idea to me.