在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):FaCuZ/laravel-theme开源软件地址(OpenSource Url):https://github.com/FaCuZ/laravel-theme开源编程语言(OpenSource Language):PHP 98.6%开源软件介绍(OpenSource Introduction):Theme Management for LaravelLaravel-Theme is a theme management for Laravel 5+ (last check 6.3), it is the easiest way to organize your skins, layouts and assets. This package is based on teepluss\theme
UsageTheme has many features to help you get started with Laravel
InstallationTo get the latest version of laravel-themes simply require it in your "facuz/laravel-themes": "^3.2" You'll then need to run Once Theme is installed you need to register the service provider with the application. Open up 'providers' => [
...
Facuz\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' => [
...
'Theme' => Facuz\Theme\Facades\Theme::class,
] Publish config using artisan CLI.
It's recommended to add to the
Create new themeThe first time you have to create theme "default" structure, using the artisan command:
This will create the following directory structure:
To delete an existing theme, use the command:
If you want to list all installed themes use the command:
You can duplicate an existing theme:
Create from the application without CLI. Artisan::call('theme:create', ['name' => 'foo']); Basic usageTo display a view from the controller: namespace App\Http\Controllers;
use Theme;
class HomeController extends Controller {
public function getIndex()
{
return Theme::view('index');
}
...
}
You can add data or define the theme and layout: ...
Theme::uses('themename');
$data['info'] = 'Hello World';
return Theme::view('index', $data);
... Or you can do: $cookie = Cookie::make('name', 'Tee');
return Theme::view([
'view' => 'index',
'theme' => 'default',
'layout' => 'layout',
'statusCode' => 200,
'cookie' => $cookie,
'args' => $data
]);
To check whether a theme exists. Theme::exists('themename'); Each theme must come supplied with a manifest file {
"slug": "default",
"name": "Default",
"author": "John Doe",
"email": "[email protected]",
"description": "This is an example theme.",
"web": "www.example.com",
"license": "MIT",
"version": "1.0"
} The manifest file can store any property that you'd like. These values can be retrieved and even set through a couple helper methods: // Get all: (array)
Theme::info();
// Get:
Theme::info("property");
// Set:
Theme::info("property", "new data"); Other ways to display a view:$theme = Theme::uses('default')->layout('mobile');
$data = ['info' => 'Hello World']; // It will look up the path 'resources/views/home/index.php':
return $theme->of('home.index', $data)->render(); // Specific status code with render:
return $theme->of('home.index', $data)->render(200); // It will look up the path 'resources/views/mobile/home/index.php':
return $theme->ofWithLayout('home.index', $data)->render(); // It will look up the path 'public/themes/default/views/home/index.php':
return $theme->scope('home.index', $data)->render(); // It will look up the path 'public/themes/default/views/mobile/home/index.php':
return $theme->scopeWithLayout('home.index', $data)->render(); // Looking for a custom path:
return $theme->load('app.somewhere.viewfile', $data)->render(); // Working with cookie:
$cookie = Cookie::make('name', 'Tee');
return $theme->of('home.index', $data)->withCookie($cookie)->render(); // Get only content:
return $theme->of('home.index')->content(); Finding from both theme's view and application's view: $theme = Theme::uses('default')->layout('default');
return $theme->watch('home.index')->render(); To find the location of a view: $which = $theme->scope('home.index')->location();
echo $which; // theme::views.home.index
$which = $theme->scope('home.index')->location(true);
echo $which; // ./public/themes/name/views/home/index.blade.php Render from string:return $theme->string('<h1>{{ $name }}</h1>', ['name' => 'Teepluss'], 'blade')->render(); Compile string: $template = `<h1>Name: {{ $name }}</h1>
<p>
{{ Theme::widget("WidgetIntro", ["title" => "Demo Widget"])->render() }}
</p>`;
echo Theme::blader($template, ['name' => 'Teepluss']); Symlink from another viewThis 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:
Theme::symlink('a');
// Location: public/themes/b/views/welcome.blade.php ConfigurationAfter the config is published, you will see a global config file The config is convenient for setting up basic CSS/JS, partial composer, breadcrumb template and also metas. 'events' => [
/*
* 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 lines anytime.
$theme->setTitle('Title Example');
$theme->setAuthor('John Doe');
$theme->setKeywords('Example, Web');
// Breadcrumb template.
$theme->breadcrumb()->setTemplate(`
<ul class="breadcrumb">
@foreach($crumbs as $i => $crumb)
@if($i != (count($crumbs) - 1))
<li>
<a href="{{ $crumb["url"] }}">{{ $crumb["label"] }}</a>
<span class="divider">/</span>
</li>
@else
<li class="active">{{ $crumb["label"] }}</li>
@endif
@endforeach
</ul>
`);
},
/*
* Listen on event before render a theme, this
* event should call to assign some assets.
*/
'asset' => function($asset)
{
$asset->themePath()->add([
['style', 'css/style.css'],
['script', 'js/script.js']
]);
// You may use elixir to concat styles and scripts.
$asset->themePath()->add([
['styles', 'dist/css/styles.css'],
['scripts', 'dist/js/scripts.js']
]);
// Or you may use this event to set up your assets.
$asset->themePath()->add('core', 'core.js');
$asset->add([
['jquery', 'vendor/jquery/jquery.min.js'],
['jquery-ui', 'vendor/jqueryui/jquery-ui.min.js', ['jquery']]
]);
},
/*
* Listen on event before render a theme, this event should
* call to assign some partials or breadcrumb template.
*/
'beforeRenderTheme' => function($theme)
{
$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' => [
'mobile' => function($theme){
$theme->asset()->usePath()->add('ipad', 'css/layouts/ipad.css');
}
]
]; Basic usage of assetsYou can add assets on the $asset->add('core-style', 'css/style.css');
// path: public/css/style.css
$asset->container('footer')->add('core-script', 'js/script.js');
// path: public/js/script.css
$asset->themePath()->add('custom', 'css/custom.css', ['core-style']);
// path: public/themes/[current-theme]/assets/css/custom.css
// This case has dependency with "core-style".
$asset->container('footer')->themePath()->add('custom', 'js/custom.js', array('core-script'));
// path: public/themes/[current theme]/assets/js/custom.js
// This case has dependency with "core-script".
Writing in-line style or script: // Dependency with.
$dependencies = [];
// Writing an in-line script.
$asset->writeScript('inline-script', '
$(function() {
console.log("Running");
})', $dependencies);
// Writing an in-line style.
$asset->writeStyle('inline-style', 'h1{ font-size: 0.9em; }', $dependencies);
// Writing an in-line script, style without tag wrapper.
$asset->writeContent('custom-inline-script', '
<script>
$(function() {
console.log("Running");
});
</script>', $dependencies); Render styles and scripts in your blade layout: // Without container
@styles()
// With "footer" container
@scripts('footer')
// Get a specific path from the asset folder
@asset('img/image.png')
or a more complex way: {!! Theme::asset()->styles() !!}
{!! Theme::asset()->container('footer')->scripts() !!} Direct path to theme asset: {!! Theme::asset()->url('img/image.png') !!} 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 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: <html>
<head>
@styles()
@styles('your-container')
</head>
<body>
...
@scripts()
@scripts('your-container')
</body>
<html> PartialsRender a partial in your layouts or views: @partial('header', ['title' => 'Header']);
Partial with current layout specific: Theme::partialWithLayout('header', ['title' => 'Header']);
Finding from both theme's partial and application's partials: Theme::watchPartial('header', ['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'); SectionsThe @sections('main') It's the same as: @partial('sections.main') Magic methodsMagic methods allow you to set, prepend and append anything. $theme->setTitle('Your title');
$theme->appendTitle('Your appended title');
$theme->prependTitle('Hello: ....');
$theme->setAnything('anything');
$theme->setFoo('foo');
$theme->set('foo', 'foo'); Render in your blade layout or view: @get('foo')
@get('foo', 'Default msj')
Theme::getAnything();
Theme::getFoo();
Theme::get('foo', 'Default msj'); Check if the place exists or not:@getIfHas('title') It's the same as: @if(Theme::has('title'))
{{ Theme::get('title') }}
@endif @if(Theme::hasTitle())
{{ Theme::getTitle() }}
@endif Get argument assigned to content in layout or region: Theme::getContentArguments();
Theme::getContentArgument('name'); To check if it exists: Theme::hasContentArgument('name');
Preparing data to viewSometimes you don't need to execute heavy processing, so you can prepare and use when you need it. $theme->bind('something', function()
{
return 'This is bound parameter.';
}); Using bound data on view: echo Theme::bind('something'); BreadcrumbIn order to use breadcrumbs, follow the instruction below: $theme->breadcrumb()->add('label', 'http://...')->add('label2', 'http:...');
// or
$theme->breadcrumb()->add([[
'label' => 'label1',
'url' => 'http://...'
],[
'label' => 'label2',
'url' => 'http://...'
]]); To render breadcrumbs: {!! $theme->breadcrumb()->render() !!} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论