官方教程:https://packagist.org/packages/teepluss/theme

主题将帮助您轻松地在Laravel项目中组织主题,并在单个目录中维护主题的相关资产,布局和部分。

  • 安装:
    Packagist的主题
    GitHub上的主题

在应用主目录的composer.json里加入: “teepluss/theme”: “^2.0”

 "require": {"php": ">=5.5.9","laravel/framework": "5.1.*","barryvdh/laravel-ide-helper": "^2.4","doctrine/dbal": " ~2.3","teepluss/theme": "^2.0"},
  • 在项目根目录输入以下命令
composer require teepluss/theme
  • 安装主题后,您需要向应用程序注册服务提供商。打开config/app.php并找到providers钥匙。
'providers' => [Teepluss\Theme\ThemeServiceProvider::class,
]
  • 执行
composer install
  • 主题还附带了一个外观,它提供了创建集合的静态语法。您可以在文件的aliases密钥中注册外观config/app.php。
'aliases' => ['Theme' => Teepluss\Theme\Facades\Theme::class,]
  • 使用artisan CLI发布配置。
php artisan vendor:publish --provider="Teepluss\Theme\ThemeServiceProvider"
  • 发布 config/themes.php 配置文件, 配置文件在config/theme.php
php artisan vendor:publish --provider=Hongyukeji\\LaravelTheme\\ThemeServiceProvider
  • 使用:
// 1. 在 config/themes.php 中 添加 templates, 如:
'frontend' => [
'path_prefix' => 'resources/views/frontend/',
'template' => 'default',
'template_default' => 'default',
],
// 2. 在控制器中使用:
public function index()
{return view('frontend::index.index');
}

用法

主题有很多功能可以帮助您开始使用Laravel

  • 使用artisan CLI创建主题
    第一次使用artisan命令创建主题“默认”结构时:
php artisan theme:create default

如果更改外观名称,则可以添加选项–facade =“Alias”。
要删除现有主题,请使用以下命令:

php artisan theme:destroy default

无需CLI即可从应用程序创建。

Artisan :: call(' theme:create ',[ ' name '  =>  ' foo ',' - type '  =>  ' blade ' ]);
  • 组态
    发布配置后,您将在“config / theme”中看到配置文件,但所有配置都可以由主题内的配置文件替换。
主题配置位置:/public/themes/[theme]/config.php

配置方便设置基本的CSS/JS, partial composer, breadcrumb template以及元数据(metas)。
例:

//config/theme.php
'events' => array(// 事件之前从包配置和之前调用的主题中继承,//您可以使用此事件来设置meta、breadcrumb模板或任何你想要继承的东西'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>// ');},// 在渲染主题之前先监听听事件,// 此事件会被调用以分配一些资产 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());// });},// 在渲染布局前监听事件,//这会被调用未layout分配style,script'beforeRenderLayout' => array('default' => function($theme){// $theme->asset()->usePath()->add('ipad', 'css/layouts/ipad.css');}))
  • 基本用法
amespace App\Http\Controllers;use Theme;class HomeController extends Controller {public function getIndex(){//使用public/themes/default/views/layouts/manage.blade.php布局 $theme = Theme::uses('default')->layout('mobile');$view = array('name' => 'Teepluss');// home.index 将查找路径 'resources/views/home/index.php'return $theme->of('home.index', $view)->render();// 用来渲染的状态码return $theme->of('home.index', $view)->render(200);// home.index  将查找路径 'resources/views/mobile/home/index.php'return $theme->ofWithLayout('home.index', $view)->render();// home.index 将查找路径 'public/themes/default/views/home/index.php'// home.index的html内容将被加载到Theme里, view中可以这样调用 {!! Theme::partial('managesetting') !!}return $theme->scope('home.index', $view)->render();// home.index 将查找路径 'public/themes/default/views/mobile/home/index.php'return $theme->scopeWithLayout('home.index', $view)->render();// 寻找自定义路径.return $theme->load('app.somewhere.viewfile', $view)->render();// 使用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

编辑器
theme现在支持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'));

来自另一个视图的符号链接
当您有多个具有相同名称的文件但需要作为单独的文件定位时,这是一个很好的功能。

// 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!
  • assets的基本用法

在路由添加assets

// 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'));

书写内联式或脚本。

// 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);

在 layout 中渲染 styles 和scripts

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

给 theme 分配直接的路径资源

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

准备assets组
有些资源你现在不想添加到页面上,但是有时候你仍然需要它们,所以“cook”和“serve”是你的魔法。
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');
});

您可以在全局包内配置上进行准备。

// 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

/ 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>
...

Partials(局部模板)
在你的layouts或者views渲染Partials

/ This will look up to "public/themes/[theme]/partials/header.php"
echo Theme::partial('header', array('title' => 'Header'));// Partial with current layout specific.
//这将找到并使用当前布局(layout)下的header.php "public/themes/[theme]/partials/[CURRENT_LAYOUT]/header.php"
echo Theme::partialWithLayout('header', array('title' => 'Header'));

从主题部分和应用程序部分中查找。

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');
},

