Skip to content

Smarty plugin URL Helper

Derek Jones edited this page Jul 5, 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:

Files

functions.url.php

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

Installion

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

Using

The plugin supports almost all of the functions that are in the URL helper.

base_url()

{url}

This will return the base URL.

current_url()

{url type="current"}

This will return the current URL.

site_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.

uri_string()

{url type="string"}

This will return the URL segments.

prep_url()

{url type="prep" url="example.com"}

//Return
http://example.com

This will return the URL with http:// if it's missing.

url_title()

{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)

auto_link()

{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)

anchor()

{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.

mail()

{url type="mail" url="mail@example.com" text="Text of link" attr="title='Title'"}

//Return
<a href="mailto:mail@example.com" 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.

safe_mail()

{url type="safemail" url="mail@example.com" text="Text of link" attr="title='Title'"}

It works in the same way es mail, but it creates the link with JavaScript.

Good to know

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.

Clone this wiki locally