用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4522|回复: 3
收起左侧

[HELP] codeiginter集成smarty后在模板中url怎么生成,求助

[复制链接]
发表于 2010-1-30 18:50:15 | 显示全部楼层 |阅读模式
codeiginter集成smarty后在模板中url怎么生成,求助。

smarty中加入的site_url函数,是引用ci的url辅助函数
PHP复制代码
 
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 例如:
HTML复制代码
 
{*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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <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 *}这样子不行。

请问大家有没有好的解决方案,小弟这里跪求了。
 楼主| 发表于 2010-1-30 22:18:37 | 显示全部楼层
一种无奈的选择:
{*site_url*}admin/articleType/edit/{*$type.id_type*}
发表于 2010-1-31 12:13:48 | 显示全部楼层
{*site_url url=admin/articleType/edit *}/{*$type.id_type*}
这样不行吗?

PS: smarty 我不是很了解,呵呵
发表于 2010-3-5 18:27:50 | 显示全部楼层
源链接:http://codeigniter.com/wiki/Use_URL_helper_from_Smarty/
下面的代码是我自己写的,我测试了你需要的功能,这个可以实现,如果有问题就发邮件(huboo82 [at] gmail [dot] com)告诉我吧。
参数url可以是字符串,也可以是数组(array('news', 'local', '123'))。
PHP复制代码
 
<?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();
}
?>
 
复制代码

本版积分规则