joas8211/html-composition

PHP的HTML构建器

1.0.3 2020-11-16 19:48 UTC

This package is auto-updated.

Last update: 2024-09-17 04:15:38 UTC


README

PHP的HTML构建器

特性

  • 方法链 🔗
  • 可选的美化打印 😍
  • 元素或完整文档渲染 🎨
  • 可配置缩进 📝
  • 代码注入 💉

安装

使用Composer

使用Composer将HTML Composition安装到您的项目中

composer require joas8211/html-composition

如果您还没有的话,需要require autoload.php

require __DIR__ . '/vendor/autoload.php';

不使用Composer

  1. 从发布中下载HtmlComposition.php。
  2. 在需要它的PHP文件中require该文件。
require 'HtmlComposition.php';

用法

示例用法

use HtmlComposition\HtmlComposition;

echo (new HtmlComposition)
    ->document()
    ->tag('html', ['lang' => 'en'])
        ->tag('head')
            ->tag('title')->text('Example Document')->end()
        ->end()
        ->tag('body')
            ->tag('h1')->text('Hello World!')->end()
            ->tag('img', [
                'src' => 'https://picsum.photos/768/432',
                'alt' => '',
            ], true)
        ->end()
    ->end();

上面的代码生成以下HTML

<!doctype html>
<html lang="en">
    <head>
        <title>
            Example Document
        </title>
    </head>
    <body>
        <h1>
            Hello World!
        </h1>
        <img src="https://picsum.photos/768/432" alt="">
    </body>
</html>