ride/lib-html

Ride框架的HTML库

1.2.0 2024-06-26 08:46 UTC

This package is auto-updated.

Last update: 2024-08-26 09:11:57 UTC


README

PHP Ride框架的HTML辅助库。

本库包含内容

元素

Element接口用于实现HTML元素。它提供设置属性的帮助方法和一个生成HTML的方法。

提供此接口的不同实现。

锚点

Anchor类是HTML文档主体中任何位置使用的元素的表示。

图像

Image类是HTML文档主体中任何位置使用的元素的表示。

元数据

Meta类是HTML文档头部中使用的元素的表示。

分页

Pagination类是一个生成分页块的辅助工具。它通过创建第一个、活动页和最后一页之间的间隔来处理多页,如下例所示。

< 1 2 ... 45 __46__ 46 ... 88 89 >

表格

_Table类是HTML文档主体中任何位置使用的

元素的表示。根据用例或数据源,可以提供扩展的实现。

Row类是HTML文档中表格中使用的

元素的表示。

单元格

Cell类是HTML文档中表格行中使用的

元素的表示。

表头单元格

HeaderCell类是HTML文档中表格行中使用的

元素的表示。

数组表格

虽然常规Table类期望你创建Row实例等,但ArrayTable类的工作方式不同。它的起点是一个简单的数据数组。数组中的每个元素都是一个Row。通过添加一个表格Decorator,你可以创建列并决定该行单元格的内容,通过格式化数据或其一部分来实现。

表单表格

FormTable类在ArrayTable的基础上进一步工作。它从一个表格创建一个表单组件,并添加了添加分页、搜索、排序和操作的功能。你可以进一步扩展它。

导出表格

ExportTable接口为实现它的表格添加了导出功能。你可以为导出添加单独的装饰器。导出通过传递一个ExportFormat实现传递给它来填充。

FormTable类实现了此接口。

HtmlParser

HtmlParser类帮助您处理HTML片段。您可以使用它将所有图像和锚点转换为绝对路径而不是相对路径。

代码示例

查看以下代码示例以了解本库的一些功能

<?php

use ride\library\form\Form;
use ride\library\html\table\decorator\StaticDecorator;
use ride\library\html\table\decorator\ValueDecorator;
use ride\library\html\table\FormTable;
use ride\library\html\Anchor;
use ride\library\html\HtmlParser;
use ride\library\html\Image;
use ride\library\html\Meta;
use ride\library\html\Pagination;

function exampleAnchor() {
    $anchor = new Anchor('ride/lib-html', 'https://github.com/all-ride/ride-lib-html');
    $anchor->setId('github-link');
    $anchor->setClass('btn');
    $anchor->addToClass('btn-primary');
    
    $html = $anchor->getHtml();
    // <a id="github-link" class="btn btn-primary" href="https://github.com/all-ride/ride-lib-html">ride/lib-html</a>
}

function exampleImage() {
    $image = new Image('https://url/to/image');
    $image->setAttribute('alt', 'Caption for the image');
    
    $html = $image->getHtml();
    // <img src="https://url-to-image" alt="Caption for the image" />
}

function exampleMeta() {
    $meta = new Meta();
    $meta->setName('viewport');
    $meta->setContent('width=device-width, initial-scale=1, shrink-to-fit=no');
    
    $html = $meta->getHtml();
    // <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        
    $meta = new Meta();
    $meta->setProperty('og:title');
    $meta->setContent('My Title');
    
    $html = $meta->getHtml();
    // <meta property="og:title" content="My Title" />
}

function examplePagination() {
    $pages = 3;
    $page = 2;
    
    $pagination = new Pagination($pages, $page);
    $pagination->setHref('http://url/to/?page=%page%');

    $previous = $pagination->getPreviousLink();
    // http://url/to/?page=1
    
    $next = $pagination->getNextLink();
    // http://url/to/?page=3
    
    $anchors = $pagination->getAnchors();
    // array all the anchor instances
    
    $html = $pagination->getHtml();
    // <div class="pagination">
    // <ul>
    //     <li><a href="http://url/to/?page=1">&laquo;</a></li>
    //     <li><a href="http://url/to/?page=1">1</a></li>
    //     <li class="active"><a href="http://url/to/?page=2">2</a></li>
    //     <li><a href="http://url/to/?page=2">3</a></li>
    //     <li><a href="http://url/to/?page=3">&raquo;</a></li>
    // </ul>
    // </div>
}

