定制 anvii/htmlelement 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

anvii/htmlelement

最新稳定版本:1.1.2

Composer 安装命令:

composer require anvii/htmlelement

包简介

A library to create HTML code in PHP

README 文档

README

HtmlElement provides an easy and efficient way to construct an HTML code in PHP

HtmlElement is useful if you want to alternate you HTML code in PHP, like in modular architecture, and have elegant source code of your program.

Prerequisites

HtmlElement requires PHP v7.3 or greater

Building the HTML

Sample code:

use anvii\HtmlElement\HtmlElement;

// Create an element
$div = new HtmlElement('div', ['class'=>'class1 class2'], 'Hello, World!');

// Build sample menu
$menu = HtmlElement::ul(['id'=>'sample-menu', 'class'=>'menu'], [
    HtmlElement::li(['class'=>'active'],
        HtmlElement::a(['href'=>'...'], 'New')
    ),
    HtmlElement::li(['class'=>''],
        HtmlElement::a(['href'=>'...'], 'Open')
    ),
    HtmlElement::li(['class'=>'disabled'],
        HtmlElement::a(['href'=>'...'], 'Save')
    )
]);

// Add one more element(s)
$menu->add([
    HtmlElement::hr(),
    HtmlElement::li(
        HtmlElement::a(['href'=>'...', '_if'=>$hasHelp], 'Help')
    )
]);

// Query elements like in jQuery
$menu->query('a')->addClass('awesome');
$menu->query('li.disabled a')->removeClass('awesome');

// Send menu to browser
echo $menu;

Quering elements

The library has a simple query engine like in jQuery. It allows to alternate HTML code by modules on the fly.

$form = $html->first('form#login');
$form->query('input[type="submit"]')->addClass('nice-button');

Class HtmlQuery also supports callback functions, but using strings with function names is forbidden due to security reason.

$form->query(function($e) {
    return ($e instanceof \my\Button::class);
});

Note, that querying in HtmlElement is very restricted. It supports only one class and only one attribute.

Special attributes

HTML attributes started with underscore '_' are special. They are not rendered, but used for building document.

echo HtmlElement::a(['href'=>'/user/profile', '_if'=>isUserRegistered()], 'Profile');

The next special attributes are used by HtmlElement:

  • _if - Conditional element. Value of this attribute can be boolean or a function.
  • _raw - Do not encode content of an element.
  • _weight - Used to define order of elements. See 'Ordering elements' below.
  • _hideempty - Don't show element if it's content is empty.

Ordering elements

You can order elements by using special attribute '_weight'. For example, you can construct menus dynamically from modules:

$userMenu->add([
    HtmlElement::a(['href'=>'/user/profile', '_weight'=>10], 'Profile'),
    HtmlElement::a(['href'=>'/user/password', '_weight'=>12], 'Change Password'),
    HtmlElement::a(['href'=>'/user/Logout', '_weight'=>100], 'Logout'),
]);

// From module:
HtmlElement::first('#userMenu')->add(
    HtmlElement::a(['href'=>'gallery', 'weight'=>30])
);

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-2.1
  • 更新时间: 2022-09-11