关于laravel自定义模板指令-tojs
下面由Laravel教程栏目给大家介绍laravel自定义模板指令-tojs ,希望对需要的朋友有所帮助!
Blade 允许你自定义命令,你可以使用 directive 方法注册命令。当 Blade 编译器遇到该命令时,它将会带参数调用提供的回调函数。blade模板可以通过directive方法来自定义模板指定,
tojs指令主要用于PHP自定义一些数据转换为js对象方便js调用
1.创建ToJsServiceProvider
<?php namespace App\Providers; use App\Helpers\ToJs\ToJs; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; class ToJsServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { $this->app->singleton('tojs', function () { return new ToJs(); }); /* * The block of code inside this directive indicates * the chosen javascript variables. */ Blade::directive('tojs', function () { return '<script> window.Laravel = ' . json_encode(app('tojs')->get()) . '</script>'; }); } }
2. ToJs方法主要是对数组的一些操作
<?php namespace App\Helpers\ToJs; use Illuminate\Support\Arr; class ToJs { protected $data = []; public function put(array $data) { foreach ($data as $key => $value) { $this->data[$key] = value($value); } return $this; } public function get($key = null, $default = null) { if (!$key) return $this->data; return Arr::get($this->data, $key, $default); } public function forget($keys) { Arr::forget($this->data, $keys); return $this; } }
3.声明facade
namespace App\Helpers\ToJs\Facades; use Illuminate\Support\Facades\Facade; class ToJsFacade extends Facade { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'tojs'; } }
4.在config数组添加serviceProvider
providers 添加\App\Providers\ToJsServiceProvider::class
aliases 添加'ToJs' => \App\Helpers\ToJs\Facades\ToJsFacade::class,
5.为了方便调用可以在写一个helper方法
if (!function_exists('to_js')) { /** * Access the javascript helper. */ function to_js($key = null, $default = null) { if (is_null($key)) { return app('tojs'); } if (is_array($key)) { return app('tojs')->put($key); } return app('tojs')->get($key, $default); } }
在PHP代码需要的地方调用 to_js(['username'=>'test']);
blade模板直接通过 @tojs
就可以在页面渲染出<script> window.Laravel = {"username":"test"}</script>
以上是关于laravel自定义模板指令-tojs的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Laravel - Artisan 命令 - Laravel 5.7 提供了处理和测试新命令的新方法。它包括测试 artisan 命令的新功能,下面提到了演示?

Laravel - 分页自定义 - Laravel 包含分页功能,可帮助用户或开发人员包含分页功能。 Laravel 分页器与查询构建器和 Eloquent ORM 集成。自动分页方法

Laravel邮件发送失败时的退信代码获取方法在使用Laravel开发应用时,经常会遇到需要发送验证码的情况。而在实�...

Laravel计划任务运行无响应排查在使用Laravel的计划任务调度时,不少开发者会遇到这样的问题:schedule:run...

在dcatadmin(laravel-admin)中如何实现自定义点击添加数据的表格功能在使用dcat...

Laravel - 转储服务器 - Laravel 转储服务器随 Laravel 5.7 版本一起提供。以前的版本不包括任何转储服务器。转储服务器将成为 laravel/laravel Composer 文件中的开发依赖项。

Laravel框架中Redis连接的共享与select方法的影响在使用Laravel框架和Redis时,开发者可能会遇到一个问题:通过配置...
