stefan-wiebe/elements 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

stefan-wiebe/elements

Composer 安装命令:

composer require stefan-wiebe/elements

包简介

A simple PHP-library for building HTML-Elements.

README 文档

README

This is a simple library to construct HTML elements with PHP, mainly to avoid getting into a concatenation-hell.

1 Generating an element

1.1 Constructor

The first way to generate an Element is using its constructor.

It takes the following parameters

Parameter Type Description Example Default
$tag String The HTML-tag 'div' Required
$attributes array HTML-attributes ['id' => 'wrap-god'] array()
$content array The element's contents ['hi', $span] array()

You can put everything into an Element's contents as long as it parses to a String, including other Elements.

use Elements\Element;

$attributes = [
	'id' => 'wrap-god',
	'class' => ['fresh', 'mexican', 'tortilla'],
];

$div = new Element('div', $attributes);

Classes can be passed as Array (as shown here) or using a String, separating them with spaces ('fresh mexican tortilla'). Note however that Strings will be exploded. As in the PHP-function.

1.2 Factory Method

There's also a fairly simple factory method for generating an Element only with a tag and the provided classes. As mentioned before, you can either provide an Array of classes or seperate them by spaces.

Parameter Type Description Example Default
$tag String The HTML-tag 'span' Required
$classes Array | String The classes 'winged-hussar' Required
use Elements\Element;

$div = Element::withClasses('div', 'undetected unexpected');

2. Modifying Attributes

Since Elements implement ArrayAccess, its attributes can be manipulated by accessing the Element like an Array.

Let's say we wanted to give an id to the Element created in 1.2. We would do it like this:

$div['id'] = 'night-witch';

With the exception of classes and styles, all attributes are Strings.

2.1 Classes

Classes are internally handled as Arrays, as previously mentioned. This enables you to easily add and remove classes without having to worry about having one space too many or too few.

If you wanted to add some to our pre-existing div, you could do it like so:

$div['class'][] = 'wings-of-glory';
$div['class'][] = 'tell-the-story';

Replacing them works in the same way as declaring them in the first place:

$div['class'] = ['deviation', 'aviation'];
// or
$div['class'] = 'deviation aviation';

2.2 Styles

Styles are also handled as Arrays, however unlike classes, they are associative Arrays, since it'd be kind of hard otherwise.

You can add classes like so:

$div['style']['display'] = 'none'; // Stealth perfected

Note that you can also pass a String to set an Element's styles, but it's not recommended since I can't guarantee that the regex will work 100% of the time. I didn't find any glaring issues, but I also didn't test a lot.

$div['style'] = 'display: none; background: linear-gradient(to bottom, red, blue);'

3 Content

An Element's contents can be anything that parses to a String. They are kept as array and can be manipulated using the addContent($content) and setContent(array $content) methods.

Example:

$div = Element::withClasses('div', 'form-group');

$input = new Element('input');
$input['type'] = 'text';

$div->addContent($input);

4 Outputting Elements

In order to output an Element, simply echo it or cast it to a string if you need to.

echo $div;

$someString .= (string) $div;

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固