Spiral Framework Pieces Module

v1.0.8 2017-08-08 12:30 UTC


This module provide ability to store page pieces data in database (currently ORM only) and access it via Web-API. Also it's compiles two versions of views: one for site editor and one for common site user. The functionality can be sometimes useful especially with something like WriteAway.


$ composer require spiral/pieces
$ ./spiral register spiral/pieces
$ ./spiral up

Add Bootloader

const LOAD = [

Configure permissions

Check app/config/modules/pieces.php for details.


There are two alternatives to include metadata to your pages: "static" and "runtime". First one will fully compile during views compilation and there will be no requests to database during page load. Second one will not.

<dark:use path="pieces/meta" as="pieces:meta"/>

<pieces:meta title="Foo" description="Bar" keywords="Baz">
  <meta name="foo" content="bar">

The code above is "static" metadata. You can optionally pass some defaults: title, description, keywords arguments and put custom default html (see code above).

If you need something a bit more complex than dumb static pages, then currently you need to use "runtime" metadata. In opposite to "static" metadata you should pass namespace, view and code arguments, but title, description and keywords are still optional.

<dark:use path="pieces/runtime-meta" as="pieces:meta"/>

<?php #compile
/** @var Article $entity */

<pieces:meta title="<?= $entity->title ?>" description="<?= $entity->description ?>"
             namespace="<?= $this->namespace #compile ?>" view="<?= $this->view #compile ?>"
             code="<?= $entity->id ?>">
    <meta name="foo" content="bar">

It's useful to note, that both alternatives will append (in editor mode):

  window.metadata = <?= json_encode($meta) ?>;

to help you with frontend editor setup.

If you don't like the behaviour you're always free to write your own code... And make a PR. :-)


Currently there's only "static" pieces.

<dark:use path="pieces/piece" as="pieces:piece"/>

<pieces:piece name="sample-piece">
  Piece content.

In editor mode content of the piece will be wrapped in div like this:

<div data-piece="html" data-id="sample-piece">
  Piece content.