Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

New modifier for Smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
jotango
Smarty Rookie


Joined: 20 Apr 2005
Posts: 13

PostPosted: Wed Feb 15, 2006 4:09 pm    Post subject: New modifier for Smarty Reply with quote

We use a custom modifier at Hitflip to build search engine friendly static urls for our files. We try and convert all funky characters, not to %20 html stuff, but to different characters. So the page:

dvd.php?id=12

will turn into dvd_die_hard_3_with_bruce_willis_12.html

to generate the die hard 3 bit we use the following modifier.

Code:

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 * @author fabian.jager@hitflip.de
 * @author jan@hitflip.de
 */


/**
 * Smarty regex_replace modifier plugin
 *
 * Type:     modifier<br>
 * Name:     seo_url<br>
 * Purpose:  create search engine friendly links
 * @param string
 * @return string
 */
function smarty_modifier_seo_url($string)
{
        $search = array();
        $replace = array();

        $search[] = "(\W)";
        $replace[] = "_";
        $search[] = "/,/";
        $replace[] = "_";
        $search[] = "/\"/";
        $replace[] = "'";
        $search[] = "/,/";
        $replace[] = "_";
        $search[] = "/Ä/";
        $replace[] = "Ae";
        $search[] = "/Ö/";
        $replace[] = "Oe";
        $search[] = "/Ü/";
        $replace[] = "Ue";
        $search[] = "/ä/";
        $replace[] = "ae";
        $search[] = "/ö/";
        $replace[] = "oe";
        $search[] = "/ü/";
        $replace[] = "ue";
        $search[] = "/ß/";
        $replace[] = "ss";
        $search[] = "/é/";
        $replace[] = "e";
        $search[] = "/è/";
        $replace[] = "e";
        $search[] = "/ê/";
        $replace[] = "e";
        $search[] = "/á/";
        $replace[] = "a";
        $search[] = "/à/";
        $replace[] = "a";
        $search[] = "/â/";
        $replace[] = "a";
        $search[] = "/__+/";
        $replace[] = "_";

        $string = preg_replace($search, $replace, $string);

        if($string[0] == "_")
                $string = substr($string, 1);

        if($string[strlen($string)-1] == "_")
                $string = substr($string, 0, strlen($string)-1);

        return $string;
}



Perhaps useful for someone else, or for inclusion in Smarty?
Back to top
View user's profile Send private message
jotango
Smarty Rookie


Joined: 20 Apr 2005
Posts: 13

PostPosted: Wed Feb 15, 2006 4:14 pm    Post subject: one more thing Reply with quote

all this does is build the text. You'll have to build the url, htaccess etc. for yourself. I typically do something like

Code:
<a href="dvd_{$dvd.title|seo_url}_{$dvd.id}.html">goto the dvd</a>
Back to top
View user's profile Send private message
mitchenall
Smarty Pro


Joined: 27 Feb 2004
Posts: 107
Location: London, UK

PostPosted: Thu Feb 16, 2006 4:09 pm    Post subject: Re: one more thing Reply with quote

jotango wrote:
all this does is build the text. You'll have to build the url, htaccess etc. for yourself. I typically do something like

Code:
<a href="dvd_{$dvd.title|seo_url}_{$dvd.id}.html">goto the dvd</a>


Interesting, but personally, I think it's easier to handle this in my dataobjects so that I just have 1 place to change it if the scheme changes.

Code:
<a href="{$dvd->getURL()}">Go to the DVD</a>


Alternatively, after looking at Rails recently, I figured that I should perhaps create a {url_for} plug-in which could use the getURL() method of the dataobject if it exists, or perhaps raising an exception if it doesn't or maybe just generate a generic URL for these objects within my framework.

Code:
<a href="{url_for dataobject=$dvd}">Go to the DVD</a>


I think having the code for making the URL cleaner in the smarty plug-in is definitely sensible, but the actual code for building the URL should be more central IHMO rather than in the templates themselves as it saves a lot of duplication and maintenance if they change.

Another advantage is that I could which method the url_for plug-in used based on template context, etc., e.g. if the template was being used for creating an email, the url_for plugin could automatically make it a full URL, etc.

Best,
Mark
_________________
Mark Mitchenall
Back to top
View user's profile Send private message Visit poster's website
jotango
Smarty Rookie


Joined: 20 Apr 2005
Posts: 13

PostPosted: Thu Feb 16, 2006 4:18 pm    Post subject: Re: one more thing Reply with quote

Quote:
Interesting, but personally, I think it's easier to handle this in my dataobjects so that I just have 1 place to change it if the scheme changes.

Code:
<a href="{$dvd->getURL()}">Go to the DVD</a>


<snip>

I think having the code for making the URL cleaner in the smarty plug-in is definitely sensible, but the actual code for building the URL should be more central IHMO rather than in the templates themselves as it saves a lot of duplication and maintenance if they change.


Hi Mark,

I see where you're coming from, but we have the rule that the outside view of our pages should change as seldomly as possible, again to optimize for search engines.

So, to change the way a static page is generated, I modify the smarty modifier. This is mostly when we discover a new character which needs "transformation". Changes in the code structure are caught by modifiying the .htaccess. So while dvd_whatever_12.html will always point to the DVD with the ID 12, the script lying behind it may (and does) change.

I guess its just a different approach.

I have some experience with integrating getURL type methods in to the data objects. I tend to shy away from doing that because the functions tend to become quite complex and build complicated, long URLs, especially to catch eventualities.

Best,

Jan
Back to top
View user's profile Send private message
mitchenall
Smarty Pro


Joined: 27 Feb 2004
Posts: 107
Location: London, UK

PostPosted: Thu Feb 16, 2006 4:45 pm    Post subject: Re: one more thing Reply with quote

jotango wrote:

I see where you're coming from, but we have the rule that the outside view of our pages should change as seldomly as possible, again to optimize for search engines.


Absolutely, but if you're sharing some dataobjects between projects, or changing the URL layout during development, I still don't want to go through loads of templates looking for these URLs.

jotango wrote:
I have some experience with integrating getURL type methods in to the data objects. I tend to shy away from doing that because the functions tend to become quite complex and build complicated, long URLs, especially to catch eventualities.


Not really... something like...

Code:

class dvd extends DB_Dataobject {

  function getURL() {
    return '/dvd/'. url_encode($this->title).'/'.$this->dvd_id.'.html' ;
  }

}


I don't see what's complicated here. The smarty plugin can easily test for the existence of this method.

Best,

Mark
_________________
Mark Mitchenall
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP