目錄 搜尋
欢迎 目录 快速参考图 基本信息 服务器要求 许可协议 变更记录 关于CodeIgniter 安装 下载 CodeIgniter 安装指导 从老版本升级 疑难解答 介绍 开始 CodeIgniter 是什么? CodeIgniter 速记表 支持特性 应用程序流程图 模型-视图-控制器 架构目标 教程 内容提要 加载静态内容 创建新闻条目 读取新闻条目 结束语 常规主题 CodeIgniter URL 控制器 保留字 视图 模型 辅助函数 使用 CodeIgniter 类库 创建你自己的类库 使用 CodeIgniter 适配器 创建适配器 创建核心系统类 钩子 - 扩展框架的核心 自动装载资源 公共函数 URI 路由 错误处理 缓存 调试应用程序 以CLI方式运行 管理应用程序 处理多环境 PHP替代语法 安全 开发规范 类库参考 基准测试类 日历类 购物车类 配置类 Email 类 加密类 文件上传类 表单验证详解 FTP 类 图像处理类 输入类 Javascript 类 语言类 装载类 迁移类 输出类 分页类 模板解析器类 安全类 Session 类 HTML 表格类 引用通告类 排版类 单元测试类 URI 类 User-Agent 类 表单验证 XML-RPC 和 XML-RPC 服务器 Zip 编码类 缓存适配器 适配器参考 适配器 数据库类 Active Record 类 数据库缓存类 自定义函数调用 数据库配置 连接你的数据库 数据库快速入门例子代码 字段数据 数据库维护类 查询辅助函数 数据库类 查询 生成查询记录集 表数据 事务 数据库工具类 JavaScript类 辅助函数参考 数组辅助函数 CAPTCHA 辅助函数 Cookie Helper 日期辅助函数 目录辅助函数 下载辅助函数 Email 辅助函数 文件辅助函数 表单辅助函数 HTML辅助函数 Inflector 辅助函数 语言辅助函数 数字辅助函数 路径辅助函数 安全辅助函数 表情辅助函数 字符串辅助函数 文本辅助函数 排版辅助函数 URL 辅助函数 XML 辅助函数
文字

CodeIgniter 用户指南 版本 2.1.0

编辑文档、查看近期更改请 登录 或 注册  找回密码
查看原文

HTML辅助函数

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

  • br()
  • heading()
  • img()
  • link_tag()
  • nbs()
  • ol() 和 ul()
  • meta()
  • doctype()

装载辅助函数

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

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

 

翻译贡献者: Hex, szlinz, yinzhili
最后修改: 2012-02-05 23:58:58
上一篇: 下一篇: