易栈网-膘叔

发文热力图 2025-12-07 ~ 2026-12-06
少 多 近 365 天共 27 篇
浏览模式: 标准 | 列表 | 图文

ThinkPHP怎么样更好的使用Smarty第三方插件

如果你在使用ThinkPHP框架的时候不想采用TP自带的模版系统,而使用第三方的模版系统,你有很多其他的选择,在这里我仅介绍Smarty这种比较官方,而且比较强大的模版系统。

由于Smarty兼容PHP4,因此,它的效率会相对低一点点,这个低只是相对的,估计等Smarty啥时候正式放弃PHP4的时候,效率可能会上很大一个台阶。


官方标准使用方法:

在TP框架的PlugIns目录下面,有一个SmartTemplate目录,里面就是系统自带的Smarty插件。

使用方法如下:

1、在项目的Conf/Config.php文件里加上:

return array(
'THINK_PLUGIN_ON' => true,
'TMPL_ENGINE_TYPE'=>'smarty',
);

2、下载好Smarty,并将smarty的libs目录整个拷至项目的PlugIns目录下(说明一下,PlugIns目录可能会不存在,需要自己建立),同时将libs目录改名为SmartTemplate(希望没有记错,其实也就是与THINKPHP的PlugIns目录里的SmartyTemplate目录同名即可),如果你不愿意把目录改成这个名字,那么,你必须到TP的插件目录里修改插件文件,使之包含路径正确。

3、注意修改每次修改action或模板文件后删除Temp下的html文件

说明一下:上面的内容来自于官方,由lin_chaoqi朋友解答的,网址为:http://bbs.thinkphp.cn/viewthread.php?tid=305&highlight=smarty


在这里我要提的方法是于上面不一样的,黑黑

因为我在使用第三方模版插件的时候,特地看了TP的view.class.php发现了一些很重要的问题,那就是,如果采用第三方模版插件,那么第三方模版插件的效率可能不能保证,因为View类的fetch方法在判断是否为第三方插件之间,作了很多TP模版插件的自有处理,而这些对于使用第三方模版插件来说,几乎是完全无效的,这些处理可能会给第三方插件带来影响,同时也影响了第三方插件的执行效率。问题已经与流年沟通过,但由于改动可能会很大,或许最近几个版本里,流年都不会尝试作改进吧,一是怕影响了那些已经使用第三方插件的程序,二来如果去除掉这些处理,那么View类可能就不需要了。流年对于这样的情况应该是不愿意看到的。毕竟这也影响了原有系统的架构,估计流年得仔细考虑了……[当然从流年个人来说,肯定是希望大家都使用TP自有的模版插件,只是我目前对于smarty则是更加熟悉而己],但对于我这个使用者来说,我需要的是临时解决方法,所以,就有了下面的内容。

为了解决这个问题,我只有从View.class.php里下手,因为Action.class.php里有一行:

$this->tpl = View::getInstance();

那么,也就是说 tpl 这个变量是 View 的单例模式,检查了一下View.class.php中的这个getInstance方法,发现里面使用了 get_instance_of 这个函数(这个函数是有一点小BUG的,这里不作解释,但我目前也没有更好的解决方法),于是我对getInstance和__construct两个方法进行了改动,删除了__construct方法,加上了init方法,改动代码如下:

static function getInstance() {
return get_instance_of(__CLASS__,'init');
}

public function init ($type=''){
if(!empty($type)) {
$this->type = strToUpper( $type );
}else{
$this->type = strtoupper(C('TMPL_ENGINE_TYPE'));
}
if ( !in_array( $this->type, array('PHP','THINK') ) ){
$type = ucfirst( strToLower( $this->type ) );
vendor( $type );
return new $type();
}
return $this;
}

也就是让View类在实例化的时候,同时调用init方法。在这个方法里,我将我自己的模版插件放到了第三方插件的目录(Vendor)下。

切记切记:千万不可漏掉最后一句return $this;,其实这就是我所说的get_instance_of的BUG,如果不加这句,那么当type变量为PHP或THINK时,getInstance是无法返回实例的。

新的使用方法步骤如下:

1、修改项目的Conf/Config.php文件:

return array(
'THINK_PLUGIN_ON' => true,
'TMPL_ENGINE_TYPE'=>'TpSmarty',
);

2、在TP的Vendor目录下面,创建TpSmarty.php,内容如下:

