Coder Social home page Coder Social logo

theme's Introduction

Theme support for Laravel

Inspired by bigecko/laravel-theme. Themes are stored inside default laravel's resources folder

Introduction

This package provides a simple way to manage themes in Laravel applications.

For example, you can develop multiple themes for your application and easily switch between themes for different purposes.

Requirements

This version requires PHP 8.1 and supports Laravel 10-11.

This package also provides support for Laravel Mix and Vite configurations.

Themes L5.5 L5.6 L5.7 L5.8 L6 L7 L8 L9 L10 L11
2.4
3.0
4.1
5.0
5.1

Installation

To get the latest version, simply require the project using Composer:

composer require "yaap/theme:^5.0"

or manually add line to composer.json

{
    "require": {
        "yaap/theme": "^5.0"
    }
}

Optionally, publish config using artisan CLI (if you want to overwrite default config).

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

Configuration

Package config

Config in config/theme.php file.

return [

    /*
    |--------------------------------------------------------------------------
    | Path to directory with themes
    |--------------------------------------------------------------------------
    |
    | The directory with your themes.
    |
    */

    'path' => base_path('themes'),

    /*
    |--------------------------------------------------------------------------
    | Path to directory with assets build
    |--------------------------------------------------------------------------
    |
    | The directory with assets build in public directory.
    |
    */

    'assets_path' => 'themes',

    /*
    |--------------------------------------------------------------------------
    | A pieces of theme collections
    |--------------------------------------------------------------------------
    |
    | Inside a theme path we need to set up directories to
    | keep "layouts", "assets" and "partials".
    |
    */

    'containerDir' => [
        'assets' => 'assets',
        'lang' => 'lang',
        'layout' => 'views/layouts',
        'partial' => 'views/partials',
        'view' => 'views',
    ],
];

Theme config

Config in theme folder. Placeholder %theme_name% will be replaced with theme name on creation.

return [

    /*
    |--------------------------------------------------------------------------
    | Theme name
    |--------------------------------------------------------------------------
    |
    | Use in assets publishing etc.
    |
    */

    'name' => '%theme_name%',

    /*
    |--------------------------------------------------------------------------
    | Inherit from another theme
    |--------------------------------------------------------------------------
    |
    | Set up inherit from another if the file is not exists.
    |
    */

    'inherit' => null,

];

Usage

Create theme with artisan CLI

Create command

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

php artisan theme:create default

By default, it will use vite as assets builder. If you want to use laravel mix instead, use the command:

or with laravel mix:

php artisan theme:create default mix

Destroy command

To delete an existing theme, use the command:

php artisan theme:destroy default

Structure

Here is an example of the folder structure of project with theme

project-root
├── app/
<...>
├── public/
|   ├── index.php
|   └── themes/
|       └── default/
|           ├── js/
|           |   └── app.js
|           ├── css/
|           |   └── styles.css
|           └── images/
|               └── icon.png
├── resources/
<...>
├── themes/
|   ├── default/
|   |   ├── assets/        
|   |   ├── lang/        
|   |   ├── views/
|   |   |   ├── layouts/
|   |   |   ├── partials/
|   |   |   └── hello.blade.php
|   |   └── config.php
|   ├── admin/
|   ├── views/
|       ├── emails/
|       |   └── notify.blade.php
|       └── hello.blade.php

Init theme

To init and use theme in your application, add the following code to any boot method in application service provider (e.g. AppServiceProvider):

use YAAP\Theme\Facades\ThemeLoader;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        ThemeLoader::init('default');
    }
}

This will add to views find path:

  • themes/{$name}/views

Lang files will be added as well:

  • themes/{$name}/lang

Making view

Laravel: Creating & Rendering Views

View::make('hello');
View::make('emails.notify');

// or

view('hello');
view('emails.notify');

Assets

Vite

If you use Vite, ensure vite.config.js have specified the input path for theme

import {defineConfig} from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'themes/default/assets/js/app.js', // for default theme
                // ...
            ],
            refresh: true,
        }),
    ],
});

Because app.js includes app.scss you can use the following code to include assets in your views:

<head>
    <!--...-->
    @vite([
        'themes/default/assets/js/app.js',
    ])
