teepluss / theme
主题可以帮助您轻松地在Laravel项目中组织主题,并维护主题相关的资产、布局和部分。
Requires
- php: >=5.4.0
- twig/twig: 1.x
Requires (Dev)
- illuminate/support: >=5.0.1
README
对于Laravel 4,请使用v1.x分支!
主题是Laravel 5的主题管理工具,是组织皮肤、布局和资产的最简单方式。目前主题支持PHP、Blade和Twig。
安装
要获取Theme的最新版本,只需在您的composer.json
文件中引入它。
"teepluss/theme": "^2.0"
然后您需要运行composer install
来下载它并更新自动加载器。
一旦安装了Theme,您需要将服务提供者注册到应用程序中。打开config/app.php
并找到providers
键。
'providers' => [
Teepluss\Theme\ThemeServiceProvider::class,
]
Theme还提供了一种门面,它提供了创建集合的静态语法。您可以在config/app.php
文件的aliases
键中注册门面。
'aliases' => [
'Theme' => Teepluss\Theme\Facades\Theme::class,
]
使用Artisan CLI发布配置。
php artisan vendor:publish --provider="Teepluss\Theme\ThemeServiceProvider"
使用方法
Theme具有许多功能,可以帮助您开始使用Laravel
- 使用Artisan CLI创建主题
- 配置
- 基本用法
- 编译器
- 从字符串渲染
- 编译字符串
- 从另一个视图创建符号链接
- 资产的基本用法
- 准备资产组
- 部分
- 处理区域
- 准备要查看的数据
- 面包屑
- 小部件设计结构
- 使用主题全局
使用Artisan CLI创建主题
第一次您需要使用Artisan命令创建“default”结构
php artisan theme:create default
如果您更改门面名称,可以添加选项 --facade="别名"。
要删除现有主题,使用以下命令
php artisan theme:destroy default
类型可以是php、blade和twig。
在不使用CLI的情况下从应用程序创建
Artisan::call('theme:create', ['name' => 'foo', '--type' => 'blade']);
配置
配置发布后,您将在“config/theme”中看到配置文件,但所有配置都可以由主题内部的一个配置文件替换。
主题配置位置:/public/themes/[主题]/config.php
配置方便设置基本的CSS/JS、部分composer、面包屑模板以及元数据。
示例
'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(' // <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, // 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'); } ) )
基本用法
namespace App\Http\Controllers; use Theme; class HomeController extends Controller { public function getIndex() { $theme = Theme::uses('default')->layout('mobile'); $view = array( 'name' => 'Teepluss' ); // 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(); } }
仅获取内容 "$theme->of('home.index')->content();".
从主题视图和应用视图查找。
$theme = Theme::uses('default')->layout('default'); return $theme->watch('home.index')->render();
检查主题是否存在。
// Returns boolean. Theme::exists('themename');
查找视图的位置。
$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
编译器
主题现在支持PHP、Blade和Twig。要使用Blade或Twig模板,只需创建一个扩展名为
[file].blade.php or [file].twig.php
从字符串渲染。
// Blade template. return $theme->string('<h1>{{ $name }}</h1>', array('name' => 'Teepluss'), 'blade')->render(); // Twig Template return $theme->string('<h1>{{ name }}</h1>', array('name' => 'Teepluss'), 'twig')->render();
编译字符串
// Blade compile. $template = '<h1>Name: {{ $name }}</h1><p>{{ Theme::widget("WidgetIntro", array("userId" => 9999, "title" => "Demo Widget"))->render() }}</p>'; echo Theme::blader($template, array('name' => 'Teepluss'));
// Twig compile. $template = '<h1>Name: {{ name }}</h1><p>{{ Theme.widget("WidgetIntro", {"userId" : 9999, "title" : "Demo Widget"}).render() }}</p>'; echo Theme::twigy($template, array('name' => 'Teepluss'));
从另一个视图创建符号链接
当您有多个文件具有相同的名称,但需要作为单独的一个定位时,这是一个很好的功能。
// 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!
资产的基本用法
在您的路由中添加资产。
// 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'));
您可以通过传递参数到方法:$theme->asset()->usePath('default')来强制使用主题查找现有主题
编写内联样式或脚本。
// 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', ' <script> $(function() { console.log("Running"); }); </script> ', $dependencies);
在布局中渲染样式和脚本。
// Without container echo Theme::asset()->styles(); // With "footer" container echo Theme::asset()->container('footer')->scripts();
直接访问主题资产路径。
echo Theme::asset()->url('img/image.png');
准备资产组。
有些资产您现在不想添加到页面上,但有时您仍然需要它们,所以“烹饪”和“服务”是您的魔法。
烹饪您的资产。
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'); });
您可以在包的配置中全局准备。
// 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'); }); } ) ....
在需要时服务主题。
// At the controller. Theme::asset()->serve('backbone');
然后您可以获取输出。
... <head> <?php echo Theme::asset()->scripts(); ?> <?php echo Theme::asset()->styles(); ?> <?php echo Theme::asset()->container('YOUR_CONTAINER')->scripts(); ?> <?php echo Theme::asset()->container('YOUR_CONTAINER')->styles(); ?> </head> ...
部分
在布局或视图中渲染部分。
// 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'));
从主题的局部和应用的局部中获取到的发现。
echo Theme::watchPartial('header', array('title' => 'Header'));
局部组合器。
$theme->partialComposer('header', function($view) { $view->with('key', 'value'); }); // Working with partialWithLayout. $theme->partialComposer('header', function($view) { $view->with('key', 'value'); }, 'layout-name');
与区域一起工作。
主题有设置、预添加和附加任何内容的魔法方法。
$theme->setTitle('Your title'); $theme->appendTitle('Your appended title'); $theme->prependTitle('Hello: ....'); $theme->setAnything('anything'); $theme->setFoo('foo'); // or $theme->set('foo', 'foo');
在您的布局或视图中渲染。
Theme::getAnything(); Theme::getFoo(); // or use place. Theme::place('anything'); Theme::place('foo', 'default-value-if-it-does-not-exist'); // or Theme::get('foo');
检查该位置是否存在。
<?php if (Theme::has('title')) : ?> <?php echo Theme::place('title'); ?> <?php endif; ?> // or <?php if (Theme::hasTitle()) : ?> <?php echo Theme::getTitle(); ?> <?php endif; ?>
获取分配给布局或区域中的内容的参数。
Theme::getContentArguments(); // or Theme::getContentArgument('name'); // To check if it exists Theme::hasContentArgument('name');
Theme::place('content')是一个用于渲染子视图的预留区域。
准备要查看的数据
有时您不需要执行重量级处理,因此您可以在需要时准备和使用它。
$theme->bind('something', function() { return 'This is bound parameter.'; });
在视图中使用绑定数据。
echo Theme::bind('something');
面包屑
为了使用面包屑,请按照以下说明操作
$theme->breadcrumb()->add('label', 'http://...')->add('label2', 'http:...'); // or $theme->breadcrumb()->add(array( array( 'label' => 'label1', 'url' => 'http://...' ), array( 'label' => 'label2', 'url' => 'http://...' ) ));
渲染面包屑。
echo $theme->breadcrumb()->render(); // or echo Theme::breadcrumb()->render();
您可以使用blade模板在任何位置设置和设置面包屑模板。
$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> ');
小部件设计结构
主题有许多称为"小部件"的有用功能,可以是任何东西。
创建小部件
您可以使用artisan命令创建小部件类
创建为全局。
php artisan theme:widget demo --global --type=blade
小部件tpl位于"resources/views/widgets/{widget-tpl}.{extension}"
创建特定主题名称。
php artisan theme:widget demo default --type=blade
小部件tpl位于"public/themes/[theme]/widgets/{widget-tpl}.{extension}"
文件名可以是demo.php、demo.blade.php或demo.twig.php
现在您将在/app/Widgets/WidgetDemo.php中看到一个小部件类
<h1>User Id: {{ $label }}</h1>
在布局或视图中调用您的 widget
echo Theme::widget('demo', array('label' => 'Demo Widget'))->render();
使用主题全局
use Teepluss\Theme\Contracts\Theme; use App\Http\Controllers\Controller; class BaseController extends Controller { /** * Theme instance. * * @var \Teepluss\Theme\Theme */ protected $theme; /** * Construct * * @return void */ public function __construct(Theme $theme) { // Using theme as a global. $this->theme = $theme->uses('default')->layout('ipad'); } }
要覆盖主题或布局。
public function getIndex() { $this->theme->uses('newone'); // or just override layout $this->theme->layout('desktop'); $this->theme->of('somewhere.index')->render(); }
支持或联系
如果您有任何问题,请联系teepluss@gmail.com