定制 mouf/html.tags 二次开发

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

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

mouf/html.tags

最新稳定版本:v1.0.0

Composer 安装命令:

composer require mouf/html.tags

包简介

This package contains a pure PHP object representation of HTML5 tags. There is one class for each existing HTML tag, and one getter/setter per attribute.

README 文档

README

This package contains a pure PHP object representation of HTML5 tags. There is one class for each existing HTML tag, and one getter/setter per attribute.

Why bother?

There are plenty of ways to represent HTML tags as PHP objects, and most of the time, you might not need one class per tag (you can use a generic PHP object representing any HTML tag like here, here or , here).

So why bother with one class per tag?

Well for a few reasons:

  • You can have autocompletion when using the tags (if you are using a sensible PHP IDE)
  • You can use those tags in dependency injection frameworks (this part might sound completely crazy to you until you learn more about the Mouf PHP framework.

List of supported tags

Basically, all the tags supported and not deprecated in HTML5. You can get a complete list on the W3C wiki.

How to use it?

###Basic usage

Pretty easy. You just create a tag using the new keyword and use getters and setters to set attributes. Finally, you call the toHtml() method to render the tag.

$div = new Div();
$div->setId('myDiv');
$div->toHtml();

###Fluent interface

The setter are using a fluent interface, so you can chain several setters if you want:

$input = new Input();
$input->setType('text')->setName('company')->setLabel('TheCodingMachine');
$input->toHtml();

###Setting classes and styles

You can set classes and styles using arrays or strings:

$label = new Label();
$label->setClasses(array("css-class1 css-class2"));
$label->setStyles(array("background-color" => "red"));
$label->toHtml();

You can also add a class or a style using addClass or addStyle.

$label->addClass("css-class3");
$label->addStyle("background-color", "red");

###Managing children

You can add children to your tags using setChildren or addChild.

$div->setChildren(array($label, $input));
$div->addChild($label);

###Adding text to a tag

Use the addText to add text to a tag.

$p = new Paragraph();
$p->addText("This is my text");

Note: this is similar to creating a HtmlString instance and adding it to the node:

// Similar to:
$p->addChild(new HtmlString("This is my text"));

Adding your own custom tags

You might be interested in adding your owns classes that generate custom HTML code. All you have to know is that all the tags are implementing the HtmlElementInterface.

This is a simple interface with a single toHtml() method.

What is this ValueInterface thing?

If you have had a look at the code, you might have noticed that each setter accepts objects implementing the ValueInterface interface.

This is a very powerful feature allowing you to put objects that will be evaluated when they are rendered. When the tag is rendered, the val() method of the object is called and attributed.

Mouf package

This package is part of Mouf, an effort to ensure good developing practices by providing a graphical dependency injection framework.

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 11
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-10-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固