PHPackages                             coreorm/slim3-view - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. coreorm/slim3-view

ActiveLibrary[Templating &amp; Views](/categories/templating)

coreorm/slim3-view
==================

view renderer for slim3 with very straightforward and simple (yet still customisable) theme/layout/templates structure

v1.1.6(9y ago)4136MITPHPPHP &gt;=5.6

Since Sep 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/coreorm/slim3-view)[ Packagist](https://packagist.org/packages/coreorm/slim3-view)[ RSS](/packages/coreorm-slim3-view/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (3)Versions (8)Used By (0)

slim3-view
==========

[](#slim3-view)

[![Build Status](https://camo.githubusercontent.com/3666c05afa74a8f3276add796286e7bf4ff52bb1ca21def2e2cb65a3c4db027a/68747470733a2f2f7472617669732d63692e6f72672f636f72656f726d2f736c696d332d766965772e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/coreorm/slim3-view)

View renderer for slim3 with very straightforward and simple (yet still customisable) theme/layout/templates structure.

This also works with any PSR-7 compliant response objects.

Example
-------

[](#example)

- live demo:
- source:

Requirement
-----------

[](#requirement)

- php 5.6+
- composer

Install
-------

[](#install)

`composer require coreorm/slim3-view`

Test
----

[](#test)

`phpunit`

Structure
---------

[](#structure)

make sure your themes directory are in the following structure:

```
themes
├── default
│   └── layouts
│       └── default.phtml
├── theme 1
│   ├── layouts
│   │   └── main.phtml
│   └── views
│       └── index.phtml
...

```

- Layout: layout files are to be placed in `/layouts/` directory.
- Note that a default layout is recommended in case a custom layout is not found, it will fall back to the default one.
- Views: views must be under views, but can be in nested directories, when you need to render it, simply use the relative path against views, e.g. if you have `views/foo/bar.phtml`, simply call `$theme->render($response, 'foo/bar')`

Layout template
---------------

[](#layout-template)

Layout template must contain a default variable `$mainContent` in the body so sub templates can appear, e.g.

```
# file: themes/default/layouts/default.phtml

...

...

```

Another way to send rendered content to the page is to use `$theme->renderView()`, e.g.

```
# in route
$theme->setData('myHeader', $theme->renderView('shared/header'));

# in layout

...

echo $myHeader

...

```

Usage Example (or just open up examples/app/index.php):
-------------------------------------------------------

[](#usage-example-or-just-open-up-examplesappindexphp)

- Use with slim

```
use \Coreorm\Slim3\Theme;

$app = new Slim\App();

$theme = Theme::instance(__DIR__ . '/../themes', 'example');
// set layout
$theme->setLayout('main')->setData('title', 'Slim 3 View Example');

$app->get('/', function ($request, $response, $args) use ($theme) {
    return $theme->render($response, 'index', [
        'foo' => 'bar'
    ]);
});

$app->run();

```

- Use with other PSR-7 compliant library

```
$response = new Response();
$response = Theme::instance('theme-base-dir', 'theme-name')->render($response, 'template-name', [data array]);
echo $response->getBody();

```

APIs
----

[](#apis)

### Theme APIs

[](#theme-apis)

#### Theme::instance($templatePath = null, $theme = 'default')

[](#themeinstancetemplatepath--null-theme--default)

**instantiate the theme class**: We use a singleton pattern to ensure all shared data are available to each and every template, so simply use:

```
use \Coreorm\Slim3\Theme;

$theme = Theme::instance('theme base directory', 'layout name');

```

#### $theme-&gt;setLayout($layout)

[](#theme-setlayoutlayout)

**switch layout**: It's possible to switch layout either from the beginning of the code, or inside the routes at run time.

```
$theme->setLayout('layout name');

```

E.g.

```
$app->get('/', function ($request, $response, $args) use ($theme) {
    $theme->setLayout('new-layout');
    return $theme->render($response, 'index', [
        'foo' => 'bar'
    ]);
});

```

#### $theme-&gt;setTheme($theme)

[](#theme-setthemetheme)

**switch theme**: This will set the current theme to the string value of `$theme`.

#### $theme-&gt;share($templateRelativePath, $theme)

[](#theme-sharetemplaterelativepath-theme)

**mark a template from a specific theme as shareable**: this will essentially make it a global template to go to when a template with the same name from a different theme is not found.

- `$templateRelativePath` the relative path to the template
- `$theme` the theme name

E.g.

```
$theme->share('views/page1', 'default');

```

This will mark the page `themes/default/views/page1.phtml` as the shared view for relative path `views/page1`, so if say I'm in theme 'new' and I call `renderView('page1')`, if `themes/new/views/page1.phtml` doesn't exist, it will use `themes/default/views/page1.phtml` instead.

#### $theme-&gt;render(ResponseInterface $response, $template, array $data = \[\], $shouldFallback = false)

[](#theme-renderresponseinterface-response-template-array-data---shouldfallback--false)

**render a template plus the layout**: This is the final render function you'd want to run in your route/controller, as it renders the give template and assigns it to the `$mainContent` partial data which in turn will be displayed in the template main body (or wherever you would like to, by calling ``). So it renders template, then layout, and then output to the browser.

- `$shouldFallback` - when this is set to true, the system will try to find the fallback template if the given template is not found (see above `share` API for more details).

E.g.

```
$theme->render($response, 'relative path/template name', [partial data], true);

```

#### $theme-&gt;renderView($template, $data = \[\], $shouldFallback = true, $reuseHTML = false)

[](#theme-renderviewtemplate-data---shouldfallback--true-reusehtml--false)

**render a template and retrieve the content**: This renders a template and will return the html.

`$reuseHTML` if this is set to true, the given template will render only once, and any future renderView/import calls to this template will reuse the HTML that's rendered from the very first one. Use this for elements that are repeated on the same page with exactly the same HTML source.

E.g.

```
$viewSrcHTML = $theme->renderView('relative path/template name', [partial data]);

```

### Template (layout/views) API

[](#template-layoutviews-api)

#### $this-&gt;import($templateFile, $data = \[\], $shouldFallback = true, $reuseHTML = false)

[](#this-importtemplatefile-data---shouldfallback--true-reusehtml--false)

**import a sub template inside a template** This is only available inside the template itself.

In the template code, do:

```
$this->import('relative path/template name', [partial data]);

```

Data scope
----------

[](#data-scope)

### Global (data available to all templates)

[](#global-data-available-to-all-templates)

`$theme->setData($k, $v)` sets data that is available for all templates/layouts;

### Private (partial data that will override global data, when used for rendering individual views)

[](#private-partial-data-that-will-override-global-data-when-used-for-rendering-individual-views)

passing partial data to the render/import function will set data that is private to the given template only. e.g. `$theme->render($response, 'page', ['foo' => 'bar'])` will set the $foo value to 'bar' only for the `page` template.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 60% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

6

Last Release

3513d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1b259c7ab14ca3c9083ef41a7097619ad432391b063ca256527d8362e9c7e5cf?d=identicon)[qstufie](/maintainers/qstufie)

---

Top Contributors

[![toughgamer](https://avatars.githubusercontent.com/u/105064?v=4)](https://github.com/toughgamer "toughgamer (18 commits)")[![coreorm](https://avatars.githubusercontent.com/u/8488369?v=4)](https://github.com/coreorm "coreorm (8 commits)")[![brucieLi](https://avatars.githubusercontent.com/u/20389118?v=4)](https://github.com/brucieLi "brucieLi (4 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/coreorm-slim3-view/health.svg)

```
[![Health](https://phpackages.com/badges/coreorm-slim3-view/health.svg)](https://phpackages.com/packages/coreorm-slim3-view)
```

###  Alternatives

[moonshine/moonshine

Laravel administration panel

1.3k217.1k59](/packages/moonshine-moonshine)[modx/revolution

MODX Revolution is a Content Management System

1.4k9.1k12](/packages/modx-revolution)[rareloop/lumberjack-core

A powerful MVC framework for the modern WordPress developer. Write better, more expressive and easier to maintain code

42155.0k19](/packages/rareloop-lumberjack-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
