takemo101/laravel-simple-templa

Laravel Simple Templa

v0.1.5 2022-06-20 02:01 UTC

This package is auto-updated.

Last update: 2024-09-20 06:54:56 UTC


README

Testing PHPStan Validate Composer

此包是对 Laravel 的 Simple Templa 的包装。
还添加了使用 Simple Templa 的 Scaffold 函数。

安装

执行以下 composer 命令。

composer require takemo101/laravel-simple-templa

发布配置

使用以下 artisan 命令发布配置。
您可以从配置中设置过滤器和 Scaffold。

php artisan vendor:publish --tag="simple-templa"

如何使用

请按照以下方式使用

Simple Templa 门面

您可以通过使用 Facade 类来使用模板语言。

// Get Template object
$template = \SimpleTempla::template('{{ data.a }} {{ data.b }}');

// Run parse
echo $template->parse([
    'data' => [
        'a' => 'hello',
        'b' => 'world',
    ],
]);
// hello world

// Run parse immediately
echo \SimpleTempla::parse(
    '{{ data.a }} {{ data.b }}'),
    [
        'data' => [
            'a' => 'hello',
            'b' => 'world',
        ],
    ]
);
// hello world

Scaffold

运行以下 artisan 命令以生成 Scaffold 类。

php artisan make:scaff ClassName

请在生成的 Scaffold 类中设置输入规则和输出路径。

<?php
namespace App\Scaffolds;

use Takemo101\LaravelSimpleTempla\Scaffold\Scaffold;

// Must inherit from Scaffold class
class DemoScaffold extends Scaffold
{
    /**
     * Constructor injection is possible
     */
    public function __construct()
    {
        //
    }

    /**
     * get need command option validation rules
     *
     * @return string[]
     */
    public function rules(): array
    {
        return [
            'name' => 'required',
        ];
    }

    /**
     * get inout path sets
     *
     * @param mixed[] $data
     * @return array<string,string|string[]>
     */
    public function inoutPaths(array $data): array
    {
        // You can create a class file using the template language from the file
        return [
            resource_path("stub/Entity.stub") 
            => app_path("Entity/{{ name|ucfirst }}Entity.php"),
        ];
    }
}

创建一个作为输出源的存根文件。

<?php
// ./resources/stub/Entity.stub

namespace Stub\Entity\Demo{{ name|ucfirst }};

class Demo{{ name|ucfirst }}Entity
{
    /**
     * @var string
     */
    private string $name = '{{ key|lower }}';
}

在配置中将创建的 Scaffold 类设置好。

<?php
// ./config/simple-sample.php

return [
    ...
    'scaffolds' => [
        'demo' => App\Scaffolds\DemoScaffold::class,
    ],
]

设置 Scaffold 类后,运行 Artisan 创建文件。

php artisan simple-templa:exec demo

Laravel 过滤器

在模板语言中,您可以使用 Laravel 的 Str 类来使用 Filter。