频道栏目
首页 > 资讯 > cakephp > 正文

CakePhp学习笔记之htmlHelper,link

16-01-08        来源:[db:作者]  
收藏   我要投稿

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

view source
 
print?
01.html4-strict
02.
03.html4-trans
04.
05.html4-frame
06.
07.xhtml-strict
08.
09.xhtml-trans
10.
11.xhtml-frame
12.
13.xhtml11
14.

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

输出:

view source
 
print?
1.

这个的%s就是参数哦。

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

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

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

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

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

这样输出的就是

view source
 
print?
1.

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

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

3.1、输出favicon.ico图标

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

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

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

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

相当于下边的这个东东。

view source
 
print?
1.echo$html->meta('icon','/img/favicon.ico' );
view source
 
print?
1.

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

view source
 
print?
1.echo$html->meta('icon');
view source
 
print?
1.

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

view source
 
print?
1.

用它的写法是:

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

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

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

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

3.2、输出keywords和Description

view source
 
print?
1.echo$html->meta('keywords','enter any meta keyword here' );echo $html->meta('description','enter any meta description here' );
view source
 
print?
1.
2.

3.3、输出自定义属性

view source
 
print?
1.echo$html->meta(null,null,array('name' =>'author','content' =>'newsn.net@gmail.com' ) );
view source
 
print?
1.

3.4、输出rss信息

view source
 
print?
1.

echo $html->meta(

 
2.'Rss的标题',
3.'/comments/index.rss',
4.array('type' =>'rss')
5.);
 
view source
 
print?
1.

cakephp_rss

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

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

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

view source
 
print?
1.echo$html->css('css');
view source
 
print?
1.

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

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

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

view source
 
print?
1.echo$html->css('../images/css');
view source
 
print?
1.

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

view source
 
print?
1.echo$html->css(array('css1','css2','css3'));
view source
 
print?
1.
2.
3.

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

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

hoho~ 赞一个先~

5、其他函数

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

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

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

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

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里面设置

view source
 
print?
1.$this->layout="tests";

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

view source
 
print?
1.$html->meta(....,false);
2.$html->css(....,false);

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

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

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

view source
 
print?
1.echo$scripts_for_layout;

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

特殊说明一下

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

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

view source
 
print?
1.border:1px;color:red;

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

view source
 
print?
1.border:1px;
2.color:red;

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

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

view source
 
print?
1.echo$this->style(...,false);
2.echo$this->style(...,true);

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

view source
 
print?
1.echo$content_for_layout;

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

相关TAG标签
上一篇:Cakephp中使用JavaScriptHelper来引入js文件
下一篇:ZendFramework中领域逻辑的处理
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站