wscore/

html

HTML标签生成类。

1.1.0 2019-05-31 12:19 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:30:21 UTC


README

PHP的HTML标签生成类。

安装

$ composer require wscore/html

示例代码

基本HTML

使用WScore\Html\Html类创建HTML对象,例如;

use WScore\Html\Html;

$html = Html::create('tagName')
            ->set('attribute', 'value')
            ->setContents('content');
echo (string) $html;

应该输出如下HTML:

<tagName attribute="value">content</tagName>

可以setaddremovereset属性。

还有一些魔法方法。

$html = Html::a('sample link')       // magic method to create a new tag and contents
            ->href('check.php')    // magic method to set href attribute
            ->target('_blank');

HTML表单

使用WScore\Html\Form类创建HTML表单对象,例如;

echo Form::open('check.php');
echo Form::input('checkbox', 'keep', 'alive')->class('form-element');
echo Form::close(); 

应该创建如下内容

<form action="check.php" method="post">
<input type="checkbox" name="keep" id="keep" value="alive" class="form-element">
</form>

一些复杂情况

要创建嵌套HTML代码,

echo Html::create('ul')
            ->class('form-list')
            ->setContents(
                Form::input('text', 'name')->placeholder('name here...'),
                Form::input('radio', 'yes', 'here')
            );

应该生成如下HTML代码。

<ul class="form-list">
<input type="text" name="name" id="name" placeholder="name here...">
<input type="radio" name="yes" id="yes" value="here">
</ul>

选项(单选按钮、复选框和下拉选择)

用户使用Form::choices方法来生成选项,例如单选按钮、复选框和下拉选择。

对于单选按钮;

echo Form::choices('test', [
    'val1' => 'label1', 
    'val2' => 'label2'], 
    'val2);

对于复选框;

echo Form::choices('test', [
    'val1' => 'label1', 
    'val2' => 'label2'], 
    'val2)
    ->multiple();

以及对于下拉选择;

echo Form::choices('test', [
    'val1' => 'label1', 
    'val2' => 'label2'], 
    'val2)
    ->expand(false);

演示

要查看WScore.Html的演示,

$ git clone https://github.com/asaokamei/WScore.Html
$ cd WScore.Html
$ composer install
$ cd demo
$ php -S 127.0.0.1:8000

并浏览最后的URL。