Smarty Cleanurl plugin
I developed a cleanurl which makes it easy to switch between classic urls and clean urls like ( http:/www.site.com/instrument/view/1 for http://www.site.com/?component=instrument&action=view&id=1 )
before adding the component you should make some changes like in .htaccess and Smarty.class.php
in Smarty.class.php add this line ( by default it is false )
| Code: |
/**
|
among the class attributes, i use it integrated with smarty and when i want to enable cleanurls i just tell to smarty to set this value true
| Code: |
$smarty = new Smarty(); // //smarty initialization by you like template_dir etc // $smarty->cleanurl = true; |
you can use another method to enable cleanurls but for now it works like this.
then you should change your .htaccess according to your urls, for example we should write a .htaccess for above url example
| Code: |
# this is the initialization |
the last line of this .htaccess sample makes your urls clean. You should change the .htaccess according to your url structure.
And the plugin itself should be added under the plugins directory.
Then all you have to do is just putting your urls as normal urls in cleanurl’s url attribute like this one
| Code: |
{cleanurl url="?component=instrument&action=instrument&id=1" name="I am a Link"} |
it will give this output when cleanurls are not enabled
| Code: |
I am a Link |
and this one when cleanurls are enabled
| Code: |
I am a Link |
url and name attributes are required and there are other attributes like;
title : when title attribute is set it adds a title to your link e.g.<a title="TITLE", if you dont set the title name attribute will be shown as the title
extension: if you add and extension to your clean urls like instrument/view/1.html you should set this value as {cleanurl extension=”.html” ….. }
image: if you want to give link to an image you should set the image attribute and it will have a link and with image_class attribute you can give a class for your image
When using with lists you should use active and id lets explain it with an example if you have a list like this, before this
$active should be assigned with smarty assign or you can just use $smarty.get.action for active attribute, for this example in php you should do this
| Code: |
$action = isset($_GET['action']) ? $_GET['action'] : '';
$smarty->assign('action',$action);
|
| Code: |
|
when the assigned value for id and active matches, the cleanurl will not show a link it will show only the text inside a span
if you want to use this for a horizontal list also you can try this css
| Code: |
.horizontal_list { list-style-type: none; padding: 0; margin: 0; }
.horizontal_list li { display: inline; }
.horizontal_list li a { padding-left: 7px; }
.horizontal_list li span { font-weight: bold; color: #3B5998; padding-left: 7px; }
|
and here is the code, and one thing to add i always use base href in my sites if you dont use it you may want to change the plugin and add the base_url in front of the cleanurl, that s up to you.
I hope it helps,
just copy the below code and save it and copy the saved file under smarty plugins fodler, function.cleanurl.php
| Code: |
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
</code><code>
/**
* Smarty {cleanurl} function plugin
*
* Type: function<br>
* Name: cleanurl<br>
* Date: July 1, 2009<br>
* Purpose: Cleans the urls to make them search-engine-friendly<br>
* Input:<br>
* - url = url for cleaning
* - name = name for link
* - title = title for link
*
* Examples: {cleanurl url="http://cleanurl.com/?sec=software&com=firma&act=info&id=1" name="Clean"}
* Output: http://cleanurl.com/software/firma/info/1
* @link http://smarty.php.net/manual/en/language.function.cleanurl.php {cleanurl}
* (Smarty online manual)
* @author Onur GUZEL onur.oguzel[at]gmail.com
* @version 0.1
* @param array
* @param Smarty
* @return string
*/
function smarty_function_cleanurl($params, &$smarty) {
foreach ( $params as $_key => $_val ) {
switch ($_key) {
case 'url':
case 'name':
case 'title':
case 'extension':
case 'image':
case 'image_class':
case 'active':
case 'id':
case 'askmessage':
$$_key = (string)$_val ;
break;
default :
$smarty->trigger_error ( "cleanurl: no permission to add extra attribute '$_key'", E_USER_NOTICE );
break;
}
}
if (empty ( $url )) {
$smarty->trigger_error ( "cleanurl: missing 'url' parameter", E_USER_NOTICE );
return;
}
if (empty ( $name )) {
$smarty->trigger_error ( "cleanurl: missing 'name' parameter", E_USER_NOTICE );
return;
}
if( empty($title) )
{
$title = $name;
}
$urls = explode('&',$url);
$urlstring = '';
if( sizeof($urls) )
{
$j=0;
foreach( $urls as $url_ )
{
$url_tmp = explode('=',$url_);
$urlstring .= $url_tmp[1];
if($j!=sizeof($urls)-1) $urlstring .= '/';
$j++;
}
}
else
{
$smarty->trigger_error ( "cleanurl: please check the url attribute", E_USER_NOTICE );
}
if( isset($extension) )
{
$urlstring .= $extension;
}
$stringurl = '';
$image_src = '';
if( count($image) )
{
$image_src = '<img src="'.$image.'" border="0" alt="'.$name.'" title="'.$title.'"/>';
$name = $image_src;
}
if( $smarty->cleanurl )
{
$stringurl = '<a href="'.$urlstring.'" title="'.$title.'">'.$name.'</a>';
}
else
{
$stringurl = '<a href="'.$url.'" title="'.$title.'">'.$name.'</a>' ;
}
if( isset($active) && isset($id) && $active == $id ) $stringurl = '<span title="'.$title.'">'.$name.'</span>';
return $stringurl;
}
?> |
Regards