跳转到内容

标记工厂


概览

Phalcon\Html\TagFactory是一个生成 HTML 标签的组件。该组件创建了一个新的类定位器,并将预定义的 HTML 标签类附加到其上。每个标签类都是懒加载的,以达到最高性能。要实例化这个工厂并获取一个标签辅助工具,你需要调用newInstance()并传入一个Phalcon\Html\Escaper对象。

如果你使用的是Phalcon\Di\FactoryDefault作为应用程序的容器,Phalcon\Html\TagFactory已经为您以名称tag.

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\TagFactory;

$escaper = new Escaper();
$factory = new TagFactory($escaper);
$helper  = $factory->newInstance('a');
<?php

use Phalcon\Di\FactoryDefault;

$container = new FactoryDefault();

$helper = $container->tag->newInstance('a');

注册的帮助程序对应的名称如下:

名称
a Phalcon\Html\Helper\Anchor
base Phalcon\Html\Helper\Base
breadcrumbs Phalcon\Html\Helper\Breadcrumbs
body Phalcon\Html\Helper\Body
button Phalcon\Html\Helper\Button
close Phalcon\Html\Helper\Close
doctype Phalcon\Html\Helper\Doctype
element Phalcon\Html\Helper\Element
form Phalcon\Html\Helper\Form
img Phalcon\Html\Helper\Img
inputCheckbox Phalcon\Html\Helper\Input\Checkbox
inputColor Phalcon\Html\Helper\Input\Color
inputDate Phalcon\Html\Helper\Input\Date
inputDateTime Phalcon\Html\Helper\Input\DateTime
inputDateTimeLocal Phalcon\Html\Helper\Input\DateTimeLocal
inputEmail Phalcon\Html\Helper\Input\Email
inputFile Phalcon\Html\Helper\Input\File
inputHidden Phalcon\Html\Helper\Input\Hidden
inputImage Phalcon\Html\Helper\Input\Image
inputInput Phalcon\Html\Helper\Input\Input
inputMonth Phalcon\Html\Helper\Input\Month
inputNumeric Phalcon\Html\Helper\Input\Numeric
inputPassword Phalcon\Html\Helper\Input\Password
inputRadio Phalcon\Html\Helper\Input\Radio
inputRange Phalcon\Html\Helper\Input\Range
inputSearch Phalcon\Html\Helper\Input\Search
inputSelect Phalcon\Html\Helper\Input\Select
inputSubmit Phalcon\Html\Helper\Input\Submit
inputTel Phalcon\Html\Helper\Input\Tel
inputText Phalcon\Html\Helper\Input\Text
inputTextarea Phalcon\Html\Helper\Input\Textarea
inputTime Phalcon\Html\Helper\Input\Time
inputUrl Phalcon\Html\Helper\Input\Url
inputWeek Phalcon\Html\Helper\Input\Week
label Phalcon\Html\Helper\Label
link Phalcon\Html\Helper\Link
meta Phalcon\Html\Helper\Meta
ol Phalcon\Html\Helper\Ol
script Phalcon\Html\Helper\Script
style Phalcon\Html\Helper\Style
title Phalcon\Html\Helper\Title
ul Phalcon\Html\Helper\Ul

方法调用

如果你不想调用newInstance(),你可以始终使用与该帮助程序名称相对应的方法调用。某些帮助程序接受一个bool $raw参数,该参数定义输入是否会被转义。在创建带有图片的锚点链接时这很有用。

public function a(
    string $href, 
    string $text, 
    array $attributes = [], 
    bool $raw = false
): string

public function base(
    string $href, 
    array $attributes = []
): string

public function body(
    array $attributes = []
): string

public function breadcrumbs(
    string $indent = '    ',
    string $delimiter = "\n"
): Breadcrumbs

public function button(
    string $text, 
    array $attributes = [], 
    bool $raw = false
): string

public function close(
    string $tag, 
    bool $raw = false
): string

public function doctype(
    int $flag, 
    string $delimiter
): Doctype

public function element(
    string $tag, 
    string $text, 
    array $attributes = [], 
    bool $raw = false
): string

public function form(
    array $attributes = []
): string