function exampleHtmlParser() {
    $html = '
<a href="some/action">Some text</a>
<a href="http://www.foo.bar">Foo</a>
<a href="#"><img src="img/icon.png"/></a>
';
    $baseUrl = 'http://url/to';
    
    $htmlParser = new HtmlParser($html);
    $htmlParser->setStripBody(true);
    $htmlParser->makeAnchorsAbsolute($baseUrl);
    $htmlParser->makeImagesAbsolute($baseUrl);
    
    $html = $htmlParser->getHtml();
    // <a href="http://url/to/some/action">Some text</a>
    // <a href="http://www.foo.bar">Foo</a>
    // <a href="#"><img src="http://url/to/img/icon.png"/></a>
}

function exampleFormTable(Form $form) {
    // some sample data, can be objects or anything
    $values = array(
        2 => array('name' => 'John', 'surname' => 'Doe', 'age' => 35),
        5 => array('name' => 'Jane', 'surname' => 'Doe', 'age' => 33),
        9 => array('name' => 'Neville', 'surname' => 'Brown', 'age' => 41),
    );
    
    $baseUrl = 'http://url/to/overview';
    
    // lets create the table
    $table = new FormTable($values);
    $table->setFormUrl($baseUrl);
    
    // add some decorators to create columns, heading decorators are optional
    $table->addDecorator(new ValueDecorator('name'), new StaticDecorator('Name'));
    $table->addDecorator(new ValueDecorator('surname'), new StaticDecorator('Surname'));
    $table->addDecorator(new ValueDecorator('age'), new StaticDecorator('Age'));
    
    // add order methods on the values
    $hasOrder = $table->hasOrderMethods();
    // false;
    
    // a simple ordering callback, one for ascending and one for descending
    $orderNameAscCallback = 'orderNameAsc';
    $orderNameDescCallback = 'orderNameDesc';
    
    $table->addOrderMethod('Name', $orderNameAscCallback, $orderNameDescCallback);
    
    // you can add extra arguments for your callbacks, check the function signatures further below
    $orderCustomAscCallback = 'orderCustomAsc';
    $orderCustomDescCallback = 'orderCustomDesc';
    
    $table->addOrderMethod('Custom', $orderCustomAscCallback, $orderCustomDescCallback, 'name', 'surname', 'age');
    
    $table->setOrderMethod('Name');
    $table->setOrderDirection('asc');

    $hasOrder = $table->hasOrderMethods();
    // false;

    // now add some pagination
    $hasPaginationOptions = $table->hasPaginationOptions();
    // false
    
    $table->setPaginationOptions(array(5, 10, 25, 50, 100, 250));
    $table->setPaginationUrl($baseUrl . '?page=%page%');
    $table->setRowsPerPage(10);
    $table->setPage(1);
    
    // searching values is to be implemented by extending the FormTable class and implementing the applySearch method
    $hasSearch = $table->hasSearch();
    // false;
    
    $table->setHasSearch(true);
    $table->setSearchQuery('doe');
    
    $searchQuery = $table->getSearchQuery();
    // 'doe'
    
    $hasSearch = $table->hasSearch();
    // true;
    
    // but it wont work unless applySearch is implemented
    
    // add some actions which can be applied on multiple items in the table
    $moveCallback = 'onMove';
    $deleteCallback = 'onDelete';
    
    $hasActions = $table->hasActions();
    // false
    
    $table->addAction('Move', $moveCallback);
    $table->addAction('Delete', $deleteCallback, 'Are you sure you want to delete the selected items?');
    
    $hasActions = $table->hasActions();
    // true
    
    $actionConfirmationMessages = $table->getActionConfirmationMessages);
    // array(
    //     'Delete' => 'Are you sure you want to delete the selected items?',
    // ) 
    
    // we have an unbuild form, add the table to it as a component
    $form->addRow('table', 'component', array(
        'component' => $table,
    ));
    $form = $form->build();
    
    $table->processForm();
    
    $numTotalRows = $table->countRows();
    $numDisplayRows = $table->countPageRows();
    $numPages = $table->getPages();
    $pagination = $table->getPagination();
    // ride\library\html\Pagination
    
    $html = $table->getHtml();
    // ... :-)
}

function orderNameAsc(array $values) {
    // order on name asc
}

function orderNameAsc(array $values) {
    // order on name desc
}

function orderNameDesc(array $values) {
    // custom ascending order with extra arguments
}

function orderCustomDesc(array $values, $name, $surname, $age) {
    // custom descending order with extra arguments
}

function onMove(array $ids) {
    // delete the selected ids
}

function onDelete(array $ids) {
    // delete the selected ids
}

相关模块

安装

您可以使用Composer来安装此库。

composer require ride/lib-html