承接 avplab/php-html-builder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

avplab/php-html-builder

Composer 安装命令:

composer require avplab/php-html-builder

包简介

PHP Html builder simplifies creation of an html code in php scripts. Allows to build (or generate) the html in simple natural way, similarly as create a html page.

README 文档

README

Build Status

Sometimes, we strongly need something simple for creating html code in php runtime. For example, we want to build a simple html report, or build a simple html for highlighting some profiling data, etc. Usually for such cases we don't want to use templates engines, or create separated html files. As result we have mess of html and php code, or tons of concatenated strings with html code. PhpHtmlBuilder was created to solve these issues quickly and without clogging the php code. You simply use it same as you write a html.

Installation

Install the component by using Composer. Update your project's composer.json file to include dependency.

"require": {
    "avplab/php-html-builder": "~2.0"
}

Usage

To start building an html code, create an instance of AvpLab\PhpHtmlBuilder and use it similary as writing html.

$builder = new \AvpLab\PhpHtmlBuilder();
$builder
    ->tag('!DOCTYPE')->setHtml()->endOpened()
    ->html()->setLang('en')
        ->head()
            ->meta()->setHttpEquiv('X-UA-Compatible')->setContent('IE=edge,chrome=1')->endOpened()
            ->title('PhpHtmlBuilder: Example')->end()
        ->end()
        ->body()
            ->div()->setClass('container')
                ->div()->setClass('row')
                    ->div()->setClass('col-md-12')
                        ->h1('PhpHtmlBuilder Demo')->end()
                        ->p('Designed to make the code easier')->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end();

echo $builder;

The example above will build the following html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>PhpHtmlBuilder: Example</title>
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col-md-12">
          <h1>PhpHtmlBuilder Demo</h1>
          <p>Designed to make the code easier</p>
        </div>
      </div>
    </div>
  </body>
</html>

Comments

To add comment block use method addComment().

$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder->addComment('foo');

//Result
<!--foo-->

Tags

There are two ways to create HTML tags: The first one(this is also the most common way) is to call the method with the same name as HTML tag in CamelCase format. Tags are always be converted into lowercase with dashes.

$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder->html()->customTag()->end()->end();

//Result
<html><custom-tag></custom-tag></html>

The second way is to call the method tag() with the name of the HTML tag. In this case no any conversion is applied. This is useful when you need to create very specific tags like <!DOCTYPE>.

$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder->tag('!DOCTYPE')->endOpened();

//Result
<!DOCTYPE>

To complete the tag need to call one of the following methods: end(), endShorted() and endOpened().

  • Method end() will create tag <tag></tag> (i.e. div, p, span, etc.).
  • Method endShorted() will create short tag <tag />. In this case, tag can have only attributes (i.e. script, link, img or input and similar).
  • Method endOpened() will create opened tag <tag>. In this case, tag can have only attributes (i.e. meta or similar).

There is also a possibility to add html and attributes during tag creation, using arguments of methods. Arguments will be recognized in the following way:

  • If only one argument is provided, and this is an array, it will be recognized as tag attributes, otherwise as tag html(see addHtml() below).
  • If two arguments are provided, the first is a tag html, and second is array of attributes. NOTE: in this case don't need to use camelCase for attributes names. It will keep the name as is. Also if attribute doesn't have a key, it will be recognized as attribute without value.
$builder = new \AvpLab\PhpHtmlBuilder();
echo $builder
    ->div('<h1>title</h1>', ['class' => 'container', 'Foo' => 'Bar', 'baz'])
        ->p(['class' => 'article'])->end()
    ->end();

//Result
<div class="container" Foo="Bar" baz><h1>title</h1><p class="article"></p></div>

Attributes

Creating attributes is very similar as creating tags. You need to call the method with an appropriate name in CamelCase format and beginning with set. If method called without arguments, only attribute name will be applied.

$builder = new \AvpLab\PhpHtmlBuilder();
$builder
    ->tag('!DOCTYPE')->setHtml()->endOpened()
    ->html()->setLang('en')->end();

//Result
<!DOCTYPE html><html lang="en"></html>

Content

To add a plain text(escaped) into the tag, call the method addText(). For adding "raw" html string, use another method addHtml().

$builder = new \AvpLab\PhpHtmlBuilder();
$builder
    ->div()
        ->addText('foo')
        ->addHtml('<b>bar</b>')
    ->end();

//Result
<div>foo<b>bar</b></div>

Render

To get an HTML string, you need to call the build() method or simply recognize it as string.

$builder = new \AvpLab\PhpHtmlBuilder();
// Do some html

$htmlString = $builder->build();
echo $htmlString;

// Supports string recognision
echo $builder;

License

PhpHtmlBuilder is licensed under the MIT License - see the LICENSE file for details

avplab/php-html-builder 适用场景与选型建议

avplab/php-html-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.63k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2015 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「html」 「builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 avplab/php-html-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 avplab/php-html-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 avplab/php-html-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-11