Once again, new to Kirby and I’m converting my site into Kirby, so far, it’s going okay.
I’m looking at adding my slider content through the panel, and displaying it in the front end using the foreach loop. I’m thinking of ‘duplicating’ the notes mechanism, where I have a ‘slider’ as a tab on the home page in the panel, and then have ‘slides’ as pages with fields and an image. Then displaying it through a loop.
Is this the best way to go about it or is there an easier way?
Currently my slider contains the image, a h1 title, h2 title and a caption. So I’m thinking 3 fields and an image per slide.
Hope this helps. You can go even further and just add a Files (Files | Kirby CMS) Field and add Headline, Subheadline and Caption to the Files Blueprint. You can then just iterate over the files and use the File-Fields for your content.
That worked, I just added the columns to that blueprint and it’s perfect.
So now that I have my slides in I have also added a toggle on/off, and then using a php statement if toggle is ‘on’ display slide. Would that be the right way to go about it? Is there a draft/published state for structured fields?
Another question - default values in fields, I can’t seem to set a default value to the toggle or to an options field.
toggle
status:
width: 1/3
label: Status
type: toggle
default: Active
text:
Inactive
Active
options
text_color:
width: 1/3
label: Text Color
type: select
default: Light
required: true
options:
light: Light
dark: Dark
I read on the forum that default values don’t work in Structured Fields - not sure if that is still the case?
Heyho @Milos2504,
I guess you would make it with a toggle as you did. At least that is the way I would do it.
As you can see here Toggle | Kirby CMS the toggle field returns a boolean wich is “true” or “false”. What you do with “Text” Option is make it readable in the blueprint with your own label. But in the Template it returns a boolean. Therefore you have to access it in the Template like (the unnecessary long way for visualization):
<?php if ($page->toggle()->toBool() === true) : ?>
//... do amazing stuff here...
<?php endif; ?>
And in the short way
<?php if ($page->toggle()->toBool()) : ?>
// ... do amazing stuff here...
<?php endif; ?>
That’s possible because your toggle returns “true” or “false” and with the method toBool() it becomes a real Boolean. So the last if clause would return true or false as well as the first one.
Oh well… it’s a bit too early. But as @texnixe wrote you can set default values, but for toogles it must be the boolean. And dont forget that these values are set when you create a new item. Not for the old ones.
Instead of checking for the toggle with an if statement, I’d filter the structure items by the toggle field to clean up the code a bit. You also don’t need the first if statement:
Okay so still no luck with your shortened code. I don’t get an error but nothing is showing up. Could be something to do with the toggle. I tried changing the toggle field name from status to toggle, and then I get an error.
The filter code I posted above should work, no matter if the field is called status or toggle. Make sure that when you change the fieldname in the blueprint, you change it for existing fields in your content files.
And make sure to use the correct field name to filter by in the filterBy method.