HTML 輔助函數(shù)?

HTML 輔助函數(shù)包含的函數(shù)輔助 HTML 運(yùn)行。

加載 HTML 輔助函數(shù)?

HTML 輔助函數(shù)使用下面的代碼加載:

helper('html');

通用函數(shù)?

下面的函數(shù)是通用的:

img([$src = ''[, $indexPage = false[, $attributes = '']]])?
param mixed $src:
 Image 原始碼數(shù)據(jù)
param bool $indexPage:
 是否像路由的 URI 字符串處理 $src
param mixed $attributes:
 HTML 屬性
returns:HTML image tag
rtype:string

讓你創(chuàng)建 HTML <img /> tags. 第一個參數(shù)包含 image 原始碼。事例:

echo img('images/picture.jpg');
// <img src="http://site.com/images/picture.jpg" />

有一個可選擇的第二參數(shù)是特定的 true/false 值并規(guī)定如果 src 將經(jīng)由 $config['indexPage'] 被添加到地址并創(chuàng)建有明確說明的頁面。推測起來,假如你正在使用一個 media 控制器那將是自以為是的:

echo img('images/picture.jpg', true);
// <img src="http://site.com/index.php/images/picture.jpg" alt="" />

此外,組合數(shù)組能被作為第一參數(shù)傳達(dá),為了完成控制額外的所有屬性和值。 如果不提供 alt 屬性,CodeIgniter 將產(chǎn)生空字符串。

例如:

$imageProperties = [
    'src'    => 'images/picture.jpg',
    'alt'    => 'Me, demonstrating how to eat 4 slices of pizza at one time',
    'class'  => 'post_images',
    'width'  => '200',
    'height' => '200',
    'title'  => 'That was quite a night',
    'rel'    => 'lightbox'
];

img($imageProperties);
// <img src="http://site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a night" rel="lightbox" />
參數(shù):
  • $href (string) – 鏈接文件的原始碼
  • $rel (string) – 關(guān)系類型
  • $type (string) – 關(guān)系文件夾的類型
  • $title (string) – 鏈接主題
  • $media (string) – 媒體類型
  • $indexPage (bool) – 是否像路由的 URI 字符串處理 $src
返回:

HTML link tag

返回類型:

string

讓你創(chuàng)建 HTML <link /> tags. 這對樣式表鏈接是有用的,和其他鏈接一樣。參數(shù)是 href ,帶著可選擇的 rel, type, title, mediaindexPage.

indexPage 是 boolean 值并規(guī)定如果 href 將經(jīng)由 $config['indexPage'] 被添加到地址并創(chuàng)建有明確說明的頁面。

例如:

echo link_tag('css/mystyles.css');
// <link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />

更多示例:

echo link_tag('favicon.ico', 'shortcut icon', 'image/ico');
// <link href="http://site.com/favicon.ico" rel="shortcut icon" type="image/ico" />

echo link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
// <link href="http://site.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />

間隔地,為了完全控制額外的所有屬性和值組合數(shù)組能被傳達(dá)到 link_tag() 函數(shù):

$link = [
    'href'  => 'css/printer.css',
    'rel'   => 'stylesheet',
    'type'  => 'text/css',
    'media' => 'print'
];

echo link_tag($link);
// <link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />
script_tag([$src = ''[, $indexPage = false]])?
參數(shù):
  • $src (mixed) – JavaScript 文件的原始碼名稱
  • $indexPage (bool) – 是否像路由的 URI 字符串處理 $src
返回:

HTML script tag

返回類型:

string

讓你創(chuàng)建 HTML <script></script> tags. 參數(shù)是 src, 與可選的 indexPage 一起.

indexPage 是 boolean 值并規(guī)定如果 src 將經(jīng)由 $config['indexPage'] 被添加到地址并創(chuàng)建有明確說明的頁面。

例如:

echo script_tag('js/mystyles.js');
// <script src="http://site.com/js/mystyles.js" type="text/javascript"></script>

間隔地,為了完全控制額外的所有屬性和值組合數(shù)組能被通過 script_tag() 函數(shù):

$script = ['src'  => 'js/printer.js'];

echo script_tag($script);
// <script src="http://site.com/js/printer.js" type="text/javascript"></script>
ul($list[, $attributes = ''])?
param array $list:
 目錄登錄
param array $attributes:
 HTML 屬性
returns:HTML-formatted 無序目錄
rtype:string

容許你從簡單或者多倍空間的數(shù)組產(chǎn)生無序 HTML 目錄。事例::

    $list = [
        'red',
        'blue',
        'green',
        'yellow'
    ];

    $attributes = [
        'class' => 'boldlist',
        'id'    => 'mylist'
    ];

    echo ul($list, $attributes);

上文的代碼將產(chǎn)生下文這樣地 HTML 代碼:

.. code-block:: html

    <ul class="boldlist" id="mylist">
        <li>red</li>
        <li>blue</li>
        <li>green</li>
        <li>yellow</li>
    </ul>

