RU EN
Get started

Documentation Tools

AJAX and JavaScript

JSON actions, the ajax layout, system.js and rendering views from the browser.

A JSON action

To respond to JavaScript code, switch the layout to ajax and return a JSON string. A JSON request body is read with the getData() method:

site/modules/shop/controllers/cartController.php
<?php

use olala\controller;
use olala\view;
use site\models\Cart;

class ShopCartController extends controller {

    // POST /shop/cart/add   body: {"id": 15, "qty": 2}
    function addAction() {
        view::ajax();
        header('Content-Type: application/json; charset=utf-8');

        $data = $this->getData();
        $total = Cart::add((int)$data['id'], (int)$data['qty']);

        return json_encode(['ok' => true, 'total' => $total]);
    }
}
JavaScript
const r = await fetch('/shop/cart/add', {
    method: 'POST',
    body: JSON.stringify({ id: 15, qty: 2 }),
});
const { total } = await r.json();

You create the ajax layout once: the file site/layouts/ajax/index.php with a single line <?= $content ?>. In this mode the core returns errors as JSON as well: {"error": "…"}.

HTML fragments

The same trick works for parts of a page: the action renders a template without the shared layout, and JS inserts the result where needed.

PHP
// GET /blog/index/more?page=2
function moreAction() {
    view::ajax();
    return $this->render('list', ['posts' => Post::page((int)($_GET['page'] ?? 1))]);
}

Client-side helpers

The olala/web folder contains small scripts you can include in your layout:

FileWhat it gives you
/olala/web/js/head.jsonPageLoaded(fn) — defer code until pageLoaded() is called, once all scripts are loaded
/olala/web/js/system.jssystem.alert(), system.dialog, system.forms.getData() (requires jQuery; dialogs need Bootstrap)
/olala/web/js/view.jssystem.view.render() — rendering templates on the server (include after system.js)
/olala/web/css/system.cssstyles for the system.alert() pop-up notification
site/layouts/default/index.php
<head>
    <script src="/olala/web/js/head.js"></script>
    <link rel="stylesheet" href="/olala/web/css/system.css">
</head>
<body>
    ...
    <script src="/assets/js/jquery.min.js"></script>
    <script src="/assets/js/bootstrap.bundle.min.js"></script>
    <script src="/olala/web/js/system.js"></script>
    <script src="/olala/web/js/view.js"></script>
    <script>pageLoaded();</script>
</body>

Notifications, dialogs, forms

JavaScript
// a pop-up message in the corner of the screen for 3 seconds
system.alert('Item added to cart', 3000);

// a modal window whose content is loaded from a URL
system.dialog.show({
    title:  'Quick view',
    url:    '/catalog/item/preview',
    params: { id: 15 },                 // appended to the URL: ?id=15
    width:  '720px',
    buttons: {
        'Add to cart': () => addToCart(15),
    },
});
system.dialog.hide();

// all form fields (including checkboxes) → FormData
const body = system.forms.getData('#order-form');
fetch('/shop/order/create', { method: 'POST', body });   // on the server — $_POST

By default the dialog markup targets Bootstrap 4. For Bootstrap 5, set system.bootstrap = 5.

Rendering templates from JavaScript

The system route /system/view/render lets the browser render a server-side template with the given parameters. For security reasons only whitelisted templates are available — the whitelist is set in config.php:

config.php
use olala\view;

view::$allowedViews = [
    'product-card' => '/site/modules/catalog/views/card',   // alias => path from the root without .php
];
JavaScript
const html = await system.view.render('product-card', { title: 'Sneakers', price: 49 });
document.querySelector('#list').insertAdjacentHTML('beforeend', html);

The parameters become the template variables $title and $price. For an alias not on the list the server returns an empty string.