Skip to content

Use URL helper from Smarty

Derek Jones edited this page Jul 5, 2012 · 6 revisions

Category:Help::TipsAndTricks

Some time ago I wrote some very small plugins for Smarty, which allow me to use CI’s URL helper functions (base_url() and site_url()) directly from my Smarty templates.

So I decided to share them: function.site_url.php

<?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']);
}
?> 

function.base_url.php

<?php
function smarty_function_base_url($params,&$smarty) {
    if (!function_exists('base_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');
    }
    
    return base_url();
    
}
?> 

Installation: just drop these two files in smarty’s plugin folder.

Usage: In your template file use {base_url} to output the root url of your site (e.g. http://fest.servehttp.com/) {site_url url="controller/function"} produces something like this http://fest.servehttp.com/index.php/controller/function

Not the most advanced plugins ever, but some might find them useful.

Clone this wiki locally