Home > PHP Framework > Laravel > body text

About laravel custom template directive-tojs

藏色散人
Release: 2021-02-08 16:18:39
forward
2700 people have browsed it

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>';
        });
    }
}
Copy after login

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;
    }
}
Copy after login

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';
    }
}
Copy after login

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);
    }
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template