Skip to main content
Documentation
On this page

Install Cirth for Slim

Slim has no view layer and no asset pipeline of its own: a route returns a response, and the web server exposes public/ as its web root. Cirth is one public file there. A PHP-View layout supplies the document head around every rendered template, so one link loads it globally.

Verified with Slim 4.15.3 on

Six steps: create the project, add the file, link it, render a page, run it in production mode and check it.

1 Create the project #

composer create-project slim/slim-skeleton my-app
cd my-app
composer require slim/php-view

On PHP 8.4 or later, also run composer require php-di/php-di:^7 -W. The skeleton's PHP-DI 6 prints deprecation notices when errors are displayed, and anything printed before <!doctype html> puts the page in quirks mode, which no stylesheet can undo.

2 Add the file #

mkdir -p public/css
curl -o public/css/cirth.min.css \
  https://cdn.jsdelivr.net/npm/@cirthcss/cirth@0.17.0/dist/cirth.min.css

Create templates/layout.php. PHP-View renders every template inside it, as $content:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></title>
    <link rel="stylesheet" href="/css/cirth.min.css">
  </head>
  <body>
    <main class="container">
      <?= $content ?>
    </main>
  </body>
</html>

4 Render a page #

templates/subscribe.php:

<article>
  <header><h1>Hello, <?= htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></h1></header>
  <form method="post" action="/subscribe">
    <label>
      Email
      <input type="email" name="email" required>
    </label>
    <button type="submit">Subscribe</button>
  </form>
</article>

<section data-theme="dark">
  <article>
    <p>Dark, whatever the system scheme is.</p>
  </article>
</section>

In app/routes.php, the home route renders it in the layout:

use Slim\Views\PhpRenderer;

$app->get('/', function (Request $request, Response $response) {
    $renderer = new PhpRenderer(__DIR__ . '/../templates', ['title' => 'Subscribe'], 'layout.php');

    return $renderer->render($response, 'subscribe.php', ['name' => 'Slim']);
});

5 Run it in production mode #

Set displayErrorDetails to false in app/settings.php, install without the development packages and serve public/:

composer install --no-dev --optimize-autoloader
php -S localhost:8080 -t public

PHP's built-in server is for trying the app; in production, Slim's web servers page shows Apache, nginx and others with public/ as the document root.

6 Check it #

Open http://localhost:8080 with the system in light mode, then in dark mode. The page follows the system, and the section with data-theme="dark" stays dark both times.

Customization covers the --cirth-* properties; set yours in a stylesheet linked after Cirth. Package and builds lists the classless, scoped and print entry points. Return to Framework guides for another stack.

Search documentation

Type at least two characters to search.