在局部设置
Theme 有 magic 方法 来设置,前置和附加任何东西。

$theme->setTitle('Your title');$theme->appendTitle('Your appended title');$theme->prependTitle('Hello: ....');$theme->setAnything('anything');$theme->setFoo('foo');// or$theme->set('foo', 'foo');

渲染你的layout or view.

Theme::getAnything();Theme::getFoo();// or use place.Theme::place('anything');Theme::place('foo', 'default-value-if-it-does-not-exist');// orTheme::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();// orTheme::getContentArgument('name');// To check if it existsTheme::hasContentArgument('name');
  • 为view准备数据
    有时您不需要执行繁重的处理,所以您可以在需要时准备并使用它。
$theme->bind('something', function()
{return 'This is bound parameter.';
});

在视图上使用绑定数据。

echo Theme::bind('something');
  • Breadcrumb
    要使用Breadcrumb,请遵循以下说明:
$theme->breadcrumb()->add('label', 'http://...')->add('label2', 'http:...');// or$theme->breadcrumb()->add(array(array('label' => 'label1','url'   => 'http://...'),array('label' => 'label2','url'   => 'http://...')
));

渲染breadcrumbs

echo $theme->breadcrumb()->render();// orecho Theme::breadcrumb()->render();

您可以使用blade模板在任何地方设置breadcrumb模板。

$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>
');
  • Widgets Design Structure(小部件设计结构)
    主题有许多有用的功能,称为“widget”,可以是任何东西。

创建widget
您可以使用artisan命令创建一个小部件类:
创造一个全局widget。

php artisan theme:widget demo --global --type=blade

Widget tpl 在本地的 “resources/views/widgets/{widget-tpl}.{extension}”

创建特定的主题名称。

 php artisan theme:widget demo default --type=blade

Widget tpl is located in “public/themes/[theme]/widgets/{widget-tpl}.{extension}”
文件名可以是demo.php、demo.blade.php或demo.twig.php。

现在,您将在/app/widgets/widgetdemo.php上看到一个widget类。

//app/widgets/widgetdemo.php
<h1>User Id: {{ $label }}</h1>

在布局或视图中调用widget

echo Theme::widget('demo', array('label' => 'Demo Widget'))->render();

使用 全局theme

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();
}
// 3. 在视图目录新建frontend目录和对应的index(frontend => 终端, default => 主题):
resources/views/frontend
resources/views/frontend/default
resources/views/frontend/default/index
resources/views/frontend/default/index/index.blade.php
  • templates 参数说明:
  1. path_prefix 为每个终端模板目录路径
  2. template 为使用模板的目录
  3. template_default 为默认模板目录,即找不到 template 模板对应的目录文件,会自动在该参数定义的目录去查找 助手函数你可能会需要:
  4. get_template_dir 获取给定目录下的所有目录

