首页 > IT.IT > Cakephp学习笔记之用htmlhelper在head区域增加标记

Cakephp学习笔记之用htmlhelper在head区域增加标记

2009年5月13日  如果本文对您有帮助,请留下您宝贵的足印!==>

一、写在前面
上篇文章说了怎么弄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

html4-strict
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
html4-trans
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
html4-frame
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
xhtml-strict
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
xhtml-trans
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
xhtml-frame
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
xhtml11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

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

输出:

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

这个的%s就是参数哦。

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

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

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

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

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

这样输出的就是

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

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

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

3.1、输出favicon.ico图标

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

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

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

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

相当于下边的这个东东。

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

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

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

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

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

用它的写法是:

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

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

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

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

3.2、输出keywords和Description

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

3.3、输出自定义属性

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

3.4、输出rss信息

echo $html->meta(
    'Rss的标题',
    '/comments/index.rss',
    array('type' => 'rss')
);
<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~~

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

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

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

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

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

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

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

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

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

hoho~ 赞一个先~

5、其他函数

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

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

echo $html->style(
  array( 'background' => '#633',
  'border-bottom' => '1px solid #000',
  'padding' => '10px'
 )
); 
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里面设置

$this->layout="tests";

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

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

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

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

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

echo $scripts_for_layout; 

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

特殊说明一下

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

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

border:1px;color:red;

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

border:1px;
color:red; 

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

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

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

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

echo $content_for_layout;

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


本文来自苏南的博客, 转载请注明网址:http://newsn.net, 谢谢!
我的淘宝小店:http://68zz.com
我的Sina圈子:http://q.blog.sina.com.cn/pctalk


作者: 苏南 分类: IT.IT 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.