codeiginter集成smarty后在模板中url怎么生成,求助
codeiginter集成smarty后在模板中url怎么生成,求助。smarty中加入的site_url函数,是引用ci的url辅助函数
function smarty_function_site_url($params,&$smarty)
{
//check if the needed function exists
//otherwise try to load it
if (!function_exists('site_url')) {
//return error message in case we can't get CI instance
if (!function_exists('get_instance')) return "Can't get CI instance";
$CI= &get_instance();
$CI->load->helper('url');
}
if (!isset($params['url'])) return base_url();
else return site_url($params['url']);
}
现在碰到一个问题,在smarty模板中需要生成一个url 例如:
{*foreach from=$typeList item=type*}
<tr align="center" bgcolor="#FFFFFF">
<td>{*$type.type_name*}</td>
<td>{*$type.type_sort*}</td>
<td>
<a href="{*site_url url=admin/articleType/edit *}">编辑</a>
<a href="{*site_url url=admin/articleType/delete*}" onclick="return window.confirm('是否真的要删除')">删除</a>
</td>
</tr>
{*foreachelse*}
啥也没有
{*/foreach*}
我需要在编辑和删除两个链接中加入type_id 这个参数。类似于这种样子:
{*site_url url=admin/articleType/edit/1 *}
我写成:{*site_url url=admin/articleType/edit/$type.id_type *}这样子不行。
请问大家有没有好的解决方案,小弟这里跪求了。 一种无奈的选择:
{*site_url*}admin/articleType/edit/{*$type.id_type*} {*site_url url=admin/articleType/edit *}/{*$type.id_type*}
这样不行吗?
PS: smarty 我不是很了解,呵呵 源链接:http://codeigniter.com/wiki/Use_URL_helper_from_Smarty/
下面的代码是我自己写的,我测试了你需要的功能,这个可以实现,如果有问题就发邮件(huboo82 gmail com)告诉我吧。
参数url可以是字符串,也可以是数组(array('news', 'local', '123'))。
<?php
/**
* @filesource http://codeigniter.com/wiki/Use_URL_helper_from_Smarty/
* @author Modified by huboo <huboo82@gmail.com>
* @link http://huboo.eblhost.cn/
* @usage {site_url url="controller/function/"(Optional: dyn=$parameter)}
* @example
* //result: http://localhost/user/profile/1
* {site_url url="user/profile/" dyn=$slide.uid}
*
* //$uri_seg = array(date('Y/n/j', $create_date), $url_topic)
* //result: http://localhost/2010/2/26/passing-afternoon
* {site_url url=$uri_seg}
* or
* {site_url url=$createdate|date_format:"%Y/%m/%d/" dyn=$url_topic}
*/
function smarty_function_site_url($params, &$smarty)
{
//check if the needed function exists
//otherwise try to load it
if(!function_exists('site_url'))
{
//return error message in case we can't get CI instance
if (!function_exists('get_instance')) return "Can't get CI instance";
$CI= &get_instance();
$CI->load->helper('url');
}
$url = (isset($params['url']) ? $params['url'] : False);
$var = (isset($params['dyn']) ? $params['dyn'] : '');
if($url)
return (is_array($url)) ? site_url(array_merge($url, array($var))) : site_url($url . $var);
else
return base_url();
}
?>
页:
[1]