Skip to content

Smarty plugin URL Helper

World Wide Web Server edited this page Jul 4, 2012 · 8 revisions

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:

[h2]Files[/h2] [h4]functions.url.php[/h4] [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();

} ?> [/code]

[h2]Installion[/h2] You just need to create this file in smarty's plugin folder, then you're ready to use it.

[h2]Using[/h2] The plugin supports almost all of the functions that are in the URL helper.

[h3]base_url()[/h3] [code]{url}[/code] This will return the base URL.

[h3]current_url()[/h3] [code]{url type="current"}[/code] This will return the current URL.

[h3]site_url()[/h3] [code] {url type="site" url="forum/page/2"}

//Return http://example.com/forum/page/2 [/code] This will return the site URL, and optionally you can pass the segments using [b]url[/b] parameter.

[h3]uri_string()[/h3] [code]{url type="string"}[/code] This will return the URL segments.

[h3]prep_url()[/h3] [code] {url type="prep" url="example.com"}

//Return http://example.com [/code] This will return the URL with http:// if it's missing.

[h3]url_title()[/h3] [code] {url type="urltitle" title="What's wrong with CSS?" mode="dash" lower=0}

//Return Whats-wrong-with-CSS [/code] Create a human-friendly URL string. You can use [b]mode[/b] parameter to determine the word delimiter, it can be [i]dash[/i] or [i]underscore[/i], and you can also pass the [b]lower[/b] parameter to lowercase the string, it can be [i]1[/i] or [i]0[/i]. (If it's 1 then the string will be lowercase)

[h3]auto_link()[/h3] [code] {url type="autolink" url="http://example.com" mode="url" new=0}

//Return http://example.com [/code] Converts URLs and e-mail addresses into links. You can use [b]mode[/b] parameter to determine if only emails or urls are converted, or both, it can be [i]email[/i] and [i]url[/i]. You can also pass the [b]new[/b] 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)

[h3]anchor()[/h3] [code] {url type="anchor" url="http://example.com" text="Text of link" attr="title='Title'"}

//Return Text of link [/code] Creates a link. You can pass [b]text[/b] parameter to define the text of the link, and [b]attr[/b] parameter to define the attributes.

[h3]mail()[/h3] [code] {url type="mail" url="[email protected]" text="Text of link" attr="title='Title'"}

//Return Text of link [/code] Creates an email link. You can pass [b]text[/b] parameter to define the text of the link, and [b]attr[/b] parameter to define the attributes.

[h3]safe_mail()[/h3] [code] {url type="safemail" url="[email protected]" text="Text of link" attr="title='Title'"}[/code] It works in the same way es mail, but it creates the link with JavaScript.

[h2]Good to know[/h2] If you want to know more about these functions then you can find the User Guide [url=http://codeigniter.com/user_guide/helpers/url_helper.html]here[/url].

If something is not clear, send me an email (or a PM), and I'll help.

PS: Sorry for my English.

Clone this wiki locally