public function img(
    string $src, 
    array $attributes = []
): string

public function inputCheckbox(
    string $name, 
    string $value = null, 
    array $attributes = []
): Checkbox

public function inputColor(
    string $name, 
    string $value = null, 
    array $attributes = []
): Color

public function inputDate(
    string $name, 
    string $value = null, 
    array $attributes = []
): Date

public function inputDateTime(
    string $name, 
    string $value = null, 
    array $attributes = []
): DateTime

public function inputDateTimeLocal(
    string $name, 
    string $value = null, 
    array $attributes = []
): DateTimeLocal

public function inputEmail(
    string $name, 
    string $value = null, 
    array $attributes = []
): Email

public function inputFile(
    string $name, 
    string $value = null, 
    array $attributes = []
): File

public function inputHidden(
    string $name, 
    string $value = null, 
    array $attributes = []
): Hidden

public function inputImage(
    string $name, 
    string $value = null, 
    array $attributes = []
): Image

public function inputInput(
    string $name, 
    string $value = null, 
    array $attributes = []
): Input

public function inputMonth(
    string $name, 
    string $value = null, 
    array $attributes = []
): Month

public function inputNumeric(
    string $name, 
    string $value = null, 
    array $attributes = []
): Numeric

public function inputPassword(
    string $name, 
    string $value = null, 
    array $attributes = []
): Password

public function inputRadio(
    string $name, 
    string $value = null, 
    array $attributes = []
): Radio

public function inputRange(
    string $name, 
    string $value = null, 
    array $attributes = []
): Range

public function inputSearch(
    string $name, 
    string $value = null, 
    array $attributes = []
): Search

public function inputSelect(
    string $name, 
    string $value = null, 
    array $attributes = []
): Select

public function inputSubmit(
    string $name, 
    string $value = null, 
    array $attributes = []
): Submit

public function inputTel(
    string $name, 
    string $value = null, 
    array $attributes = []
): Tel

public function inputText(
    string $name, 
    string $value = null, 
    array $attributes = []
): Text

public function inputTextarea(
    string $name, 
    string $value = null, 
    array $attributes = []
): Textarea

public function inputTime(
    string $name, 
    string $value = null, 
    array $attributes = []
): Time

public function inputUrl(
    string $name, 
    string $value = null, 
    array $attributes = []
): Url

public function inputWeek(
    string $name, 
    string $value = null, 
    array $attributes = []
): Week

public function label(
    string $label, 
    array $attributes = [], 
    bool $raw = false
): string

public function link(
    string $indent = '    ', 
    string $delimiter = PHP_EOL
): Link

public function meta(
    string $indent = '    ', 
    string $delimiter = PHP_EOL
): Meta

public function ol(
    string $text, 
    array $attributes = [], 
    bool $raw = false
): Ol

public function script(
    string $indent = '    ', 
    string $delimiter = PHP_EOL
): Script

public function style(
    string $indent = '    ', 
    string $delimiter = PHP_EOL
): Style

public function title(
    string $indent = '    ', 
    string $delimiter = PHP_EOL
): Title

public function ul(
    string $text, 
    array $attributes = [], 
    bool $raw = false
): Ul
<?php

use Phalcon\Di\FactoryDefault;

$container = new FactoryDefault();

$result = $container->tag->a('https://phalcon.io', 'Phalcon Website');

$image  = $container
    ->tag
    ->img('https://phalcon.io/img/phalcon.png')
;

$result = $container
    ->tag
    ->a(
        'https://phalcon.io', 
        $image,
        true
    )
;

辅助工具

Phalcon\Html\TagFactory使用的所有辅助工具Phalcon\Html\Helper命名空间下。如需要,你可以单独创建这些类中的每一个,或者也可以使用如上所示的标签工厂。

注意

下方代码和输出已为可读性进行了格式化

a

Phalcon\Html\Helper\Anchor创建一个<a>(超链接)标签。

参数 描述
string $href href 属性
string $text 要显示的文本
array $attributes = [] 附加属性 (键/值)
bool $raw = false 是否转义文本
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Anchor;