</head>

Laravel Mix

If you use Laravel Mix, ensure webpack.mix.js have specified mix configuration for theme

const mix = require('laravel-mix');

mix.disableNotifications();
mix.browserSync({
    open: true,
    proxy: 'localhost:8000',
    files: [
        'app/**/*',
        'routes/**/*', 
        'themes/**/*', // manually add this line
    ]
});

// other configutraions...

// mix for default theme
mix.copyDirectory('themes/default/assets/img', 'public/themes/default/img');
mix.copyDirectory('themes/default/assets/fonts', 'public/themes/default/fonts');
// js
mix.js(['themes/default/assets/js/app.js'], 'public/themes/default/js/app.min.js')
// sass
mix.sass('themes/default/assets/sass/app.scss', 'public/themes/default/css/app.min.css')

Then you can use the following code to include assets in your views:

in the <head> tag

<head>
    <!--...-->
    <link rel="stylesheet" href="{{ mix('/themes/default/css/app.min.css') }}"/>
</head>

and before </body> tag

<body>
    <!--...-->
    <script type="text/javascript" src="{{ mix('/themes/default/js/app.min.js') }}"></script>
</body>

To use images, you can use the following code:

<img src="{{ mix('themes/default/img/icon.png') }}" alt="icon">

Building layouts

Layouts using Components

Laravel: Blade Components

Layouts using template inheritance

To build layouts we use template inheritance. You can use @extends directive to specify a parent layout.

@extends('layouts.master')

@include('partials.header')

@section('content')
    <section id="main">
        <h1>HOME</h1>
    </section>
@stop

@include('partials.footer')

Fallback capability

You still able to use default View::make('emails.notify') which is stored outside the themes directory.

Can I hire you guys?

Yes! Say hi: [email protected]

We will be happy to work with you! Other work we’ve done

Follow us

Stay up to date with the latest news! Follow us on LinkedIn or Facebook

License

MIT license.

theme's People

Contributors

oleksandr-moik avatar snipe avatar sroutier avatar terion-name avatar yaapis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

theme's Issues

5.4 Compatibility

Hi there - is there any plan to make this package compatible with Laravel 5.4? $this->app->share was removed from 5.4, and needs to instead use singletons. I can work on a PR for this if you'd be willing to accept it, and if you don't think you'll have time to work on it yourself.

Thanks!

Laravel 5.2?

Hi, I'm very interested in using yaapis Theme but there is an error white trying to install via composer ...

Problem 1
    - Conclusion: remove laravel/framework v5.2.6
    - Conclusion: don't install laravel/framework v5.2.6
    - Conclusion: don't install yaap/theme v2.1.1
    - Conclusion: don't install yaap/theme 2.1
    - Conclusion: don't install illuminate/support v5.0.33
    - Conclusion: don't install laravel/framework v5.2.5
    - Conclusion: don't install illuminate/support v5.0.28
    - Conclusion: don't install laravel/framework v5.2.4
    - Conclusion: don't install illuminate/support v5.0.26
    - Conclusion: don't install laravel/framework v5.2.3
    - Conclusion: don't install illuminate/support v5.0.25
    - Conclusion: don't install laravel/framework v5.2.2
    - Conclusion: don't install illuminate/support v5.0.22
    - Installation request for yaap/theme 2.* -> satisfiable by yaap/theme[2.0, 2.1, v2.1.1].
    - Conclusion: don't install laravel/framework v5.2.1
    - yaap/theme 2.0 requires illuminate/support 5.0.* -> satisfiable by illuminate/support[v5.0.0, v5.0.22, v5.0.25, v5.0.26, v5.0.28, v5.0.33, v5.0.4].
    - don't install illuminate/support v5.0.0|don't install laravel/framework v5.2.0
    - don't install illuminate/support v5.0.4|don't install laravel/framework v5.2.0
    - Installation request for laravel/framework 5.2.* -> satisfiable by laravel/framework[v5.2.0, v5.2.1, v5.2.2, v5.2.3, v5.2.4, v5.2.5, v5.2.6]

Maybe an update in yaapis/theme/composer.json to support laravel 5.2 would do the trick?

