Skip to content

jitheshgopan/laravel-theme

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Description

Laravel License Downloads

This is a package for the Laravel 5 Framework that adds basic support for managing themes. It allows you to seperate your views & your assets files in seperate folders, and supports for theme extending! Awesome :)

Features:

  • Views & Asset seperation in theme folders
  • Theme inheritence: Extend any theme and create Theme hierarcies (WordPress style!)
  • Intergrates Orchestra/Asset to provide Asset dependencies managment
  • Your App & Views remain theme-agnostic. Include new themes with (almost) no modifications

How it works

Very simple, you create a folder for each Theme in 'resources/views' and keep all your views seperated. The same goes for assets: create a folder for each theme in your 'public' directory. Set your active theme and you are done. The rest of your application remains theme-agnostic©, which means that when you View::make('index') you will access the index.blade.php from your selected theme's folder. Same goes for your assets.

Installation

Edit your project's composer.json file to require:

"require": {
    "igaster/laravel-theme": "~1.0"
}

and install with composer update

Add the service provider in app/config/app.php, Providers array:

'igaster\laravelTheme\themeServiceProvider',

also edit the Facades array and add:

'Theme'  => 'igaster\laravelTheme\Facades\Theme',

Almost Done. You only need to publish configuration file to your application with

artisan vendor:publish

That's it. You are now ready to start theming your applications!

Defining themes

Simple define your themes in the themes array in config/themes.php. The format for every theme is very simple:

// Select a name for your theme
'theme-name' => [

    // Theme to extend
    // Defaults to null (=none)
    'extends'	 	=> 'theme-to-extend',

    // The path where the view are stored
    // Defaults to 'theme-name' 
    // It is relative to /resources/views (or whatever is defined in config/view.php)
    'views-path' 	=> 'path-to-views',

    // The path where the assets are stored
    // Defaults to 'theme-name' 
    // It is relative to laravels public folder (/public)
    'asset-path' 	=> 'path-to-assets',   // defaults to: theme-name

    // you can add your own custom keys and retrieve them with Theme::config('key'). e.g.:
    'parameter'        => 'value', 
],

all settings are optional and can be ommited. Check the example in the configuration file... If you are OK with the defaults then you don't even have to touch the configuration file. If a theme is not found then the default values will be used (Convention over configuration)

Extending themes

You can set a theme to extend an other. When you are requesting a view/asset that doesn't exist in your active theme, then it will be resolved from it's parent theme. You can easily create variations of your theme by simply overiding your views/themes that are different.

All themes fall back to the default laravel folders if a resource is not found on the theme folders. So for example you can leave your common libraries (jquery/bootstrap ...) in your public folder and use them from all themes. No need to dublicate common assets for each theme!

Working with Themes

The default theme can be configured in the theme.php configuration file. Working with themes is very straightforward. Use:

Theme::set('theme-name');    // switch to 'theme-name'
Theme::get();                // retrieve current theme's name
Theme::config('key');        // read current theme's configuration value for 'key'

For example this is a Service Provider that will select a different theme for the /admin/xxx urls:

class themeSelectServiceProvider extends ServiceProvider {

    public function boot()
    {
        if (\Request::segment(1)=='admin')
            \Theme::set('adminTheme');
    }

}

...or you can use a middleware to apply a theme to a Route::group() etc. For more advanced example check demo application: Set Theme in Session

Building your views

Whenever you need the url of a local file (image/css/js etc) you can retrieve its path with:

Theme::url('path-to-file')

The path is relative to Theme Folder (NOT to public!). For example, if you have placed an image in public/theme-name/img/logo.png your Blade code would be:

<img src="{{Theme::url('img/logo.png')}}">

When you are refering to a local file it will be looked-up in the current theme hierarcy, and the correct path will be returned. If the file is not found on the current theme or its parents then you can define in the configuration file the action that will be carried out: THROW_EXCEPTION | LOG_ERROR as warning (Default) | IGNORE completly.

Some usefull helpers you can use:

Theme::js('file-name')
Theme::css('file-name')
Theme::img('src', 'alt', 'class-name')

Paremeters in filenames

You can include any configuration key of the current theme inside any path string using {curly brackets}. For examle:

Theme::url('main-{version}.css')

if there is a "version" key defined in the theme's configuration it will be evaluated and then the filename will be looked-up in the theme hierarcy. (e.g: many comercial themes ship with multiple versions of the main.css for different color-schemes)

Assets Managment (Optional)

This package provides intergration with Orchestra/Asset component. All the features are explained in the official documentation. If you don't need the extra functinality you can skip this section. Orchestra/Asset is NOT installed along with this package - you have to install it manualy.

To install Orchestra\Asset you must add it in your composer.json (see the Official Documentation) and then add in your Providers array:

'Orchestra\Asset\AssetServiceProvider',
'Orchestra\Html\HtmlServiceProvider',

Add the Asset facade in your Facades array in app/config/app.php

'Asset' => 'Orchestra\Support\Facades\Asset',

Now you can leverage all the power of Orchestra\Asset package. However the syntax can become quite cumbersome when you are using Themes + Orchestra/Asset, so some Blade-specific sugar has been added to ease your work. Here how to build your views:

In any blade file you can require a script or a css:

@css('filename')
@js('filename')

Please note that you are just defining your css/js files but not actually dumping them in html. Usually you only need write your css/js decleration in one place on the Head/Footer of you page. So open your master layout and place:

{!! Asset::styles() !!}
{!! Asset::scripts() !!}

exactly where you want write your declerations.

Assets dependencies

This is an Orchestra/Asset feature explained well in the official documentation. Long story short:

@css ('filename', 'alias', 'depends-on')
@js  ('filename', 'alias', 'depends-on')

and your assets dependencies will be auto resolved. Your assets will be exported in the correct order. The biggest benefit of this approach is that you don't have to move all your declerations in your master layout file. Each sub-view can define it's requirements and they will auto-resolved in the correct order with no doublications. Awesome! A short example:

@js  ('jquery.js',    'jquery')
@js  ('bootstrap.js', 'bootsrap', jquery)

FAQ:

Is this package compatible with AWS?

Yes with one exception: If you are building Theme hierarcies, asset's will not be looked up on the parent theme. Performing file searching on a remote repository is not the best practice. Should be addressed in a future version... However Blade templates auto-discovery works fine since they are local files.

What about external assets (eg CDN)?

Link directly to your external assets. Every url that starts with http(s) will not be proccesed by default.

How do I change the public path?

Rebind Laravel's 'path.public'. (More info)

I'm editing a view but I dont see the changes

Laravel is compiling your views every-time you make an edit. A compiled view will not recompile unless you make any edit to your view. Keep this in mind while you are developing themes...

About

Theme support for Laravel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%