$escaper = new Escaper();
$helper  = new Anchor($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('/myurl', 'click<>me', $options);
// <a href="/myurl" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">
//     click&lt;&gt;me
// </a>

base

Phalcon\Html\Helper\Base创建一个<base> tag.

参数 描述
string $href href 属性
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Base;

$escaper = new Escaper();
$helper  = new Base($escaper);
$options = [
    'target' => '_blank',
];

echo $helper('/myurl', $options);
// <base href="/myurl" 
//    target="_blank">

Phalcon\Html\Helper\Breadcrumbs根据现有的或传递的模板创建面包屑导航的 HTML。

参数 描述
string $indent 缩进
string $delimiter 分隔符

许多 Web 应用程序中常见的 HTML 元素之一是面包屑。这些链接通常由空格或/字符通常用来表示应用程序的树状结构。目的是给用户提供另一个简单直观的方式来浏览整个应用程序。

一个例子是具有admin模块、invoices区域和view invoice页面的应用程序。通常情况下,您会先选择admin模块,然后从链接中选择invoices(列表),再点击列表中的某张发票进行查看。为了表示这种树状结构,显示的面包屑可能是:

Home / Admin / Invoices / Viewing Invoice [1234]
上面的每个词(最后一个除外)都是指向相应页面的链接。这样用户可以快速返回到不同的区域,而不需要点击后退按钮或使用其他菜单。

Phalcon\Html\Helper\Breadcrumbs提供添加文本、URL、图标和属性到每个元素的功能。调用render()生成的 HTML 将根据模板定义的结构来格式化每个面包屑,每个元素之间会用默认的分隔符分开。<li>/</li>.

方法

public function __invoke(
    string $indent = '    ',
    string $delimiter = PHP_EOL
): static 

设置缩进和分隔符,并返回对象自身

public function add(
    string $text,
    string $link = '',
    string $icon = '',
    array $attributes = []
): static 
添加一个新的面包屑。

// Adding a crumb with a link
$breadcrumbs->add("Home", "/");

// Adding a crumb with added attributes
$breadcrumbs->add("Home", "/", ["class" => "main"]);

// Adding a crumb without a link (normally the last one)
$breadcrumbs->add("Users");
public function clear(): void

清除所有面包屑。

$breadcrumbs->clear()
public function clearAttributes(): static

清除父元素的属性

public function getAttributes(): array

返回父元素的属性

public function getSeparator(): string
public function getTemplate(): array

返回当前模板

public function remove(int $index): void

按索引移除一个面包屑项。

$breadcrumbs->remove(2);
public function render(): string

基于之前设置的模板渲染并输出面包屑。

echo $breadcrumbs->render();
public function setAttributes(array $attributes): static

设置父元素的属性

public function setSeparator(string $separator): static

设置分隔符

public function setTemplate(
    string $main,
    string $line,
    string $last
): static 

设置 HTML 模板

public function toArray(): array

返回内部面包屑数组

模板

默认模板有:

主体

<nav%attributes%>
<ol>
%items%
</ol>
</nav>

<li%attributes%><a href="%link%">%icon%%text%</a></li>

最后一个元素

<li><span%attributes%>%text%</span></li>

可以提供不同的模板以满足应用需求。可以使用setTemplate()方法设置模板。模板是一个字符串,可以包含占位符,在渲染面包屑时会被实际值替换。

可用的占位符包括:

占位符 描述 应用于
%attributes% 父元素属性 父级
%items% 项目列表 父级
%link% 元素的链接 元素
%text% 元素的文本 元素
%icon% 元素的图标 元素
%attributes% 元素的属性 元素

分隔符

分隔符是各个面包屑之间打印的内容。默认情况下,分隔符是<li>/</li>。你可以通过调用setSeparator().

示例

我们的程序需要以下格式展示面包屑导航:

Home > Admin > Invoices > Viewing Invoice [1234]
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\TagFactory;

$escaper = new Escaper();
$tagFactory = new TagFactory($escaper);

$separator = '
<span class="mx-5 text-gray-500 dark:text-gray-300 rtl:-scale-x-100">
    <svg xmlns="http://www.w3.org/2000/svg" 
         class="w-5 h-5" 
         viewBox="0 0 20 20" 
         fill="currentColor">
        <path fill-rule="evenodd" 
              d="M7.293 14.707a1 1 0 010-1.414L10.586 
                10 7.293 6.707a1 1 0 011.414-1.414l4 
                4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" 
              clip-rule="evenodd" />
    </svg>
</span>
';

$homeIcon = '
<svg xmlns="http://www.w3.org/2000/svg" 
     class="w-5 h-5" viewBox="0 0 20 20" 
     fill="currentColor">
    <path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 
            0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 
            0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 
            0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 
            0 001.414-1.414l-7-7z" />
</svg>
';

$mainTemplate = '
<div%attributes%>
%items%
</div>
';
$lineTemplate = '
<a href="%link%"%attributes%>
    %icon%%text%
</a>
';
$lastTemplate = '
<a href="%link%"%attributes%>
    %icon%%text%
</a>
';

$homeAttributes = [
    'class' => 'text-gray-600 dark:text-gray-200',
];
$lineAttributes = [
    'class' => 'text-gray-600 dark:text-gray-200 hover:underline',
];
$lastAttributes = [
    'class' => 'text-blue-600 dark:text-blue-400 hover:underline',
];

$breadcrumbs
    ->setTemplate($mainTemplate, $lineTemplate, $lastTemplate)
    ->setSeparator($separator)
    ->add('', '#', $homeIcon, $homeAttributes)
    ->add('Admin', '#', '', $lineAttributes)
    ->add('Invoices', '#', '', $lineAttributes)
    ->add('Viewing Invoice [1234]', '#', '', $lastAttributes)
;

echo $breadcrumbs->render();    

输出 HTML:

<div class="flex items-center py-4 overflow-x-auto whitespace-nowrap">
    <a href="#" class="text-gray-600 dark:text-gray-200">
        <svg xmlns="http://www.w3.org/2000/svg" 
             class="w-5 h-5" 
             viewBox="0 0 20 20" 
             fill="currentColor">
            <path d="M10.707 2.293a1 1 
                0 00-1.414 0l-7 7a1 1 
                0 001.414 1.414L4 10.414V17a1 1 
                0 001 1h2a1 1 0 001-1v-2a1 1 
                0 011-1h2a1 1 0 011 1v2a1 1 
                0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 
                0 001.414-1.414l-7-7z" />
        </svg>
    </a>

    <span class="mx-5 text-gray-500 dark:text-gray-300 rtl:-scale-x-100">
        <svg xmlns="http://www.w3.org/2000/svg" 
             class="w-5 h-5" 
             viewBox="0 0 20 20" 
             fill="currentColor">
            <path fill-rule="evenodd" 
                  d="M7.293 14.707a1 1 
                    0 010-1.414L10.586 10 7.293 6.707a1 1 
                    0 011.414-1.414l4 4a1 1 
                    0 010 1.414l-4 4a1 1 
                    0 01-1.414 0z" 
                  clip-rule="evenodd" />
        </svg>
    </span>

    <a href="#" class="text-gray-600 dark:text-gray-200 hover:underline">
        Admin
    </a>

    <span class="mx-5 text-gray-500 dark:text-gray-300 rtl:-scale-x-100">
        <svg xmlns="http://www.w3.org/2000/svg" 
             class="w-5 h-5" 
             viewBox="0 0 20 20" 
             fill="currentColor">
            <path fill-rule="evenodd" 
                  d="M7.293 14.707a1 1 
                    0 010-1.414L10.586 10 7.293 6.707a1 1 
                    0 011.414-1.414l4 4a1 1 
                    0 010 1.414l-4 4a1 1 
                    0 01-1.414 0z" 
                  clip-rule="evenodd" />
        </svg>
    </span>

    <a href="#" class="text-gray-600 dark:text-gray-200 hover:underline">
        Invoices
    </a>

    <span class="mx-5 text-gray-500 dark:text-gray-300 rtl:-scale-x-100">
        <svg xmlns="http://www.w3.org/2000/svg" 
             class="w-5 h-5" 
             viewBox="0 0 20 20" fill="currentColor">
            <path fill-rule="evenodd" 
                  d="M7.293 14.707a1 1 
                    0 010-1.414L10.586 10 7.293 6.707a1 1 
                    0 011.414-1.414l4 4a1 1 
                    0 010 1.414l-4 4a1 1 
                    0 01-1.414 0z" 
                  clip-rule="evenodd" />
        </svg>
    </span>

    <a href="#" class="text-blue-600 dark:text-blue-400 hover:underline">
        Viewing Invoice [1234]
    </a>
</div>

body

Phalcon\Html\Helper\Body创建一个<body> tag.

参数 描述
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Body;

$escaper = new Escaper();
$helper  = new Body($escaper);
$options = [
    'class' => 'my-class',
    'id'    => 'my-id',
];

echo $helper($options);
// <body id="my-id" class="my-class">

注意

此助手仅创建开始标签。你需要使用<body>助手来生成结束Close标签的关闭</body> tag.

button

Phalcon\Html\Helper\Button创建一个<button> tag.

参数 描述
string $text 要显示的文本
array $attributes = [] 附加属性 (键/值)
bool $raw = false 是否转义文本
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Button;

$escaper = new Escaper();
$helper  = new Button($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('click<>me', $options);
// <button 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">
//     click&lt;&gt;me
// </button>

close

Phalcon\Html\Helper\Close创建一个闭合标签。

参数 描述
string $text 要显示的文本
bool $raw = false 是否转义文本
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Close;

$escaper = new Escaper();
$helper  = new Close($escaper);

echo $helper('form');
// </form>

doctype

Phalcon\Html\Helper\Doctype创建一个<doctype> tag.

参数 描述
int $flag 要显示的文本
string $delimiter 是否转义文本
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Doctype;

$escaper = new Escaper();
$helper  = new Doctype($escaper);

echo $helper(Doctype::XHTML11, '-:-');
// <!DOCTYPE html
//     PUBLIC "-//W3C//DTD XHTML 1.1//EN"-:-
//     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">-:-

element

Phalcon\Html\Helper\Element根据传入的name.

参数 描述
string $tag href 属性
string $text 要显示的文本
array $attributes = [] 附加属性 (键/值)
bool $raw = false 是否转义文本
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Element;

$escaper = new Escaper();
$helper  = new Element($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('address', 'click<>me', $options);
// <address 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">
//     click&lt;&gt;me
// </address>

form

Phalcon\Html\Helper\Form创建一个<form> tag.

参数 描述
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Form;

$escaper = new Escaper();
$helper  = new Form($escaper);
$options = [
    'class'   => 'my-class',
    'name'    => 'my-name',
    'id'      => 'my-id',
    'method'  => 'post',
    'enctype' => 'multipart/form-data'
];

echo $helper($options);
// <form 
//    id="my-id" 
//    name="my-name" 
//    class="my-class"
//    method="post"
//    enctype="multipart/form-data">

注意

此助手仅创建开始标签。你需要使用<form>助手来生成结束Close标签的关闭</form> tag.

img

Phalcon\Html\Helper\Img创建一个<img> tag.

参数 描述
string $src 图片源地址
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Img;

$escaper = new Escaper();
$helper  = new Img($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('/my-url', $options);
// <img 
//    src="/my-url" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputCheckbox

[Phalcon\Html\Helper\Checkbox][html-helper-checkbox] 创建一个<input type="checkbox"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)

方法

public function label(array $attributes)
设置复选框的标签

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Checkbox;

$escaper = new Escaper();
$helper  = new Checkbox($escaper);
$options = [
    'id'        => 'my-id',
    'unchecked' => 'no',
    'checked'   => 'yes',
];

$result = $helper('my-name', 'yes', $options);

echo $result;
// <hidden name="my_name" value="no">
// <label for="my_id">
//     <input type="checkbox"
//         id="my_id"
//         name="x_name"
//         value="yes"
//         checked="checked" />
//     some text
// </label>

inputColor

[Phalcon\Html\Helper\Color][html-helper-color] 创建一个<input type="color"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Color;

$escaper = new Escaper();
$helper  = new Color($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="color"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputDate

[Phalcon\Html\Helper\Date][html-helper-date] 创建一个<input type="date"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Date;

$escaper = new Escaper();
$helper  = new Date($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="date"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputDatetime

[Phalcon\Html\Helper\DateTime][html-helper-datetime] 创建一个<input type="datetime"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\DateTime;

$escaper = new Escaper();
$helper  = new DateTime($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="datetime"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputDatetimeLocal

[Phalcon\Html\Helper\DateTimeLocal][html-helper-datetime-local] 创建一个<input type="datetime-local"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\DateTimeLocal;

$escaper = new Escaper();
$helper  = new DateTimeLocal($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="datetime-local"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputEmail

[Phalcon\Html\Helper\Email][html-helper-email] 创建一个<input type="email"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Email;

$escaper = new Escaper();
$helper  = new Email($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="email"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputFile

[Phalcon\Html\Helper\File][html-helper-file] 创建一个<input type="file"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\File;

$escaper = new Escaper();
$helper  = new File($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="file"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputHidden

[Phalcon\Html\Helper\Hidden][html-helper-hidden] 创建一个<input type="hidden"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Hidden;

$escaper = new Escaper();
$helper  = new Hidden($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="hidden"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputImage

[Phalcon\Html\Helper\Image][html-helper-image] 创建一个<input type="image"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Image;

$escaper = new Escaper();
$helper  = new Image($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="image"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputMonth

[Phalcon\Html\Helper\Month][html-helper-month] 创建一个<input type="month"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Month;

$escaper = new Escaper();
$helper  = new Month($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="month"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

input

[Phalcon\Html\Helper\Input][html-helper-input] 创建一个<input> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)

方法

public function setType(string $type)
设置输入元素的类型

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Input;

$escaper = new Escaper();
$helper  = new Input($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

$result = $helper('test-name', "test-value", $options);

$result->setType('month');

echo $result;
// <input type="month"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputNumeric

[Phalcon\Html\Helper\Numeric][html-helper-numeric] 创建一个<input type="numeric"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Numeric;

$escaper = new Escaper();
$helper  = new Numeric($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="numeric"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputPassword

[Phalcon\Html\Helper\Password][html-helper-password] 创建一个<input type="password"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Password;

$escaper = new Escaper();
$helper  = new Password($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="password"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputRadio

[Phalcon\Html\Helper\Radio][html-helper-radio] 创建一个<input type="radio"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)

方法

public function label(array $attributes)
设置单选按钮的标签

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Radio;

$escaper = new Escaper();
$helper  = new Radio($escaper);
$options = [
    'id'        => 'my-id',
    'unchecked' => 'no',
    'checked'   => 'yes',
];

$result = $helper('my-name', 'yes', $options);

echo $result;
// <hidden name="my_name" value="no">
// <label for="my_id">
//     <input type="radio"
//         id="my_id"
//         name="x_name"
//         value="yes"
//         checked="checked" />
//     some text
// </label>

inputRange

[Phalcon\Html\Helper\Range][html-helper-range] 创建一个<input type="range"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Range;

$escaper = new Escaper();
$helper  = new Range($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="range"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputSearch

[Phalcon\Html\Helper\Search][html-helper-search] 创建一个<input type="search"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Search;

$escaper = new Escaper();
$helper  = new Search($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="search"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputSelect

[Phalcon\Html\Helper\Select][html-helper-select] 创建一个<select> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)

方法

public function add(
    string $text,
    string $value = null,
    array $attributes = [],
    bool $raw = false
): Select
向列表中添加一个元素

public function addPlaceholder(
    string $text,
    mixed $value = null,
    array $attributes = [],
    bool $raw = false
): Select
向元素添加一个占位符

public function optGroup(
    string $label = null,
    array $attributes = []
): Select
创建选项组

public function selected(string $selected): Select
设置选定的选项

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Select;

$escaper = new Escaper();
$helper  = new Select($escaper);

$options = [
    'id' => 'carsList',
];

$result = $helper('    ', PHP_EOL, $options);
$result
    ->add("Ferrari", "1", ["class" => "active"])
    ->add("Ford", "2")
    ->add("Dodge", "3")
    ->add("Toyota", "4")
    ->optGroup(
        'oneLabel',
        [
            'class' => 'form-input',
        ]
    )
    ->addPlaceholder(
        'Choose & Car...',
        "0",
        [],
        true,
    )
    ->selected("3")
;

echo $result;
//
//    <select id="carsList">
//        <optgroup class="form-input" label="oneLabel">
//            <option value="0">Choose & Car...</option>
//            <option value="1" class="active">Ferrari</option>
//            <option value="2">Ford</option>
//            <option value="3" selected="selected">Dodge</option>
//            <option value="4">Toyota</option>
//        </optgroup>
//    </select>"

inputSubmit

[Phalcon\Html\Helper\Submit][html-helper-submit] 创建一个<input type="submit"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Submit;

$escaper = new Escaper();
$helper  = new Submit($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="submit"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputTel

[Phalcon\Html\Helper\Tel][html-helper-tel] 创建一个<input type="tel"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Tel;

$escaper = new Escaper();
$helper  = new Tel($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="tel"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputText

[Phalcon\Html\Helper\Text][html-helper-text] 创建一个<input type="text"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Text;

$escaper = new Escaper();
$helper  = new Text($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="text"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputTextarea

[Phalcon\Html\Helper\TextArea][html-helper-textarea] 创建一个<textarea>标签

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\TextArea;

$escaper = new Escaper();
$helper  = new TextArea($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('click<>me', $options);
// <textarea 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">
//     click&lt;&gt;me
// </textarea>

inputTime

[Phalcon\Html\Helper\Time][html-helper-time] 创建一个<input type="time"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Time;

$escaper = new Escaper();
$helper  = new Time($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="time"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputUrl

[Phalcon\Html\Helper\Url][html-helper-url] 创建一个<input type="url"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Url;

$escaper = new Escaper();
$helper  = new Url($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="url"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

inputWeek

[Phalcon\Html\Helper\Week][html-helper-week] 创建一个<input type="week"> tag.

参数 描述
string $name 名称
string $value
array $attributes = [] 附加属性 (键/值)
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Input\Week;

$escaper = new Escaper();
$helper  = new Week($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper('test-name', "test-value", $options);
// <input type="week"
//    value="test-value" 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

label

Phalcon\Html\Helper\Label创建一个<label> tag.

参数 描述
string $label 标签
array $attributes = [] 附加属性 (键/值)
bool $raw = false 是否转义文本
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Label;

$escaper = new Escaper();
$helper  = new Label($escaper);
$options = [
    'class' => 'my-class',
    'name'  => 'my-name',
    'id'    => 'my-id',
];

echo $helper($options);
// <label 
//    id="my-id" 
//    name="my-name" 
//    class="my-class">

注意

此助手仅创建开始标签。你需要使用<label>助手来生成结束Close标签的关闭</label> tag.

Phalcon\Html\Helper\Link创建一个<link> tag.

参数 描述
string $indent 缩进
string $delimiter 分隔符
<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Link;

$escaper = new Escaper();
$helper  = new Link($escaper);

$result = $helper();
$result
    ->add('https://phalcon.io/page/1', ['rel' => 'prev'])
    ->add('https://phalcon.io/page/2', ['rel' => 'next'])
;

echo $result;
// <link rel="prev" href="https://phalcon.io/page/1" />
// <link rel="next" href="https://phalcon.io/page/2" />

meta

Phalcon\Html\Helper\Meta创建一个<meta> tag.

参数 描述
string $indent 缩进
string $delimiter 分隔符

方法

public function add(array $attributes = []): Meta
向列表中添加一个元素

public function addHttp(string $httpEquiv, string $content): Meta
添加一条 HTTP meta 标签

public function addName(string name, string content) -> <Meta>
添加一条 name meta 标签

public function addProperty(string name, string content) -> <Meta>
添加一条 property meta 标签

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Meta;

$escaper = new Escaper();
$helper  = new Meta($escaper);

$result = $helper();

$result
    ->add(
        [
            "charset" => 'utf-8',
        ]
    )
    ->addHttp("X-UA-Compatible", "IE=edge")
    ->addName("generator", "Phalcon")
    ->addProperty("org:url", "https://phalcon.io")
;

echo $result;
//    <meta charset="utf-8">
//    <meta http-equiv="X-UA-Compatible" content="IE=edge">
//    <meta name="generator" content="Phalcon">
//    <meta property="org:url" content="https://phalcon.io">

ol

Phalcon\Html\Helper\Ol创建一个<ol> tag.

参数 描述
string $indent 缩进
string $delimiter 分隔符

方法

public function add(
    string $text,
    array $attributes = [],
    bool $raw = false
): Ol
向列表中添加一个元素

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Ol;

$escaper = new Escaper();
$helper  = new Ol($escaper);
$options = [
    'id' => 'carsList',
]

$result = $helper('    ', PHP_EOL, $options);

$result
    ->add("Ferrari", "1", ["class" => "active"])
    ->add("Ford", "2")
    ->add("Dodge", "3")
    ->add("Toyota", "4")
;

echo $result;
// <ol id="carsList">
//     <li class="active">Ferrari</li>
//     <li>> Ford</li>
//     <li>> Dodge</li>
//     <li>> Toyota</li>
// </ol>

script

Phalcon\Html\Helper\Script创建一个<script> tag.

参数 描述
string $indent 缩进
string $delimiter 分隔符

方法

public function add(
    string $url,
    array $attributes = []
): Script
向列表中添加一个 URL

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Script;

$escaper = new Escaper();
$helper  = new Script($escaper);

$result = $helper();

$result
    ->add('/js/custom.js')
    ->add('/js/print.js', ['ie' => 'active'])
;

echo $result;
//    <script type="application/javascript" 
//            src="/js/custom.js"></script>
//    <script type="application/javascript" 
//            src="/js/print.js" ie="active"></script>

style

Phalcon\Html\Helper\Script创建一个<link> tag.

参数 描述
string $indent 缩进
string $delimiter 分隔符

方法

public function add(
    string $url,
    array $attributes = []
): Script
向列表中添加一个 URL

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Script;

$escaper = new Escaper();
$helper  = new Script($escaper);

$result = $helper();

$result
    ->add('custom.css')
    ->add('print.css', ['media' => 'print'])
;

echo $result;
//    <link rel="stylesheet" type="text/css"
//        href="custom.css" media="screen" />
//    <link rel="stylesheet" type="text/css"
//        href="print.css" media="print" />

title

Phalcon\Html\Helper\Title创建一个<title> tag.

参数 描述
string $indent 缩进
string $delimiter 分隔符

方法

public function append(
    string $text, 
    bool $raw = false
): Title
在当前文档标题后追加文本

public function get(): string
返回标题

public function set(
    string $text, 
    bool $raw = false
)): Title
设置标题

public function setSeparator(
    string $separator, 
    bool $raw = false
)): Title
设置分隔符

public function prepend(
    string $text, 
    bool $raw = false
): Title
在当前文档标题前插入文本

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Ul;

$escaper = new Escaper();
$helper  = new Ul($escaper);
$options = [
    'id' => 'carsList',
]

$result = $helper();

$result
    ->setSeparator(' | ')
    ->set('<Dodge>')
    ->append('< Ferrari', true)
    ->prepend('Ford <')
;

echo $result->get();
// &lt; Dodge &gt;

echo $result;
// <title>Ford > | &lt; Dodge &gt; | < Ferrari</title>

ul

Phalcon\Html\Helper\Ul创建一个<ul> tag.

参数 描述
string $indent 缩进
string $delimiter 分隔符

方法

public function add(
    string $text,
    array $attributes = [],
    bool $raw = false
): Ol
向列表中添加一个元素

<?php

use Phalcon\Html\Escaper;
use Phalcon\Html\Helper\Ul;

$escaper = new Escaper();
$helper  = new Ul($escaper);
$options = [
    'id' => 'carsList',
]

$result = $helper('    ', PHP_EOL, $options);

$result
    ->add("Ferrari", "1", ["class" => "active"])
    ->add("Ford", "2")
    ->add("Dodge", "3")
    ->add("Toyota", "4")
;

echo $result;
// <ul id="carsList">
//     <li class="active">Ferrari</li>
//     <li>> Ford</li>
//     <li>> Dodge</li>
//     <li>> Toyota</li>
// </ul>
无噪 Logo
无噪文档
25 年 6 月翻译
版本号 5.9
文档源↗