Home >PHP Framework >Laravel >About laravel custom template directive-tojs

About laravel custom template directive-tojs

藏色散人
藏色散人forward
2021-02-08 16:18:272740browse

The following is the tutorial column of Laravel to introduce laravel custom template instructions-tojs. I hope it will be helpful to friends in need!

Blade allows you to customize commands. You can register commands using the directive method. When the Blade compiler encounters this command, it will call the provided callback function with arguments. The blade template can customize the template specification through the directive method. The

tojs directive is mainly used in PHP to convert some data into js objects to facilitate js calls.

1. Create 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. The ToJs method mainly performs some operations on the array

<?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. Declare 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. Add serviceProvider

providers in the config array Add
\App\Providers\ToJsServiceProvider::class

aliases Add
'ToJs' => \App\Helpers\ToJs\Facades\ToJsFacade::class,

5. For the convenience of calling, you can write a helper method

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

Call where needed in the PHP code to_js(['username'=>'test'] );

blade template can be rendered on the page directly through @tojs
<script> window.Laravel = {"username":"test "}</script>

The above is the detailed content of About laravel custom template directive-tojs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete