inkphp GitHub 中文

Page skeleton and layouts

The skeleton

<?php
[$raw] = params(0, 1);              // 1. URL params
allow('GET', 'POST');               // 2. methods (omit for GET-only pages)
$me = require_login();              // 3. when login is required
// 4. logic: validation, SQL, redirects
if (is_post()) {
    csrf_check();
    // ...
}
$rows = all('SELECT ...');
layout('site', ['title' => 'Title']);  // 5. the frame (optional)
?>
<!-- 6. HTML: every variable through e() -->
<h1><?= e($title) ?></h1>

Pages run in global scope and never require the kernel themselves.

Layouts: layout()

layout('site', [...]) wraps the page output, passed as $content, in layouts/site.php. A layout only sees the variables passed explicitly, each with a default ($title ?? ''). For an htmx fragment, call layout(null) or exit before calling layout().

layouts/site.php<!doctype html>
<title><?= e($title ?? 'My site') ?></title>
<main><?= $content ?></main>

In-page fragments

inkphp has no HTML fragments shared between pages (no partial()). HTML a page prints twice (full page and htmx refresh) is a function inside that page:

$list = function (array $rows): void { ?>
    <ul><?php foreach ($rows as $r): ?><li><?= e($r['name']) ?></li><?php endforeach ?></ul>
<?php };

header('Vary: HX-Request');
if (is_htmx()) { $list($rows); exit; }   // just the fragment
layout('site', ['title' => 'List']);
?>
<div id="list" hx-get="/list" hx-trigger="every 30s"><?php $list($rows) ?></div>

If another page needs a similar list, it gets its own copy. A little repetition buys you this: changing one page can never break another.

Shared business functions

Functions several pages need live in lib/, and pages require them explicitly:

require_once INK_ROOT . '/lib/money.php';   // after the leading declarations
echo e(money_format_usd($cents));

Files in lib/ only define functions and constants: no output and no queries when included, and prefixed function names.