-
Notifications
You must be signed in to change notification settings - Fork 0
Use URL helper from Smarty
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: [code] 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']); } ?> [/code] function.base_url.php [code] <?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();
} ?> [/code]
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.