PHPackages                             appengine-afa/theme - 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. appengine-afa/theme

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

appengine-afa/theme
===================

This is an override of the teepluss/laravel-theme package, as the latest version of that package does not support Laravel &gt;=5.4

1.0(8y ago)016MITPHPPHP &gt;=5.4.0

Since Nov 14Pushed 8y agoCompare

[ Source](https://github.com/appengine-afa/theme)[ Packagist](https://packagist.org/packages/appengine-afa/theme)[ Docs](https://github.com/appengine-afa/theme)[ RSS](/packages/appengine-afa-theme/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Theme Management for Laravel
----------------------------

[](#theme-management-for-laravel)

### Installation

[](#installation)

- See [Theme on GitHub](https://github.com/teepluss/laravel-theme)

To get the latest version of Theme simply require it in your `composer.json` file.

```
"appengine-afa/theme": "^1.0"

```

You'll then need to run `composer install` to download it and have the autoloader updated.

Once Theme is installed you need to register the service provider with the application. Open up `config/app.php` and find the `providers` key.

```
'providers' => [

    AppEngineAFA\Theme\ThemeServiceProvider::class,

]

```

Theme also ships with a facade which provides the static syntax for creating collections. You can register the facade in the `aliases` key of your `config/app.php` file.

```
'aliases' => [

    'Theme' => AppEngineAFA\Theme\Facades\Theme::class,

]

```

Publish config using artisan CLI.

```
php artisan vendor:publish --provider="AppEngineAFA\Theme\ThemeServiceProvider"

```

Usage
-----

[](#usage)

Theme has many features to help you get started with Laravel

- [Create theme with artisan CLI](#create-theme-with-artisan-cli)
- [Configuration](#configuration)
- [Basic usage](#basic-usage)
- [Compiler](#compiler)
- [Render from string](#render-from-string)
- [Compile string](#compile-string)
- [Symlink from another view](#symlink-from-another-view)
- [Basic usage of assets](#basic-usage-of-assets)
- [Preparing group of assets](#preparing-group-of-assets)
- [Partials](#partials)
- [Working with regions](#working-with-regions)
- [Preparing data to view](#preparing-data-to-view)
- [Breadcrumb](#breadcrumb)
- [Widgets design structure](#widgets-design-structure)
- [Using theme global](#using-theme-global)

### Create theme with artisan CLI

[](#create-theme-with-artisan-cli)

The first time you have to create theme "default" structure, using the artisan command:

```
php artisan theme:create default

```

> If you change the facade name you can add an option --facade="Alias".

To delete an existing theme, use the command:

```
php artisan theme:destroy default

```

> the type can be php, blade and twig.

Create from the applicaton without CLI.

```
Artisan::call('theme:create', ['name' => 'foo', '--type' => 'blade']);
```

### Configuration

[](#configuration)

After the config is published, you will see the config file in "config/theme", but all the configuration can be replaced by a config file inside a theme.

> Theme config location: /public/themes/\[theme\]/config.php

The config is convenient for setting up basic CSS/JS, partial composer, breadcrumb template and also metas.

Example:

```
'events' => array(

    // Before event inherit from package config and the theme that call before,
    // you can use this event to set meta, breadcrumb template or anything
    // you want inheriting.
    'before' => function($theme)
    {
        // You can remove this line anytime.
        $theme->setTitle('Copyright ©  2013 - Laravel.in.th');

        // Breadcrumb template.
        // $theme->breadcrumb()->setTemplate('
        //
        //     @foreach ($crumbs as $i => $crumb)
        //         @if ($i != (count($crumbs) - 1))
        //         {{ $crumb["label"] }}/
        //         @else
        //         {{ $crumb["label"] }}
        //         @endif
        //     @endforeach
        //
        // ');
    },

    // Listen on event before render a theme,
    // this event should call to assign some assets,
    // breadcrumb template.
    'beforeRenderTheme' => function($theme)
    {
        // You may use this event to set up your assets.
        // $theme->asset()->usePath()->add('core', 'core.js');
        // $theme->asset()->add('jquery', 'vendor/jquery/jquery.min.js');
        // $theme->asset()->add('jquery-ui', 'vendor/jqueryui/jquery-ui.min.js', array('jquery'));

        // $theme->partialComposer('header', function($view)
        // {
        //     $view->with('auth', Auth::user());
        // });
    },

    // Listen on event before render a layout,
    // this should call to assign style, script for a layout.
    'beforeRenderLayout' => array(

        'default' => function($theme)
        {
            // $theme->asset()->usePath()->add('ipad', 'css/layouts/ipad.css');
        }

    )

)
```

### Basic usage

[](#basic-usage)

```
namespace App\Http\Controllers;

use Theme;

class HomeController extends Controller {

    public function getIndex()
    {
        $theme = Theme::uses('default')->layout('mobile');

        $view = array(
            'name' => 'AppEngineAFA'
        );

        // home.index will look up the path 'resources/views/home/index.php'
        return $theme->of('home.index', $view)->render();

        // Specific status code with render.
        return $theme->of('home.index', $view)->render(200);

        // home.index will look up the path 'resources/views/mobile/home/index.php'
        return $theme->ofWithLayout('home.index', $view)->render();

        // home.index will look up the path 'public/themes/default/views/home/index.php'
        return $theme->scope('home.index', $view)->render();

        // home.index will look up the path 'public/themes/default/views/mobile/home/index.php'
        return $theme->scopeWithLayout('home.index', $view)->render();

        // Looking for a custom path.
        return $theme->load('app.somewhere.viewfile', $view)->render();

        // Working with cookie.
        $cookie = Cookie::make('name', 'Tee');
        return $theme->of('home.index', $view)->withCookie($cookie)->render();
    }

}
```

> Get only content "$theme-&gt;of('home.index')-&gt;content();".

Finding from both theme's view and application's view.

```
$theme = Theme::uses('default')->layout('default');

return $theme->watch('home.index')->render();
```

To check whether a theme exists.

```
// Returns boolean.
Theme::exists('themename');
```

To find the location of a view.

```
$which = $theme->scope('home.index')->location();

echo $which; // themer::views.home.index

$which = $theme->scope('home.index')->location(true);

echo $which; // ./public/themes/name/views/home/index.blade.php
```

### Compiler

[](#compiler)

Theme now supports PHP, Blade and Twig. To use Blade or Twig template you just create a file with extension

```
[file].blade.php or [file].twig.php

```

### Render from string.

[](#render-from-string)

```
// Blade template.
return $theme->string('{{ $name }}', array('name' => 'AppEngineAFA'), 'blade')->render();

// Twig Template
return $theme->string('{{ name }}', array('name' => 'AppEngineAFA'), 'twig')->render();
```

### Compile string

[](#compile-string)

```
// Blade compile.
$template = 'Name: {{ $name }}{{ Theme::widget("WidgetIntro", array("userId" => 9999, "title" => "Demo Widget"))->render() }}';

echo Theme::blader($template, array('name' => 'AppEngineAFA'));
```

```
// Twig compile.
$template = 'Name: {{ name }}{{ Theme.widget("WidgetIntro", {"userId" : 9999, "title" : "Demo Widget"}).render() }}';

echo Theme::twigy($template, array('name' => 'AppEngineAFA'));
```

### Symlink from another view

[](#symlink-from-another-view)

This is a nice feature when you have multiple files that have the same name, but need to be located as a separate one.

```
// Theme A : /public/themes/a/views/welcome.blade.php

// Theme B : /public/themes/b/views/welcome.blade.php

// File welcome.blade.php at Theme B is the same as Theme A, so you can do link below:

// ................

// Location: public/themes/b/views/welcome.blade.php
Theme::symlink('a');

// That's it!
```

### Basic usage of assets

[](#basic-usage-of-assets)

Add assets in your route.

```
// path: public/css/style.css
$theme->asset()->add('core-style', 'css/style.css');

// path: public/js/script.css
$theme->asset()->container('footer')->add('core-script', 'js/script.js');

// path: public/themes/[current theme]/assets/css/custom.css
// This case has dependency with "core-style".
$theme->asset()->usePath()->add('custom', 'css/custom.css', array('core-style'));

// path: public/themes/[current theme]/assets/js/custom.js
// This case has dependency with "core-script".
$theme->asset()->container('footer')->usePath()->add('custom', 'js/custom.js', array('core-script'));
```

> You can force use theme to look up existing theme by passing parameter to method: $theme-&gt;asset()-&gt;usePath('default')

Writing in-line style or script.

```
// Dependency with.
$dependencies = array();

// Writing an in-line script.
$theme->asset()->writeScript('inline-script', '
    $(function() {
        console.log("Running");
    })
', $dependencies);

// Writing an in-line style.
$theme->asset()->writeStyle('inline-style', '
    h1 { font-size: 0.9em; }
', $dependencies);

// Writing an in-line script, style without tag wrapper.
$theme->asset()->writeContent('custom-inline-script', '

        $(function() {
            console.log("Running");
        });

', $dependencies);
```

Render styles and scripts in your layout.

```
// Without container
echo Theme::asset()->styles();

// With "footer" container
echo Theme::asset()->container('footer')->scripts();
```

Direct path to theme asset.

```
echo Theme::asset()->url('img/image.png');
```

### Preparing group of assets.

[](#preparing-group-of-assets)

Some assets you don't want to add on a page right now, but you still need them sometimes, so "cook" and "serve" is your magic.

Cook your assets.

```
Theme::asset()->cook('backbone', function($asset)
{
    $asset->add('backbone', '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js');
    $asset->add('underscorejs', '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js');
});
```

You can prepare on a global in package config.

```
// Location: config/theme/config.php
....
    'events' => array(

        ....

        // This event will fire as a global you can add any assets you want here.
        'asset' => function($asset)
        {
            // Preparing asset you need to serve after.
            $asset->cook('backbone', function($asset)
            {
                $asset->add('backbone', '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js');
                $asset->add('underscorejs', '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js');
            });
        }

    )
....
```

Serve theme when you need.

```
// At the controller.
Theme::asset()->serve('backbone');
```

Then you can get output.

```
...

    scripts(); ?>
    styles(); ?>
    container('YOUR_CONTAINER')->scripts(); ?>
    container('YOUR_CONTAINER')->styles(); ?>

...
```

### Partials

[](#partials)

Render a partial in your layouts or views.

```
// This will look up to "public/themes/[theme]/partials/header.php"
echo Theme::partial('header', array('title' => 'Header'));

// Partial with current layout specific.
// This will look up up to "public/themes/[theme]/partials/[CURRENT_LAYOUT]/header.php"
echo Theme::partialWithLayout('header', array('title' => 'Header'));
```

Finding from both theme's partial and application's partials.

```
echo Theme::watchPartial('header', array('title' => 'Header'));
```

Partial composer.

```
$theme->partialComposer('header', function($view)
{
    $view->with('key', 'value');
});

// Working with partialWithLayout.
$theme->partialComposer('header', function($view)
{
    $view->with('key', 'value');
}, 'layout-name');
```

### Working with regions.

[](#working-with-regions)

Theme has magic methods to set, prepend and append anything.

```
$theme->setTitle('Your title');

$theme->appendTitle('Your appended title');

$theme->prependTitle('Hello: ....');

$theme->setAnything('anything');

$theme->setFoo('foo');

// or

$theme->set('foo', 'foo');
```

Render in your layout or view.

```
Theme::getAnything();

Theme::getFoo();

// or use place.

Theme::place('anything');

Theme::place('foo', 'default-value-if-it-does-not-exist');

// or

Theme::get('foo');
```

Check if the place exists or not.

```
