CakePhp学习笔记之htmlHelper,link

字体大小: 中小 标准 ->行高大小: 标准

 

一、写在前面
上篇文章说了怎么弄Cakephp下的连接a东东。
今天学习的内容是生成head区域的标记内容。
head区域的内容其实是很丰富的,
但是多数是幕后功臣, 在页面上貌似只有title和style是能看到作用的。
hoho,不说这个,下面说说我在Cakephp里面用htmlhelper输出这些tags的学习过程。

二、head区域代码输出

当然继续沿用 http://newsn.net/2009/05/415  中的那个特殊构造的controller 来试验我们今天的内容,当然,这样,是不符合标准的,但是对于初学者是实用的。
官方的相关链接地址如下:
http://book.cakephp.org/view/206/Inserting-Well-Formatted-elements

1、$html->docType(string $type = ‘xhtml-strict’)
关于type的取值,官方的说明不是很确切。 可以通过查看核心源代码获得最标准的答案。
位置是:cake/libs/view/helpers/html.php

01.html4-strict
02.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
03.html4-trans
04.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
05.html4-frame
06.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
07.xhtml-strict
08.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
09.xhtml-trans
10.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
11.xhtml-frame
12.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
13.xhtml11
14.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

2、$html->charset(string $charset=null)

输出:

1.<meta http-equiv="Content-Type" content="text/html; charset=%s" />

这个的%s就是参数哦。

默认为空的时候,会取到App.encoding的值。

如果还是空的话,就默认是utf-8。

如果不是空的话,就用输入的那个东东代替了。hoho~

居然没有对输入的charset值进行合法性检测。崩溃。

1.Configure::write('App.encoding','gb2312'); echo $html->charset();

这样输出的就是

1.<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

3、$html->meta($type, $url = null, $attributes = array(), $inline = true)

这个是个比较复杂的方法。

3.1、输出favicon.ico图标

1.echo $html->meta( 'favicon.ico', '/img/favicon.ico',array('type' => 'icon') );

这个是官方给的范例中的写法,其实很有误导作用。

他的第三个参数中type会覆盖第一个参数,

也就是说他的第一个参数是啥都行。

相当于下边的这个东东。

1.echo $html->meta( 'icon', '/img/favicon.ico' );
1.<link href="/img/favicon.ico" type="image/x-icon" rel="icon" /> <link href="/img/favicon.ico" type="image/x-icon" rel="shortcut icon" />

下面是不自定义位置的办法,使用默认值。

1.echo $html->meta('icon');
1.<link href="favicon.ico" type="image/x-icon" rel="icon" /> <link href="favicon.ico" type="image/x-icon" rel="shortcut icon" />

不过据说还有个bookmark的favicon.ico,不知道为啥没有一并输出。

1.<link href="images/fav.ico" type="image/x-icon" rel="Bookmark" />

用它的写法是:

1.echo $html->meta( 'icon', 'favicon.ico', array('rel'=>'Bookmark') );

不过这个输出的路径地址是带网址的绝对路径。

(要想改成相对地址,貌似除非改核心代码)。

很是不爽,所以个人认为Cakephp里面对ico的处理还需要进一步改进。

3.2、输出keywords和Description

1.echo $html->meta( 'keywords', 'enter any meta keyword here' );echo $html->meta( 'description', 'enter any meta description here' );
1.<meta name="keywords" content="enter any meta keyword here"/>
2.<meta name="description" content="enter any meta description here"/>

3.3、输出自定义属性

1.echo $html->meta(null,null, array( 'name' => 'author', 'content' =>'newsn.net@gmail.com' ) );
1.<meta name="author" content="newsn.net@gmail.com"/>

3.4、输出rss信息

echo $html->meta(

2.    'Rss的标题',
3.    '/comments/index.rss',
4.    array('type' => 'rss')
5.);
1.<link href="comments/index.rss" type="application/rss+xml" rel="alternate" title="Rss的标题" />

cakephp_rss 

4、$html->css(mixed $path, string $rel = null, array $htmlAttributes = array(), boolean $inline = true)

我觉得这个css语句貌似还算是对的起观众,

因为我一直记不住那段css的html语句。 hoho~~

1.echo $html->css('css');
1.<link rel="stylesheet" type="text/css" href="css/css.css" />

这个东东的变态的地方就是非要让css文件放到根目录下的css目录中,

而我的习惯是放到images目录下面去。

估计Cakephp的作者看到我下边这句条语句肯定会气晕。

1.echo $html->css('../images/css');
1.<link rel="stylesheet" type="text/css" href="css/../images/css.css" />

这样的话,还是可以放到images目录下面去。hoho~

1.echo $html->css(array('css1','css2','css3'));
1.<link rel="stylesheet" type="text/css" href="css/css1.css" />
2.<link rel="stylesheet" type="text/css" href="css/css2.css" />
3.<link rel="stylesheet" type="text/css" href="css/css3.css" />

输出多个css,这个css的方法我觉得是目前看到的最棒的函数。

不过对应的其他参数没有做实验。

hoho~ 赞一个先~

5、其他函数

在head区域里面能用到的也许还有:

$html->style(array $data, boolean $inline = true)

1.echo $html->style(
2.  array( 'background' => '#633',
3.  'border-bottom' => '1px solid #000',
4.  'padding' => '10px'
5. )
6.);
1.background:#633; border-bottom:1px solid #000; padding:10px;

看到了没有,连个都<style></style>没得输出,真是个超级垃圾函数。

6、关于上边涉及的函数里面的$inline参数

官方的解释是:

If $inline is set to false, the link tags are added to the $scripts_for_layout variable which you can print inside the head tag of the document.

个人理解是如果这个inline被设置为false后,就崩echo了,echo出不来了。

先在tests_controller里面的index里面设置

1.$this->layout="tests";

在/views/tests/index.ctp里面使用helper

1.$html->meta(....,false);
2.$html->css(....,false);

注意上边没有echo哦,echo也是输出为空。

然后在/views/layout/tests.ctp里面使用,

这个tests.ctp是在controller的那个index方法里面指定的哦。

1.echo $scripts_for_layout;

获得最后的数据,真是好麻烦哦~hoho。

特殊说明一下

上边的$html->style里面的$inline居然和其他的$inline不是一个意思。

这个地方的$inline=true的时候 输出的style内容是在一行内的,看代码的时候没有换行。

1.border:1px;color:red;

而为false的时候看代码是换行的,

1.border:1px;
2.color:red;

就是说这个inline的意思是“在一行”的意思。 而且必须都要echo。hoho~

并且它在views/tests/index.ctp里面调用的时候,

1.echo $this->style(...,false);
2.echo $this->style(...,true);

是在views/layouts/tests.ctp里面的

1.echo $content_for_layout;

这里显示出来的哦。hoho~ 真是一个风格迥异的函数。hoho~

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/791.html