<?php
include_once(PLUGIN_PATH."smarty/Smarty.class.php");

class TpSmarty extends Smarty {
public function __construct (){
parent::Smarty();
$this->caching = true;
$this->template_dir = TMPL_PATH;
$this->compile_dir = CACHE_PATH ;
$this->cache_dir = TEMP_PATH ;
}
}
?>

上面是最简单的写法,实际使用中,这些变量请改为与你自己的站点相配合。

3、根据上面的文件里的include_once函数,将smarty的libs目录拷贝至项目的PlugIns目录下,改名为:smarty(只需要与include_once中的目录相匹配即可)

4、然后,在项目的方法里就可以直接使用:

class IndexAction extends Action{
public function index(){
$this->assign('test','testss');
$this->display('default/index.html');
}
}

只是,使用了插件后,display方法的参数是模版的全路径,而且不能留空(并非不能解决,只是要改动的代码就会更多,目前这个方法是改动最少的)。

测试一下,是否正常了?呵呵 。

现在,我们把Config里的模版引擎换回Think,同时在Tpl/default/目录下建立Index目录,里面放上index.html,并修改上面的index()方法,将原来的$this->display('default/index.html'); 改为$this->display(); ,试一下,是不是也正常了?

测试一下:

<?php

if( !emptyempty( $test )){
echo 'test fck empty';
}
果然。不是我的错。。。哈哈是自动生成两个empty的。。。

肖佑阳第一天

今天是小阳阳的第一天哦,昨天没有带相机,实在是很惋惜。今天也可惜,因为我下午才去医院的,在我去之前,小阳阳已经陪着护士阿姨洗了个澡,还接受了人生第一次按摩哦。

照相机比较破,是300W的卡片机,几年前买的。不过不影响小阳阳在镜头前的展示。摄像太大,无法上传,啥时候,我直接用FTP上传吧。

照片拍的不好哦,点击小图看大图,大图全是1024*768的,估计以后的桌面是跑不掉了

57728bac069e79093844dcff93a3f5e8

(拍我干嘛呢?)

NND,第二次上传居然成了“该页无法显示”,看来我只能一张一张传了(顺便提醒一下,FF下面,点击小图,可能会有一个反斜框在路径里,会造成打不开网页,请删除该反斜杠再进行打开,对不住FF用户了)

064fbc676d3da40c32e94bd855db720a

(不要以为医生说我半个月后才能看见东西就以为我真的看不见)

315abaa67cc18f219fa62b732ed03186

(鄙视你,看不到我的中指吧。。)

6cf9149d0751a4698d01184e8d017fc1

(外婆喂奶,享受ING)

600811b7610560b274eeebb441afc3c4

(我比隔壁的小姑娘都漂亮)

890e3d15af4fd6cc42b58e43857a6690

为了方便朋友们订阅我的博客,特了加了点功能

为了方便朋友们订阅我的博客,特了加了点功能

看到左侧那些大大小小的图标了没有?你们可以直接通过http://feed.neatcn.com来订阅我的博客。只是增加了一点新功能,自已没有写一行代码。全部是别人提供的服务。但,确实是方便了订阅。

希望可以给大家带来方便,如果觉得左侧这几个图标影响页面打开速度,也请告诉我,我可以删掉它。

2008年6月20日上午11时48分,儿子出世了

2008年6月20日上午11时48分,儿子出世了

2008年6月20日凌晨1点58分,发现羊水破了,立马打车去医院,2点半左右,在医院挂上了急诊,进行待产房。

上午5点多,开始肚子疼,7点左右检查时,宫口开了两指,此时疼痛间隔大约在7分钟和10分钟之间,9点疼痛加剧,9点半检查,开了三指,大约在10点半时再检查,已经开了四指左右,此时接生人员建议进入产房。

2008年6月20日上午11时48分,儿子出世了(顺产)。身高51CM,目前母子平安。

下午3点多,儿子喝上了出世后第一口奶,不过这是医院配制的。

3点多时,被医院的护士长进行了简单培训,比如喂奶、给婴儿洗澡、换尿片啥的。

将近5点,妻子终于尿出来了,听说顺产后一定的时间内,要小便,否则可能会引起内出血。现在几乎排除这个问题了

乘着回家给老婆拿一些日常用品的机会,把经历贴出来。等拍了照片,我会继续上传

