CodeIgniter 中文手册 版本 2.1.0

查看原文

HTML辅助函数

HTML 辅助函数涵盖了一些用于 HTML 操作的函数。

装载辅助函数

本辅助函数的装载通过如下代码完成:

$this->load->helper('html');

可用的函数如下:

br()

生成指定个数的换行标签 (<br />) 。例如:

echo br(3);

如上代码将显示: <br /><br /><br />

heading()

帮助你创建 HTML <h1> 标签. 第一个字段用于标记显示内容,第二个字段用于标该标签所用字号。例如:

echo heading('Welcome!', 3);

如上代码将显示: <h3>Welcome!</h3>

Additionally, in order to add attributes to the heading tag such as HTML classes, ids or inline styles, a third parameter is available.

echo heading('Welcome!', 3, 'class="pink"')

The above code produces: <h3 class="pink">Welcome!<h3>

img()

帮助你创建 HTML <img /> 标签。 第1个参数包含的是图片文件的路径。 例如:

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

第2个参数是可选的, 其值为TRUE或者FALSE, 作用是决定该函数生成的图片地址中是否包含由$config['index_page']所设置的起始页面。 一般来说, 当你使用媒体控制器时才使用这个。

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

此外, 关联数组也能够被作为参数传递到 img() 函数中, 用来实现对所有属性和值的完全控制。如果没指定 alt 属性,则 CodeIgniter 将产生一个空字符串。

$image_properties = array(
          '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',
);

echo img($image_properties);
// <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" />

link_tag()

帮助你创建 HTML <link /> 标签。在链接样式表以及其他内容时非常有用。参数包括 href 以及可选的 rel, type, title, media 以及 index_page。index_page 是一个TRUE/FALSE 值,作用是决定该函数生成的 href 地址中是否包含由 $config['index_page']所设置的起始页面。 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" />

此外,关联数组也能够被作为参数传递到 link_tag() 函数中, 用来实现对所有属性和值的完全控制。

$link = array(
          '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" />

nbs()

生成不换行的指定个数的空格标签(&nbsp;)。例如:

echo nbs(3);

如上代码将显示: &nbsp;&nbsp;&nbsp;

ol()  和  ul()

允许你通过简单或多维的数组生成有序或无序的HTML列表。例如:

$this->load->helper('html');

$list = array(
            'red',
            'blue',
            'green',
            'yellow'
            );

$attributes = array(
                    'class' => 'boldlist',
                    'id'    => 'mylist'
                    );

echo ul($list, $attributes);

如上代码将显示:

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

这是一个更复杂的例子,应用了多维数组:

$this->load->helper('html');

$attributes = array(
                    'class' => 'boldlist',
                    'id'    => 'mylist'
                    );

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


echo ul($list, $attributes);

如上代码将显示:

<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>

meta()

帮助你创建meta标签. 你可以将字符串、简单数组或者多维数组传递给函数. 例如:

echo meta('description', 'My Great site');
// 生成: <meta name="description" content="My Great Site" />


echo meta('Content-type', 'text/html; charset=utf-8', 'equiv'); // 注意第3个参数.,可以是 "equiv" 或者 "name"
// 生成: <meta http-equiv="Content-type" content="text/html; charset=utf-8" />


echo meta(array('name' => 'robots', 'content' => 'no-cache'));
// 生成: <meta name="robots" content="no-cache" />


$meta = array(
        array('name' => 'robots', 'content' => 'no-cache'),
        array('name' => 'description', 'content' => 'My Great Site'),
        array('name' => 'keywords', 'content' => 'love, passion, intrigue, deception'),
        array('name' => 'robots', 'content' => 'no-cache'),
        array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv')
    );

echo meta($meta);
// 生成:
// <meta name="robots" content="no-cache" />
// <meta name="description" content="My Great Site" />
// <meta name="keywords" content="love, passion, intrigue, deception" />
// <meta name="robots" content="no-cache" />
// <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

doctype()

帮助你创建文档类型声明以及DTD。默认值是 XHTML 1.0 Strict ,但你也可以指定其他很多文档类型。

echo doctype();
// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

下面是支持的文档类型列表。它们都是可灵活配置的,可以在 application/config/doctypes.php 中配置。

文档类型 选项 结果
XHTML 1.1 doctype('xhtml11') <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
XHTML 1.0 Strict doctype('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 doctype('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 doctype('xhtml1-frame') <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
HTML 5 doctype('html5') <!DOCTYPE html>
HTML 4 Strict doctype('html4-strict') <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
HTML 4 Transitional doctype('html4-trans') <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
HTML 4 Frameset doctype('html4-frame') <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

 

最后修改: 2012-02-05 23:58:58