Forum Moderators: coopster

Message Too Old, No Replies

smarty function

         

hanyaz

1:13 pm on Nov 6, 2008 (gmt 0)

10+ Year Member



hi,
i've never tried using smarty, this is the first time...i got a small site i want to convert it into smarty site. In this site i got few functions that i want to make compatible with smarty standard so to be able to use them. example, i got this function :
function code_url($string) {
$string = strtolower($string);
$string = str_replace("( ¦')", "-", $string);
$accent = array('&','â','à','é','è','ê','î','ô','û','ç');
$sans_accent = array('and','a','a','e','e','e','i','o','u','c');
$string = str_replace($accent, $sans_accent, $string);
$string = eregi_replace("[^a-z0-9]","-",$string);
$string = eregi_replace("(^(_)*¦(_)*$)","",$string);
$string = eregi_replace("(-){2,3}","",$string);
return $string;
}

How can i convert it to a smarty function and use it in the template ?
Thanks for help
yaz

cameraman

3:44 pm on Nov 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you saying that you want to make them smarty plugins? You don't really need to do that just to use them.
$tpl = new Smarty();
$sans = code_url($my_data_from_somewhere);
$tpl->assign('url',$sans);

Then put {$url} in the template content wherever it is you need it.

If you really want to make them template functions, the easiest way is to pick a plugin of the type you want yours to be, open it in your editor, replace the function name & content with yours, and save it as a different name. If you want this one to be a modifier function, for example:

  1. open libs/plugins/modifier.date_format.php
  2. Change the function name - it has to start with smarty_modifier_ so perhaps smarty_modifier_code_url
  3. Replace the function content with yours
  4. Save the file as libs/plugins/modifer.code_url.php

I've been using smarty for quite a while now, and I've only ever had to write 2 plugins, one of them being their db example adapted to my db class (and actually I may have only used it once ;) )

hanyaz

7:49 pm on Nov 6, 2008 (gmt 0)

10+ Year Member



hi,
I just copied and pasted what u wrote :
$tpl = new Smarty();
$sans = code_url('trial sentence');
$tpl->assign('url',$sans);
i put the above part in my index.php
and the {$url} in my index.tpl
but a blank thing
i replaced $my_data_from_somewhere by 'trial sentence'
I got a blank page.

cameraman

9:40 pm on Nov 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And you put
$tpl->display('index.tpl');
after assigning everything you needed to assign?
If so, check that your template directories are set up correctly and all that fun stuff.