PS:忘了说明体重了,7斤7两,同时,小BABY还有一个小耳朵。依他外婆的意思,现在就用激光切掉,但被我以存在的就是合理的先搪塞过去。名字准备叫肖佑阳,母亲属羊,希望他长大后能够以男子汉的身体佑护他妈妈,毕竟怀胎10月以及生产都是一件很痛苦的事情

为自己添加协议

为自己添加协议

原文地址:http://kb.mozillazine.org/Register_protocol

A protocol is a method that is used to send, receive, and handle information over a connection. Common protocols viewed from the browser include http, https, ftp, and mailto. In order for you to view information sent over a specific protocol, it must be registered. If you enter a URL for an unknown protocol (foo) in the Location Bar, you will receive a message such as, Firefox doesn't know how to open this address, because the protocol (foo) isn't associated with any program or, in Mozilla Suite/SeaMonkey, foo not a registered protocol.

Once registered, the protocol can then be handled by the program you specify, such as your browser or a third party viewer. This means that a hyperlink ( e.g. foo://fred) can use the handler for protocol foo to open the file named fred.

Windows

On Windows, protocol registration is done by the operating system (OS). For example, when you set a web browser as the default browser, the OS will register the http, https and ftp protocols and associate them with that web browser; when you open an http https or ftp link in a mail message or from an Internet Shortcut, the URL will open in your default browser. Similarly, when you set a default mail client, the OS registers the mailto protocol so that mailto links in the browser open in the associated mail application.

Note: Mozilla Suite/SeaMonkey integrates mail and browser functions so that web links in an email message (e.g., http:// links) will open in a SeaMonkey browser window and "mailto:" links in a web page will open in SeaMonkey Mail, regardless of the OS protocol handlers.

Other protocols can be registered and associated with an external application by configuring an installed program to handle the content. For example, you can set RealPlayer as the registered protocol handler for RTSP URLs (Real-Time Streaming Protocol) by using RealPlayer's preference settings under "Content -> Media Types". When you next open an rtsp:// link, Firefox or Mozilla Suite/SeaMonkey will ask you to confirm the external protocol request before it will launch the application [1] [2].

It is also possible to register a protocol by creating a .reg file such as the following, and placing this in the file:

REGEDIT4

[HKEY_CLASSES_ROOT\foo]
@="URL:foo Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\foo\shell]

[HKEY_CLASSES_ROOT\foo\shell\open]

[HKEY_CLASSES_ROOT\foo\shell\open\command]
@="\"C:\\Program Files\\Application\\program.exe\" \"%1\""

Replace foo with the protocol you're specifying and C:\\Program Files\\Application\\program.exe with the program you want to run. [3] Note that backslashes must be doubled and the additional slashes and quotes before the path must not be removed. Save the file, then double-click it to insert into the Windows registry. See Registering an Application to a URL Protocol for additional information.

Linux and Mac

  • Type about:config into the address bar and press Enter.
  • Right-click -> New -> Boolean -> Name: network.protocol-handler.external.foo -> Value -> true (Replacing foo with the protocol you're specifying)
  • Right-click -> New -> String -> Name: network.protocol-handler.app.foo -> Value -> /path/to/app (Replacing foo with the protocol you're specifying and /path/to/app with the path to the application you want to run.
  • Ensure network.protocol-handler.expose-all is set to true.

Troubleshooting

  • (Linux and Mac) If the path or name is incorrect when setting the network.protocol-handler.app.(protocol) preference , Firefox will display an error saying "protocol (foo) isn't associated with any program". (bug 312953)
  • If you receive a message in Firefox that the mailto protocol is not associated with any program, reset your default mail client. [4]
  • Certain unsafe protocols are ignored by default (bug 173010).

See also

External links

The protocol is not associated with any program (Firefox Support)

为JQuery的cycle插件作图片自动缩放

为JQuery的cycle插件作图片自动缩放

function showImg(){
pw = parseInt( $(this).parent().css('width') );
ph = parseInt( $(this).parent().css('height') );
showImage = new Image();
showImage.src = $(this).children('a').children('img').attr('src');
sw = showImage.width;
sh = showImage.height;
//原先这些sw是直接用 $(this).width()来判断,但实际上很多时间都取回的是pager里面的图片大小属性,或者取回来有不正常的时候,这是cycle的BUG,没有时间却验证了,因此,只有用Image类来进行处理,这样取回来的就是图片真正的大小
idx = $('#bigPic li').index( $(this) );
alert(showImage.src+' - '+sw+' - '+sh);
if (sw > pw || sh > ph){
if (sw > sh ){
sh = Math.floor(sh * ( pw / sw ));
sw = pw-10;
}else {
sw = Math.floor(sw * ( ph / sh ));
sh = ph-10;
}
//实在不知道这里为什么会多出16px,如果不减6,就会每次缩小
//这里的长宽比例会不正确。。。
$(this).children('a').children('img').css({'width': sw,'height':sh,'paddingTop':3});
}
if (sh <= ph){
$(this).css('marginTop',(ph - sh)/2).css('verticalAlign','middle');
}
if (sw <= pw){
$(this).css('marginLeft' , (pw - sw)/2).css('textAlign','center');
}
if( idx == 0 ){
$('#thumbnail a').trigger('click');
}
}

Mark it ,and update it on work time...

长时间不更新,送上点JS

长时间没有更新了,一是因为单位比较忙二来是老婆快生了。。

但一直不更新也不太好,这里送上一小段JS,FOR jQuery。

很多时候,我们需要在一堆图片列表里进行左移右移,比如我现在有10个IMG,我放到DIV里面默认显示5个,这时候我会在页面上留下两个按钮进行左移,右移,以前的移动可能会很复杂,自从有了jquery,一切变得这么简单。

<div id="pic_list">
<span id="p_left"> << </span>
<ul>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
<li><img src="aa.jpg" /><li>
</ul>
<span id="p_right"> >> </span>
</div>
<script language="JavaScript">
<!--
$('#p_left').click(function(){
var list_length = $('#pic_list ul li').length;
$('#pic_list ul li:eq(0)').before( $('#pic_list ul li:eq('+(list_length-1)+')').remove() );
});
$('#p_right').click(function(){
$('#pic_list ul').append( $('#pic_list ul li:eq(0)').remove() );
});
//-->
</script>

具体样式我就不提供了,黑黑,这样的方法应该算是最简单的了。。毕竟你在做页面的时候,肯定会都把li的宽度设的一样

Smarty的一些常用方法

Smarty的一些常用方法

网上流传的大师兄教程确实给了我们很多方便,只是大师兄教程却不是很全面。或许他给很多设计师们带来了福音,但对程序开发人员来说,并没有什么特别的高深之处。

不过,我还是挺佩服他的,毕竟,他将自己的思想和实现方法提供出来,如果每个人都这样,或许,我们能够再进步一些。呵呵

在工作,仍然是在使用着smarty,也在开发中积累了一点点的心得,逐步写出来,与大家共同分享,当然应该会存有错误,与大家一起改进。

smarty在使用时是需要配置的,最简单的配置方法就是将基本的变量写成一个数组,然后new Smarty之后,foreach一下,由$smarty自行加载,如:

---------------------------------------------

辛苦写了半天,结果超时了。下次补上,今天没时间了。

onDblClick?onDoubleClick?ondblclick?

onDblClick?onDoubleClick?ondblclick?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<script language="JavaScript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.pack.js"> </script>
</head>

<body>
<table>
<tr>
<td><span class="forupdate" name="price" id="price">1234.56</span></td>
</tr>
<tr>
<td><span class="forupdate" name="number" id="number">55</span></td>
</tr>
</table>

<script language="JavaScript">
<!--
$(document).ready(function(){
$('.forupdate').dblclick(function(){
var Input = "<input type='text' >";
var InputValue = $(this).text();
$(this).html( Input );
$(this).children('input').attr('name', $(this).attr('name')+'_update')
.attr('id', $(this).attr('id')+'_update')
.val( InputValue );
$('#'+$(this).attr('id')+'_update').focus()
.blur( function(){
//这里用ajax如果成功,再返回。否则。。
//....现在不处理。
$ajaxReturn = true;
if( $ajaxReturn == true){
$(this).parent().html( $(this).val() ) ;
}
});
});
});
//-->
</script>
</body>
</html>

可以拷贝到编辑器里,双击数字进行测试……

后记:虽然知道这种控件网上会很多,但是没料到会这么多……

贴一个地址吧,也是jQuery的插件:http://www.appelsiini.net/projects/jeditable,NND啥时候一生气,也搞成插件出来……