rchipka/tagbuilder

dev-master 2018-07-23 20:13 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:27:50 UTC


README

PHP的轻量级HTML标签构建API

优点

  • 不再有HTML破坏和切换 <?php 模式

  • 不再有内联属性逻辑/切换

  • 减少痛苦地输出,连接

  • 轻松将HTML标签存储在变量中

  • 不可能写入格式错误的HTML(PHP括号要求平衡)

  • 自动转义属性值

  • 能够钩子/过滤器所有元素/内容/属性

  • 与VBTK上下文集成,实现更细粒度的钩子/过滤器

  • 不会显示空内容的标签(不再需要if !empty()逻辑)

用法

_tagname() <- 输出 html __tagname() <- 返回 html

参数

_tagname($attributes, $content)

$attributes - 可选 - 属性的键/值数组

  • 值可以是字符串、数字或数组

  • 提供一个纯顺序数组(数字键),则值将默认为 class 属性

  • 如果属性设置为键/值数组,则值将确定是否包含相应的键(用于类切换)

$content - 可选 - 值或值数组

  • 值可以是字符串、函数或字符串/函数的数组

示例

<?php
_ul( [ 'class' =>  [ 'no-bullet', 'sidebar-links' ] ], function () {
  while ( have_rows( 'links' ) ) : the_row();
    _li( function () {
      _h5( get_sub_field('title') );
      _ul( [ 'class' => [ 'no-bullet', 'sub-links' ] ], function () {
        foreach (get_sub_field('links') as $link) :
          _li(
            __a([ 'href' => $link['url'] ], $link['title'] )
          );
        endforeach;
      });
    });
  endwhile;
});
_div([ 'class' => 'event-meta' ], [
    __p([ 'class' => 'event-date' ], function ($expect) {
        date( 'm.j', $expect( get_field( 'start_date' ) ) );
    }),
    __p([ 'class' => 'event-time' ], function ($expect) {
      echo implode(' - ', array_unique([
          strtoupper( date( 'g:ia', $expect( get_field( 'start_time' ) ) ) ),
          strtoupper( date( 'g:ia', $expect( get_field( 'end_time' ) ) ) ),
        ]));
    })
]);