Tutorials

Last updated: July 18th, 2018

Intro

This page will contain several components of the KikCMS you should familiarize yourself with to properly use the KikCMS and also to learn about it's capabilities.

Before you begin, make sure that you have setup a working dev environment.

Create a template

Templates are what determine how a page looks. Each page you create in the CMS has it's own template. We will show you here how to create one.

Step 1: Add the template as option

1. Edit the following file, which looks something like this:

app/Classes/TemplateFields.php
<?php
class TemplateFields extends TemplateFieldsBase
{
    public function getTemplates(): array
    {
        return [
            (new Template('home', 'Homepage')),
            (new Template('default', 'Default', ['content'])),
        ];
    }

    public function getFields(): array
    {
        return [
            'content' => (new WysiwygField('content:value', 'Page content', [new PresenceOf])),
        ];
    }
}

2. Add this to the getTemplates array:

(new Template('mytemplate', 'My template', ['content']))

Now when you create or edit a page, you have the option to choose the 'My template' template. The 3rd parameter contains which fields are in this template. You don't have to concern about that for now, just know that your template has a rich text editor for the field 'content'.

Step 2: Add the actual template

Create the following file with the following content:

app/Views/templates/mytemplate.twig
{% extends '@website/base.twig' %}

{% block templateBody %}

    <div style="background-color: #eee">
    My custom template content:
    <br><br>
    {{ content|raw }}
    </div>

{% endblock %}

Show DataTable contents

DataTable's are great to edit data, and here we'll show you how to use the data in your website. Note that this could be done with any data, not just data that was added using a DataTable.

Before proceeding, make sure you have a DataTable ready in the backend and added some records.

Now add this piece of code:

app/Classes/TemplateVariables.php
public function getHomeVariables(): array
{
    return [
        'persons' => Person::find(),
    ];
}

This tells the CMS to add an array of persons to the home template. The find mathod is part of Phalcon. You can now use this in the home template:

app/Views/templates/home.twig
{% for person in persons %}
    <div>{{ person.name }}</div>
{% endfor %}

Show images

If you followed the previous part, you now output the person's name. Now change the code a little to add an image:

app/Views/templates/home.twig
{% for person in persons %}
    <div>
        {{ person.name }}<br>
        <img src="{{ mediaFile(person.image_id, 'default') }}">
    </div>
{% endfor %}