PHPackages                             tatter/themes - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. tatter/themes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tatter/themes
=============

Lightweight theme manager for CodeIgniter 4

v1.2.0(5y ago)1710.3k↓50%5[3 issues](https://github.com/tattersoftware/codeigniter4-themes/issues)[1 PRs](https://github.com/tattersoftware/codeigniter4-themes/pulls)2MITPHPPHP ^7.3 || ^8.0

Since Jun 20Pushed 2y ago2 watchersCompare

[ Source](https://github.com/tattersoftware/codeigniter4-themes)[ Packagist](https://packagist.org/packages/tatter/themes)[ Docs](https://github.com/tattersoftware/codeigniter4-themes)[ Fund](https://paypal.me/tatter)[ GitHub Sponsors](https://github.com/tattersoftware)[ RSS](/packages/tatter-themes/feed)WikiDiscussions develop Synced 3d ago

READMEChangelog (9)Dependencies (4)Versions (12)Used By (2)

Tatter\\Themes
==============

[](#tatterthemes)

Lightweight theme manager for CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-themes/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-themes/actions/workflows/test.yml)[![](https://github.com/tattersoftware/codeigniter4-themes/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-themes/actions/workflows/analyze.yml)[![](https://github.com/tattersoftware/codeigniter4-themes/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-themes/actions/workflows/inspect.yml)[![Coverage Status](https://camo.githubusercontent.com/8c72049e5c48c61b5da304dc0231097b09861908e5883c0f3774d85a9b877b15/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f746174746572736f6674776172652f636f646569676e69746572342d7468656d65732f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/tattersoftware/codeigniter4-themes?branch=develop)

Quick Start
-----------

[](#quick-start)

1. Install with Composer: `> composer require tatter/themes`
2. Update the database: `> php spark migrate -all`
3. Seed the database: `> php spark db:seed "Tatter\Themes\Database\Seeds\ThemeSeeder"`
4. Place theme files in **public/assets/themes/default**
5. Add theme files to your page, e.g.: `echo view('\Tatter\Themes\Views\css)`

Features
--------

[](#features)

Provides convenient theme file organization and display for CodeIgniter 4

Installation
------------

[](#installation)

Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date:

- `> composer require tatter/themes`

Or, install manually by downloading the source files and adding the directory to `app/Config/Autoload.php`.

Once the files are downloaded and included in the autoload, run any library migrations to ensure the database is setup correctly:

- `> php spark migrate -all`

You will also need to seed the database with a default theme:

- `> php spark db:seed "Tatter\Themes\Database\Seeds\ThemeSeeder"`

Dependencies
------------

[](#dependencies)

`Themes` relies heavily on these libraries; be sure you are familiar with their own requirements and installation process.

- [Tatter\\Assets](https://github.com/tattersoftware/codeigniter4-assets) handles asset discovery and HTML tag injection
- [Tatter\\Preferences](https://github.com/tattersoftware/codeigniter4-preferences) allows for user- or session-specific theme changing
- [CodeIgniter\\Settings](https://github.com/codeigniter4/settings) (a dependency of `Preferences`) loads and stores theme preferences into persistent storage

If you plan on allowing users to change their own themes then you will also need to include an authentication library the provides `codeigniter4/authentication-implementation` (no other configuration necessary).

> Read more about CodeIgniter Authentication in the [User Guide](https://codeigniter.com/user_guide/extending/authentication.html).

Usage
-----

[](#usage)

*This library assumes you already have the asset files (CSS and JavaScript) used by your themes.*

Themes are managed via the database and configured for your application using Filters.

### Managing Themes

[](#managing-themes)

Theme files all go into a directory relative to the config property `$directory` from `Tatter\Assets` (default is **public/assets/**), as defined by a theme's `path`. E.g.

- public/assets/themes/default/styles.css
- public/assets/themes/default/script.js
- public/assets/themes/dark/header.css
- public/assets/themes/dark/fonts.css
- public/assets/themes/perky/Perky.CSS

Each theme is an entry in the database `themes` table with the following properties:

- `name`: A short, unique name used for theme lookup and display, e.g. "Aquatic Journey"
- `path`: The path (relative to the `Assets` directory) to the publicly-available theme files, e.g. "themes/aquatic/"
- `description` (optional): A brief description of this theme's features, mostly useful for allowing user selection, e.g. "A blue theme with deep hues and liquid borders"
- `dark`: Whether this theme is dark (light text, dark backgrounds) or not, e.g. `true`

You may use the supplied model (`Tatter\Themes\Models\ThemeModel`) to create new themes or return entities (`Tatter\Themes\Entities\Theme`). The library comes with the `ThemeSeeder`which will create an initial "Default" theme for you at **public/assets/themes/default/**. There is also a `themes:add` Spark command to guide you through loading themes from CLI.

### Selecting a Theme

[](#selecting-a-theme)

The current theme is determined as follows:

1. Is there an authenticated user? Check `Preferences` for that user's theme preference
2. No authenticated user? Check the Session for a theme preference
3. Neither of above? Check `Settings` for a stored persistent theme
4. None of the above? Fall back on the config file: `config('Preferences')->theme` (default value "Default")

Likewise, you can set the current theme using the corresponding methods (in priority order):

1. `preference('theme', $themeName)` (with authenticated user)
2. `preference('theme', $themeName)` (without authenticated user)
3. `setting('Preferences.theme, $themeName)`
4. Create or edit **app/Config/Preferences.php** and add: `public $theme = 'theme_name';`

To assist with methods 1 &amp; 2 this library comes with a tiny module to display a form and process user input. You can add the form to an existing page with the Form View:

```

```

...or add the preconfigured `` field to an existing form:

```
