Coder Social home page Coder Social logo

laravel-simple-templa's Introduction

The Simple Templa For Laravel

Testing PHPStan Validate Composer

This package is a wrap of Simple Templa for Laravel.
A Scaffold function using Simple Templa has also been added.

Installation

Execute the following composer command.

composer require takemo101/laravel-simple-templa

Publish the config

Publish the config with the following artisan command.
You can set filters and scaffolds from the config.

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

How to use

Please use as follows

Simple Templa Facade

You can use the template language by using the Facade class.

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

Run the Artisan command below to generate the Scaffold class.

php artisan make:scaff ClassName

Please set the input rule and output path in the generated Scaffold class.

<?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"),
        ];
    }
}

Create a stub file as the output source.

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

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

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

Set the created Scaffold class in the config.

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

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

After setting the Scaffold class, run Artisan to create the file.

php artisan simple-templa:exec demo

Filter for Laravel

In the template language, you can use Filter using Laravel's Str class.

filter str method
camel Str::camel
headline Str::headline
kebab Str::kebab
lower Str::lower
markdown Str::markdown
plural Str::plural
pluralStudly Str::pluralStudly
reverse Str::reverse
singular Str::singular
slug Str::slug
snake Str::snake
studly Str::studly
title Str::title
ucfirst Str::ucfirst
upper Str::upper

laravel-simple-templa's People

Contributors

takemo101 avatar hayama-kei avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.