laravel 主题模板功能扩展包teepluss/theme相关推荐

  1. Laravel 主题模板功能扩展包, 这可能是我用过最优雅的 Laravel 主题扩展插件了

    你可能和我一样找了很久 GitHub, 却找不到一款优雅的 laravel 主题扩展插件,老样子,既然找不到就自己动手丰衣足食,该扩展插件核心为:利用 View::addNamespace () 和 ...

  2. 帝国cms 主导航条下拉菜单功能实现,模板功能扩展

    主导航条下拉菜单样式: 具体应用请查看我的个人网站:无知人生 实现步骤 1.在公共模板变量>页面头部模板中使用以下标签来加载菜单项 [listshowclass]'0',13,0,0[/list ...

  3. 一些非常好用的laravel框架扩展包你还不知道么?

    前言 自从有了 composer 包管理工具,出现大量优秀的扩展包,让我们可以解放双手,大大提高我们的开发效率,有了更多的时间去 enjoy life and accompany family,下面我 ...

  4. (一)VirtualBox及其扩展包下载安装

    文章目录 一.VirtualBox下载 二.VitualBox 安装 三.VitualBox安装功能拓展包 一.VirtualBox下载 zynq平台的linux开发 选择vbox安装Ubuntu 主 ...

  5. 轻松入门 | 用 WordPress 和主题模板做网站

    本文来自作者 achair 在 GitChat 上分享「用 WordPress 和主题模板做网站」,「阅读原文」查看交流实录 「文末高能」 编辑 | 嘉仔 本话题围绕如何用 WordPress 和主题 ...

  6. laravel框架应用和composer扩展包开发

    laravel5.5+ laravel官方地址 laravel是目前最流行的php框架,发展势头迅猛,应用非常广泛,有丰富的扩展包可以应付你能想到的各种应用场景,laravel框架思想前卫,跟随时代潮 ...

  7. php个推透传消息,GitHub - Lysice/laravel-getui: Laravel个推的集成包,支持单推/多推/全量推送/透传消息等功能。...

    欢迎使用 Laravel扩展包 laravel-getui 网上有一款shaozeming/laravel-getui,自己在lumen下用,无奈报错依赖出问题,于是自己写了一款. 主要功能 单人推送 ...

  8. 2022新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题

    苹果CMS2022新版海螺影视主题M3.1版本,这个主题我挺喜欢的,之前也有朋友给我提供过原版主题,一直想要破解但是后来找了几个SG11解密的大哥都表示解密需要大几百大洋,所以一直被搁置了.这个版本是 ...

  9. 下载量最高 100 个 Laravel 扩展包推荐

    本文经授权转自 PHPHub 社区,后续更新将以 PHPHub 帖子内容 和 GitHub 内容 为准. 说明 Laravel 另一个令人喜欢的地方,是拥有活跃的开发者社区,而活跃的开发者社区带来的, ...

最新文章

  1. JavaScript语言基础13
  2. asp.net MVC iis6 虚拟主机兼容开发方式
  3. 4+5的值是python_Python基础:数据类型-数字(5)
  4. 【RippleNet】(一)preprocessor.py【未完】
  5. codeforces 536a//Tavas and Karafs// Codeforces Round #299(Div. 1)
  6. 用python语言实现反恐精英基础版-案例
  7. word中插入代码_如何在Word中优雅的插入公式
  8. 360安全卫士卸载干净-笔记
  9. 【转】Excel表格的35招必学秘技
  10. 玩转直播:如何从 0 到 1 构建简单直播系统
  11. 微生物群落的circos图怎么画_微生物群落分析
  12. Oracle:错误码ORA-28040 的坑
  13. C语言 最大公约数与素数探求
  14. 非局域网如何使用ssh远程访问Linux主机
  15. ASR长语音识别,基于vue2
  16. 苹果电脑Finder快捷键汇总
  17. ESP8266与STM32
  18. javaweb发布到云服务器上之后,验证码接收不到问题
  19. 个人笔记FreeRTOS
  20. c++ 懒羊羊找朋友

热门文章

  1. 廉价特斯拉也要来了,或发15万元级车型
  2. html——搜索结果简单ListView展示页面
  3. 《蓝桥杯备赛》CT117E嵌入式竞赛板LCD驱动库的使用(带完整源码)
  4. C++ 实现matlab randperm函数
  5. 【转】为何有的职业后期不给力?
  6. MongoDB Sharding使用总结
  7. 前端开发:Vue项目打包步骤
  8. selenium中使用阿布云代理
  9. java date转filetime_Win32 FILETIME 结构与 java.util.Date 互转
  10. chatGPT对大学生来说有什么利好?