Home

Awesome

Laravel Themify

Themify is a Laravel package that provides basic theme functionality in a non-obtrusive way. The purpose of Themify is to allow the developer to group views inside themes, having each theme its own folder. If you have experience with Yii framework theming, you will find this package usage very familiar.

A sample structure folder could be like this:

app/
├── Http
├── ...
├── themes
│   ├── admin
│   │   ├── category
│   │   ├── dashboard
│   │   ├── ...
│   └── default
│       ├── index.blade.php
│       ├── layouts
│       ├── post
│       └── ...

Themify expects you to store your themes in a given folder, which is app/themes by default. Then, each theme should have its own views inside its folder, just like it was a views folder.

Installation

composer require nwidart/themify=*
'providers' => array(
    ...
    'Illuminate\View\ViewServiceProvider',
    'Illuminate\Workbench\WorkbenchServiceProvider',

    'Nwidart\Themify\ThemifyServiceProvider',
),
'aliases' => array(
    ...
    'URL'             => 'Illuminate\Support\Facades\URL',
    'Validator'       => 'Illuminate\Support\Facades\Validator',
    'View'            => 'Illuminate\Support\Facades\View',

    'Themify'         => 'Nwidart\Themify\Facades\Themify',
),
php artisan vendor:publish

Then, modify settings as needed by editing config/themify.php.

Usage

First, you have to tell the package which theme you want to use. You have three different ways to do this, ordered by priority:

  1. Calling Themify::set($theme). Being theme the name of the folder of the theme you want to use.
  2. Defining a public $theme property in your controller.
  3. Using Themify::defaults($theme), which is a shortcut to changing the themify::default_theme property in package settings.

Once you have defined your theme, you can render your views using the View class in the traditional way. Themify will try to find the specified view inside the defined theme folder. If it doesn't find it, it will fallback to the default views folder (or whatever you have defined in your app/config/view.php).

View::render('foo', compact($bar));

Priorities

Each of the mentioned methods has an internal priority assigned:

<?php

class MyAwesomeController extends BaseController {
    
    public $theme = 'bootstrap';

    public function index()
    {
        return View::make('index');
    }

}

Theme assets

Themify expects you to have a folder inside your public directory (or the one that you have defined in your Laravel configuration) to store theme assets. By default, this folder is public/assets/themes, but it can be modified in the package configuration file.

Thus, this assets folder should contain one folder per theme. For example, if you are using a bootstrap theme, you should create public/assets/themes/bootstrap, and then create your stylesheets, javascripts and other assets there.

Helpers

Themify provides two convenient helpers for your views: theme_url() and theme_secure_url(), which will return the path to your current theme assets folder.

<link rel="stylesheet" type="text/css" href="{{ theme_url() }}/css/styles.css">
<script src="{{ theme_url() }}/js/main.min.js"></script>

Examples

Setting a theme for a group of routes

<?php

Route::filter('admin.theme', function()
{
    // We use default() so we can 
    // override later if we want
    Themify::default('admin')
});

Route::group(['prefix' => 'admin', 'before' => 'admin.theme'], function()
{
    // All of these routes will use
    // 'admin' theme

    // Override this route with a
    // different theme
    Route::get('login', function() {
        Themify::set('basic');
    });
});

Setting a theme for all controller actions

You can define your theme in a per controller basis, using a public $theme property inside your controllers:

<?php

class FooController extends BaseController {

    public $theme = 'footheme';

    public function someAction()
    {
        // For this one, use a different theme
        Themify::set('bartheme');
    }

}