-
Notifications
You must be signed in to change notification settings - Fork 0
Smarty plugin URL Helper
I'm using Smarty as a template library for CodeIgniter, and when I wanted to use CodeIgniter features I searched plugins, but I couldn't find a plugin which was usable for me, so I decided to write my own.
Now I finished it (took me 1 hour, horrible : D) and I'm here to post it to you.
So here is the code:
<?php
/**
* Smarty plugin
*/
/**
* Smarty {url} function plugin
*
* Type: function
* Name: url
* @author: Trimo
* @mail: trimo.1992[at]gmail[dot]com
*/
function smarty_function_url($params,&$smarty)
{
if (!function_exists('current_url')) {
if (!function_exists('get_instance')) {
$smarty->trigger_error("url: Cannot load CodeIgniter");
return;
}
$CI = &get;_instance();
$CI->load->helper('url');
}
if ($params['type'] == 'string')
return uri_string();
elseif ($params['type'] == 'anchor' && isset($params['url']))
return anchor($params['url'],$params['text'],$params['attr']);
elseif ($params['type'] == 'safemail' && isset($params['url']))
return safe_mailto($params['url'],$params['text'],$params['attr']);
elseif ($params['type'] == 'mail' && isset($params['url']))
return mailto($params['url'],$params['text'],$params['attr']);
elseif ($params['type'] == 'autolink' && isset($params['url']))
return auto_link($params['url'],(isset($params['mode']))?$params['mode']:'both',($params['new'] == 1)?TRUE:FALSE);
elseif ($params['type'] == 'urltitle' && isset($params['title']))
return url_title($params['title'],(isset($params['mode']))?$params['mode']:'dash',($params['lower'] == 1)?TRUE:FALSE);
elseif ($params['type'] == 'prep' && isset($params['url']))
return prep_url($params['url']);
elseif ($params['type'] == 'current')
return current_url();
elseif ($params['type'] == 'site')
return site_url($params['url']);
else
return base_url();
}
?>
You just need to create this file in smarty's plugin folder, then you're ready to use it.
The plugin supports almost all of the functions that are in the URL helper.
{url}
This will return the base URL.
{url type="current"}
This will return the current URL.
{url type="site" url="forum/page/2"}
//Return
http://example.com/forum/page/2
This will return the site URL, and optionally you can pass the segments using url parameter.
{url type="string"}
This will return the URL segments.
{url type="prep" url="example.com"}
//Return
http://example.com
This will return the URL with http:// if it's missing.
{url type="urltitle" title="What's wrong with CSS?" mode="dash" lower=0}
//Return
Whats-wrong-with-CSS
Create a human-friendly URL string. You can use mode parameter to determine the word delimiter, it can be dash or underscore, and you can also pass the lower parameter to lowercase the string, it can be 1 or 0. (If it's 1 then the string will be lowercase)
{url type="autolink" url="http://example.com" mode="url" new=0}
//Return
<a href="http://example.com">http://example.com</a>
Converts URLs and e-mail addresses into links. You can use mode parameter to determine if only emails or urls are converted, or both, it can be email and url. You can also pass the new parameter which determines if the link is opened in a new window or not. (If it's one then the link will be shown in a new window)
{url type="anchor" url="http://example.com" text="Text of link" attr="title='Title'"}
//Return
<a href="http://example.com" title="Title">Text of link</a>
Creates a link. You can pass text parameter to define the text of the link, and attr parameter to define the attributes.
{url type="mail" url="[email protected]" text="Text of link" attr="title='Title'"}
//Return
<a href="mailto:[email protected]" title="Title">Text of link</a>
Creates an email link. You can pass text parameter to define the text of the link, and attr parameter to define the attributes.
{url type="safemail" url="[email protected]" text="Text of link" attr="title='Title'"}
It works in the same way es mail, but it creates the link with JavaScript.
If you want to know more about these functions then you can find the User Guide here.
If something is not clear, send me an email (or a PM), and I'll help.
PS: Sorry for my English.