Technical notes about OctoberCMS a Laravel CMS. Notes are written by inetis team, a Swiss webagency

Did you manage multiple websites? Try our OctoberCMS monitoring solution

<- Back

Advanced repeaters in Static Pages plugin

Posted on by

RainLab Static Page plugin allow you to create simple custom fields and good news : advanced repeaters are also supported ! but they needs a few lines of code for work properly

First of all your need to know that OctoberCMS support few type of repeaters :

Simple repeaters

You can add the following lines in your layout

{repeater name="sections" tab="Sections" prompt="Add a section"}
  {variable name="title" type="text" span="left"}{/variable}
  {variable name="content" type="richeditor"}{/variable}
{/repeater}

and after process them like you want in your template :

{% for section in sections %}
    <section id="{{ section.title|slug }}">
        <h2>{{ section.title }}</h2>
        {{ section.content|raw }}
    </section>
{% endfor %}

Nested repeaters

Add following in your Plugin boot method

Event::listen('backend.form.extendFieldsBefore', function (\Backend\Widgets\Form $form) {

    if (
        !$form->getController() instanceof \RainLab\Pages\Controllers\Index ||
        !$form->model instanceof \RainLab\Pages\Classes\Page ||
        $form->isNested
    ) {
        return;
    }

    if ($form->model->viewBag['layout'] == 'page') {

        $fields = Yaml::parseFile(themes_path('themes/YOUR_THEME/meta/nestead-repeaters.yaml'));

        $form->secondaryTabs['fields']['viewBag[sections]'] = [
            'tab'      => 'Sections',
            'type'     => 'repeater',
            'form'     => ['fields' => $fields],
            'cssClass' => 'secondary-tab ',
        ];

        // For make the field translatable with RainLab.Translate plugin
        $form->model->translatable[] = 'viewBag[sections]';
    }

});

Create the following file themes/YOUR_THEME/meta/nestead-repeaters.yaml and past the following content :

title:
    label: Title
    type: text
    span: full

content:
    label: Content
    type: repeater
    prompt: Add new content block
    form:
        fields:
            title:
                label: Title
                type: text
                span: full

            content:
                label: Content
                type: richeditor
                span: full

Grouped repeaters

In your layout add the following lines

{repeater name="section" groups="themes/YOUR_THEME/meta/grouped-repeaters.yaml" tab="Sections" prompt="Add a section" }
  {# parser fail if there is noting between open and close tag #}
{/repeater}

Create the following file themes/YOUR_THEME/meta/grouped-repeaters.yaml and past the following content :

content:
    name: Section
    description: Content block
    icon: icon-paragraph
    fields:
        title:
            label: Title
            type: text
            span: full

        content:
            label: Content
            type: richeditor
            span: full

title:
    name: Title
    description: Heading
    icon: icon-header
    fields:
        title:
            label: Title
            type: text
            span: full

More informations