下面是更復(fù)雜的事例,使用多維空間的數(shù)組::

    $attributes = [
        'class' => 'boldlist',
        'id'    => 'mylist'
    ];

    $list = [
        'colors' => [
            'red',
            'blue',
            'green'
        ],
        'shapes' => [
            'round',
            'square',
            'circles' => [
                'ellipse',
                'oval',
                'sphere'
            ]
        ],
        'moods'  => [
            'happy',
            'upset'   => [
                'defeated' => [
                    'dejected',
                    'disheartened',
                    'depressed'
                ],
                'annoyed',
                'cross',
                'angry'
            ]
        ]
    ];

    echo ul($list, $attributes);

上文的代碼將產(chǎn)生這樣的 HTML 前端代碼:

.. code-block:: html

    <ul class="boldlist" id="mylist">
        <li>colors
            <ul>
                <li>red</li>
                <li>blue</li>
                <li>green</li>
            </ul>
        </li>
        <li>shapes
            <ul>
                <li>round</li>
                <li>suare</li>
                <li>circles
                    <ul>
                        <li>elipse</li>
                        <li>oval</li>
                        <li>sphere</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>moods
            <ul>
                <li>happy</li>
                <li>upset
                    <ul>
                        <li>defeated
                            <ul>
                                <li>dejected</li>
                                <li>disheartened</li>
                                <li>depressed</li>
                            </ul>
                        </li>
                        <li>annoyed</li>
                        <li>cross</li>
                        <li>angry</li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
ol($list, $attributes = '')?
參數(shù):
  • $list (array) – 目錄登錄
  • $attributes (array) – HTML 屬性
返回:

HTML-formatted 有序目錄

返回類型:

string

完全相似于 ul() ,為了代替有序目錄 <ul> 它僅產(chǎn)生 <ol> tag.

video($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])?
參數(shù):
  • $src (mixed) – 任一原始碼字符串或者原始碼的數(shù)組. 參看 source() 函數(shù)
  • $unsupportedMessage (string) – 如果 media tag 不支持由瀏覽器提供的消息會顯示
  • $attributes (string) – HTML 屬性
  • $tracks (array) – 在數(shù)組里使用追蹤函數(shù)。參看 track() 函數(shù)
  • $indexPage (bool) –
返回:

HTML-formatted 影像元素

返回類型:

string

容許你從簡單的或者原始碼數(shù)組產(chǎn)生 HTML 影像元素。事例:

$tracks =
[
    track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No'),
    track('subtitles_yes.vtt', 'subtitles', 'yes', 'Norwegian Yes')
];

echo video('test.mp4', 'Your browser does not support the video tag.', 'controls');

echo video
(
    'http://www.codeigniter.com/test.mp4',
    'Your browser does not support the video tag.',
    'controls',
    $tracks
);

echo video
(
    [
      source('movie.mp4', 'video/mp4', 'class="test"'),
      source('movie.ogg', 'video/ogg'),
      source('movie.mov', 'video/quicktime'),
      source('movie.ogv', 'video/ogv; codecs=dirac, speex')
    ],
    'Your browser does not support the video tag.',
    'class="test" controls',
    $tracks
 );

上文的編碼將產(chǎn)生這樣地 HTML 前端代碼:

<video src="test.mp4" controls>
  Your browser does not support the video tag.
</video>

<video src="http://www.codeigniter.com/test.mp4" controls>
  <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
  <track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />
  Your browser does not support the video tag.
</video>

<video class="test" controls>
  <source src="movie.mp4" type="video/mp4" class="test" />
  <source src="movie.ogg" type="video/ogg" />
  <source src="movie.mov" type="video/quicktime" />
  <source src="movie.ogv" type="video/ogv; codecs=dirac, speex" />
  <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
  <track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />
  Your browser does not support the video tag.
</video>
audio($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])?
參數(shù):
  • $src (mixed) – 任一原始碼字符串或者原始碼數(shù)組。參看 source() 函數(shù)
  • $unsupportedMessage (string) – 如果 media tag 不支持由瀏覽器提供的消息會顯示
  • $attributes (string) –
  • $tracks (array) – 在數(shù)組里用追蹤函數(shù). 參看 track() 函數(shù)
  • $indexPage (bool) –
返回:

HTML-formatted 音頻元素

返回類型:

string

完全相似于 video(), 它僅僅產(chǎn)生 <audio> tag 代替 <video>.

source($src = ''[, $type = false[, $attributes = '']])?
param string $src:
 media source的路徑
param bool $type:
 以可選擇的編碼參數(shù)的資源 MIME(多用途的網(wǎng)絡(luò)郵件擴(kuò)充協(xié)議)類型
param array $attributes:
 HTML 屬性
returns:HTML source tag
rtype:string

讓你創(chuàng)建 HTML <source /> tags. 第一個參數(shù)包含起源 source. 例如:

echo source('movie.mp4', 'video/mp4', 'class="test"');
// <source src="movie.mp4" type="video/mp4" class="test" />
embed($src = ''[, $type = false[, $attributes = ''[, $indexPage = false]]])?
param string $src:
 資源的路徑 embed
