HTML 辅助函数文件包含了用于处理 HTML 的一些函数。
加载辅助函数
该辅助函数通过下面的代码加载:
$this->load->helper("html");
可用函数
该辅助函数有下列可用函数:
heading([$data = ""[, $h = "1"[, $attributes = ""]]])
参数:
- $data (string) -- Content
- $h (string) -- Heading level
- $attributes (mixed) -- HTML attributes
返回: HTML heading tag
返回类型: string
用于创建 HTML 标题标签,第一个参数为标题内容,第二个参数为标题大小。例如:
echo heading("Welcome!", 3);
上面代码将生成:Welcome!
另外,为了向标题标签添加属性,例如 HTML class、id 或内联样式,可以通过第三个参数传一个字符串或者一个数组:
echo heading("Welcome!", 3, "class="pink"");
echo heading("How are you?", 4, array("id" => "question", "class" => "green"));
上面代码将生成:
<h3 class="pink">Welcome!<h3>
<h4 id="question" class="green">How are you?</h4>
img([$src = ""[, $index_page = FALSE[, $attributes = ""]]])
参数:
- $src (string) -- Image source data
- $index_page (bool) -- Whether to treat $src as a routed URI string
- $attributes (array) -- HTML attributes
返回: HTML image tag
返回类型: string
用于生成 HTML 标签,第一个参数为图片地址,例如:
echo img("images/picture.jpg"); // gives <img src="http://site.com/images/picture.jpg" />
第二个参数可选,其值为 TRUE 或 FALSE,用于指定是否在生成的图片地址中添加由 $config["index_page"] 所设置的起始页面。 一般来说,当你使用媒体控制器时才使用这个:
echo img("images/picture.jpg", TRUE); // gives <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"
);
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([$href = ""[, $rel = "stylesheet"[, $type = "text/css"[, $title = ""[, $media = ""[, $index_page = FALSE]]]]]])
参数:
- $href (string) -- What are we linking to
- $rel (string) -- Relation type
- $type (string) -- Type of the related document
- $title (string) -- Link title
- $media (string) -- Media type
- $index_page (bool) -- Whether to treat $src as a routed URI string
返回: HTML link tag
返回类型: string
用于生成 HTML 标签,这在生成样式的 link 标签时很有用,当然也可以生成其他的 link 标签。 参数为 href ,后面的是可选的:rel 、type 、 title 、 media 和 index_page 。
index_page 参数是个布尔值,用于指定是否在 href 链接中添加由 $config["index_page"] 所设置的起始页面。
例如:
echo link_tag("css/mystyles.css");
// gives <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() 函数传递一个关联数组来完全控制所有的属性和值:
$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" />
ul($list[, $attributes = ""])
参数:
- $list (array) -- List entries
- $attributes (array) -- HTML attributes
返回: HTML-formatted unordered list
返回类型: string
用于生成 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>
下面是个更复杂的例子,使用了多维数组:
$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>
ol($list, $attributes = "")
参数:
- $list (array) -- List entries
- $attributes (array) -- HTML attributes
返回: HTML-formatted ordered list
返回类型: string
和 ul() 函数一样,只是它生成的是有序列表( )。
meta([$name = ""[, $content = ""[, $type = "name"[, $newline = "n"]]]])
参数:
- $name (string) -- Meta name
- $content (string) -- Meta content
- $type (string) -- Meta type
- $newline (string) -- Newline character
返回: HTML meta tag
返回类型: string
用于生成 meta 标签,你可以传递一个字符串参数,或者一个数组,或者一个多维数组。
例如:
echo meta("description", "My Great site");
// Generates: <meta name="description" content="My Great Site" />
echo meta("Content-type", "text/html; charset=utf-8", "equiv");
// Note the third parameter. Can be "equiv" or "name"
// Generates: <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
echo meta(array("name" => "robots", "content" => "no-cache"));
// Generates: <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);
// Generates:
// <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([$type = "xhtml1-strict"])
参数:
- $type (string) -- Doctype name
返回: HTML DocType tag
返回类型: string
用于生成 DTD (文档类型声明,document type declaration),默认使用的是 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 | xhtml11 | |
XHTML 1.0 Strict | xhtml1-strict | |
XHTML 1.0 Transitional | xhtml1-trans | |
XHTML 1.0 Frameset | xhtml1-frame | |
XHTML Basic 1.1 | xhtml-basic11 | |
HTML 5 | html5 | |
HTML 4 Strict | html4-strict | |
HTML 4 Transitional | html4-trans | |
HTML 4 Frameset | html4-frame | |
MathML 1.01 | mathml1 | |
MathML 2.0 | mathml2 | |
SVG 1.0 | svg10 | |
SVG 1.1 Full | svg11 | |
SVG 1.1 Basic | svg11-basic | |
SVG 1.1 Tiny | svg11-tiny | |
XHTML+MathML+SVG (XHTML host) | xhtml-math-svg-xh | |
XHTML+MathML+SVG (SVG host) | xhtml-math-svg-sh | |
XHTML+RDFa 1.0 | xhtml-rdfa-1 | |
XHTML+RDFa 1.1 | xhtml-rdfa-2 |
br([$count = 1])
参数:
- $count (int) -- Number of times to repeat the tag
返回: HTML line break tag
返回类型: string
根据指定的个数生成多个换行标签( )。 例如:
echo br(3);
上面的代码将生成:
<br /><br /><br />
注解
该函数已经废弃,请使用原生的 str_repeat() 函数代替。
nbs([$num = 1])
参数:
- $num (int) -- Number of space entities to produce
返回: A sequence of non-breaking space HTML entities
返回类型: string
根据指定的个数生成多个不换行空格( )。 例如:
echo nbs(3);
上面的代码将生成:
注解
该函数已经废弃,请使用原生的 str_repeat() 函数代替。