Reusable Contentpieces – How to

Hi,
i’ fairly new to Kirby. So far I explored the starter kit and testet some basics blueprint, template implementation myself.

But now, this early, I hit a roadblock.

How can I build reusable content pieces / field combinations in the panel?

So for example if I want to display something like a list of work experiences with different entries. Every entry has for example a date, a name, and such. So a combination of different fields. I want to be able to add multiple new entries, every entry with the fixed set of fields.

This doesn’t need to be a page, or ist this the way to go? Can I use blocks for this?

Any hint would be a appreciated :smiley:

If the fields are the same, you would use a structure field. For different field combinations, you could use the blocks field

The structure field was in general what I was looking for. thanks.

but now I have another question. Is it possible to create 3 different structures but have them “editable” in one field block.

I think about something like three different variants of the same “content type”. So in this work experience example the could be one variation with only a year and an employer, the next with a year, a role and employer and a last one with a year, an employer, a role and a image collection.

Is this possible to achieve?

As I wrote above, a structure field always has the same set of fields. But in your example, you have those four fields (year, employer, role, image collection), why not use them all but not require them, why would you need variants? In the frontend, you would just check if a field has content.

Of course, you can use show fields conditionally (Fields | Kirby CMS), but not sure if this fits your use case.

Yes, I just tried something like that. Combination of structure with conditional.

fields:
  milestones:
    label: Milestones
    type: structure
    fields:
      variant:
        required: true
        label: Variant
        type: radio
        options:
          large: large
          medium: medium
          small: small
      large:
        label: Large Milestone
        type: structure
        when:
          variant: large
        fields:
          info:
            label: Project Info
            type: text
          client:
            label: Project Client
            type: text
          year:
            label: Year
            type: date
      medium:
        label: Medium Milestone
        type: structure
        when:
          variant: medium
        fields:
          info:
            label: Project Info
            type: text
          client:
            label: Project Client
            type: text
      small:
        label: Small Milestone
        type: structure
        when:
          variant: small
        fields:
          info:
            label: Project Info
            type: text

But the problem is, no one can create multiple entries of “small” in the second level. but I only want entries in the first level, with only different variants.

The use-case: I want to use completely different snippets/templates for every variant and in the panel I want that the editors knows which “template” he triggers.

Ok, I the multiple entries could be prevent by using the “max” on the second structure. but I don’t think, that this is a good solution. Don’t have better ideas with my limited knowledge :smiley:

I meant something else:

fields:
  milestones:
    label: Milestones
    type: structure
    fields:
      variant:
        required: true
        label: Variant
        type: radio
        options:
          large: large
          medium: medium
          small: small
      info:
        label: Project Info
        type: text
      client:
        label: Project Client
        type: text
        when:
          variant: medium
     anotherField:
       type: whatever
       when:
        variant: large
     # etc.

So only one structure field but the fields in the structure are conditional. (Indentation might be off)

Oh, yeah that’s also possible. I tested that before. Do you think, that’s the best case for what I want to achieve?
(I think in the end this wouldn’t work, because in my case I would need an conditional ‘or’ since I want to show some fields in all cases)

In the end I want to display some kind of “timeline” in the frontend mit multiple entries, sorted by year, and different visual variants like small, medium, large.

If you want to show a field in all cases, you don’t need a when property at all.

For cases where you would need an or condition, you can use the WhenQuery plugin: WhenQuery | Kirby CMS

1 Like

Oh, that’s cool :slight_smile: Do you see a better solution to achieve my goal? Or is a combination of structure + conditional the best solution here?

Additional Question: Is it possible to save something like these “milestones” without the need to have a page for it/include it in a page? So like just “data” and consume this data in multiple page layouts? For example top 3 entries on the homepage and all entries on another page or so.

You can store such things either in a (separate) page or in the site object (site.yml). In both cases, this content would be available anywhere and you can filter stuff as needed.

1 Like