param bool $type:
 MIME(多用途的網(wǎng)絡(luò)郵件擴(kuò)充協(xié)議)類型
param array $attributes:
 HTML 屬性
param bool $indexPage:
 
returns:HTML embed tag
rtype:string

讓你創(chuàng)建 HTML <embed /> tags.第一參數(shù)包含 embed source. 例如:

echo embed('movie.mov', 'video/quicktime', 'class="test"');
// <embed src="movie.mov" type="video/quicktime" class="test"/>
object($data = ''[, $type = false[, $attributes = '']])?
參數(shù):
  • $data (string) – 資源 URL
  • $type (bool) – 資源的內(nèi)容類型
  • $attributes (array) – HTML 屬性
  • $params (array) – 在數(shù)組里使用 param 函數(shù)。參看 param() 函數(shù)
返回:

HTML object tag

返回類型:

string

讓你創(chuàng)建 HTML <object /> tags. 第一參數(shù)包含 object data. 事例:

echo object('movie.swf', 'application/x-shockwave-flash', 'class="test"');

echo object
(
    'movie.swf',
    'application/x-shockwave-flash',
    'class="test"',
    [
        param('foo', 'bar', 'ref', 'class="test"'),
        param('hello', 'world', 'ref', 'class="test"')
    ]
);

上文編碼將產(chǎn)生這樣的 HTML 前端代碼:

<object data="movie.swf" class="test"></object>

<object data="movie.swf" class="test">
  <param name="foo" type="ref" value="bar" class="test" />
  <param name="hello" type="ref" value="world" class="test" />
</object>
param($name = ''[, $type = false[, $attributes = '']])?
參數(shù):
  • $name (string) – 參數(shù)的名字
  • $value (string) – 參數(shù)的值
  • $attributes (array) – HTML 屬性
返回:

HTML param tag

返回類型:

string

讓你創(chuàng)建 HTML <param /> tags. 第一個參數(shù)包含 param source. 事例:

echo param('movie.mov', 'video/quicktime', 'class="test"');
// <param src="movie.mov" type="video/quicktime" class="test"/>
track($name = ''[, $type = false[, $attributes = '']])?
參數(shù):
  • $name (string) – 參數(shù)的名稱
  • $value (string) – 參數(shù)的值
  • $attributes (array) – HTML 屬性
返回:

HTML track tag

返回類型:

string

產(chǎn)生一個跟蹤元素去具體指定時間的軌跡。在 WebVVT 格式里軌跡已被格式化。事例:

echo track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No');
// <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />
doctype([$type = 'html5'])?
參數(shù):
  • $type (string) – Doctype 名字
返回:

HTML DocType tag

返回類型:

string

幫助你產(chǎn)生 document type 聲明, 而 DTD’s. HTML 5 是默認(rèn)使用的,但是許多 doctypes 是通用的。

事例:

echo doctype();
// <!DOCTYPE html>

echo doctype('html4-trans');
// <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

接下來的是重定義 doctype 選擇的目錄。 這些是可設(shè)置的, 被從 application/Config/DocTypes.php 出棧,或者在你的 .env 結(jié)構(gòu)里它們能被加載。

文檔類型 選項 結(jié)果
XHTML 1.1 xhtml11 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
XHTML 1.0 Strict xhtml1-strict <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
XHTML 1.0 Transitional xhtml1-trans <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
XHTML 1.0 Frameset xhtml1-frame <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Frameset//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd”>
XHTML Basic 1.1 xhtml-basic11 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML Basic 1.1//EN” “http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd”>
HTML 5 html5 <!DOCTYPE html>
HTML 4 Strict html4-strict <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
HTML 4 Transitional html4-trans <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
HTML 4 Frameset html4-frame <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Frameset//EN” “http://www.w3.org/TR/html4/frameset.dtd”>
MathML 1.01 mathml1 <!DOCTYPE math SYSTEM “http://www.w3.org/Math/DTD/mathml1/mathml.dtd”>
MathML 2.0 mathml2 <!DOCTYPE math PUBLIC “-//W3C//DTD MathML 2.0//EN” “http://www.w3.org/Math/DTD/mathml2/mathml2.dtd”>
SVG 1.0 svg10 <!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.0//EN” “http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd”>
SVG 1.1 Full svg11 <!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.1//EN” “http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd”>
SVG 1.1 Basic svg11-basic <!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.1 Basic//EN” “http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd”>
SVG 1.1 Tiny svg11-tiny <!DOCTYPE svg PUBLIC “-//W3C//DTD SVG 1.1 Tiny//EN” “http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd”>
XHTML+MathML+SVG (XHTML host) xhtml-math-svg-xh <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN” “http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd”>
XHTML+MathML+SVG (SVG host) xhtml-math-svg-sh <!DOCTYPE svg:svg PUBLIC “-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN” “http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd”>
XHTML+RDFa 1.0 xhtml-rdfa-1 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML+RDFa 1.0//EN” “http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd”>
XHTML+RDFa 1.1 xhtml-rdfa-2 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML+RDFa 1.1//EN” “http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd”>