Cheers ;D

support twig

is this package support twig template engine ?

make custom auth login view

I make login view under /themes/theme-name/views/auth/login.blade.php

and activate Theme::init("theme-name") already

but view does not override

How can i do it?

Support IDE

After installing & using this package, PhpStorm can't go to the file directly by pressing the name of the views.

return view('pages.notifications.jobIndex', [ 'entries' => $entries ]);

Who knows how to fix this?

assetFullpath

Are you sure you used the right syntax ('theme::assets_path') ?

$assets_path = trim($this->app['config']->get('theme::assets_path', 'assets/themes'), '/');

problem with artisan optimize command

UPDATE
when I run

php artisan optimize --force -vvv                                                          

I see :

Generating optimized class loader
Compiling common classes
Compiling views



  [InvalidArgumentException]                                                      
  The "/var/www/project/app/views/themes/default/views" directory does not exist.  



Exception trace:
 () at /var/www/project/vendor/symfony/finder/Symfony/Component/Finder/Finder.php:677
 Symfony\Component\Finder\Finder->in() at /var/www/project/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php:272
 Illuminate\Filesystem\Filesystem->allFiles() at /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php:130
 Illuminate\Foundation\Console\OptimizeCommand->compileViews() at /var/www/project/vendor/laravel/framework/src/Illuminate/Foundation/Console/OptimizeCommand.php:71
 Illuminate\Foundation\Console\OptimizeCommand->fire() at /var/www/project/vendor/laravel/framework/src/Illuminate/Console/Command.php:112
 Illuminate\Console\Command->execute() at /var/www/project/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:253
 Symfony\Component\Console\Command\Command->run() at /var/www/project/vendor/laravel/framework/src/Illuminate/Console/Command.php:100
 Illuminate\Console\Command->run() at /var/www/project/vendor/symfony/console/Symfony/Component/Console/Application.php:889
 Symfony\Component\Console\Application->doRunCommand() at /var/www/project/vendor/symfony/console/Symfony/Component/Console/Application.php:193
 Symfony\Component\Console\Application->doRun() at /var/www/project/vendor/symfony/console/Symfony/Component/Console/Application.php:124
 Symfony\Component\Console\Application->run() at /var/www/project/artisan:59


optimize [--force] [--psr]

this is my laravel version

Laravel Framework version 4.2.17

and this my theme config

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Theme name
    |--------------------------------------------------------------------------
    |
    | Use in assets publishing etc
    |
    */

    'name' => 'default',

    /*
    |--------------------------------------------------------------------------
    | Inherit from another theme
    |--------------------------------------------------------------------------
    |
    | Set up inherit from another if the file is not exists
    |
    */

    'inherit' => null,


);

this is my directory structure for the app/views directory

app/views
└── themes
    └── default
        ├── auth
        ├── config.php
        ├── emails
        ├── hello.blade.php
        ├── layouts
        ├── menu
        ├── partials
        └── users

the package config:

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Path to directory with themes
    |--------------------------------------------------------------------------
    |
    | The directory with your themes.
    |
    */

        'path'         => app_path('views/themes'),
    /*
    |--------------------------------------------------------------------------
    | Path to directory with assets
    |--------------------------------------------------------------------------
    |
    | The directory with assets.
    |
    */

        'assets_path'  => 'assets/themes',
    /*
    |--------------------------------------------------------------------------
    | A pieces of theme collections
    |--------------------------------------------------------------------------
    |
    | Inside a theme path we need to set up directories to
    | keep "layouts", "assets" and "partials".
    |
    */

        'containerDir' => array(
            'layout'  => 'layouts',
            'partial' => 'partials',
            'view'    => '',
        ),


);

the last thing I will need to tell you about is I set my

Theme::init('default');

in
app/start/globals.php

Unused params in theme initialisation

My config:
'containerDir' => [
'layout' => 'layouts',
'partial' => 'partials',
'view' => 'templates', // <-- as you can see I changed default directory placement
]

But in the function 'Init' you continue to use default 'view' directory:
$this->finder->addLocation($path . '/' . $theme . '/views');

I suppose you have to change it to:
config('theme.containerDir.view')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.