 |
Smarty
The discussions here are for Smarty, a template engine for the PHP programming language. Dedicated server web hosting provided by Guru-host.eu. |
| View previous topic :: View next topic |
| Author |
Message |
g00fy Smarty Rookie
Joined: 07 Dec 2005 Posts: 19
|
Posted: Sat Dec 05, 2009 9:57 pm Post subject: sprintf-like module |
|
|
I called it function.sprintf.php (and I use it succesfully with Smarty 3):
| Code: | <?php
function smarty_function_sprintf($params, $smarty, $template)
{
if ( !isset($params['format']) )
{
trigger_error("sprintf: required parameter 'format' missing.", E_USER_WARNING);
return false;
}
$fmt = $params['format'];
$fmt_e = explode('%', $fmt);
# Do NOT skip the first element when the first character is '%'
$skip_next = substr($fmt, 0, 1) != '%';
foreach( $fmt_e as $idx => $piece )
{
if ( $skip_next )
{
$skip_next = false;
continue;
}
if ( $piece == '' ) // %%
{
unset($fmt_e[$idx]);
$skip_next = true;
continue;
}
# read name until next space
$name = '';
for( $i = 0, $total = strlen($piece); $i < $total; ++$i )
{
if ( strpos(" \r\t\n", $piece{$i}) !== false )
break;
$name .= $piece{$i};
}
if ( !isset($params[$name]) )
{
trigger_error( "sprintf: unknown argument '%{$name}'.", E_USER_WARNING );
continue;
}
$fmt_e[$idx] = $params[$name] . substr($piece, strlen($name));
}
return implode($fmt_e);
} |
Usage:
| Code: | | {sprintf format="Hallo %World, it is now %hour o'clock" World="Someone out there" hour="{$some_date|date_format:"%H"}"} |
Depending on the $some_date variable you can get something like this:
| Code: | | Hallo Someone out there, it is now 12 o'clock |
For those who like it : enjoy.
For those who don't, please mail me at /dev/null. |
|
| Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7047 Location: Lincoln Nebraska, USA
|
Posted: Sat Dec 05, 2009 11:41 pm Post subject: |
|
|
Or directly as modifier:
{"Hallo %s, it is now %s o'clock"|sprintf:"Someone out there":{$some_date|date_format:"%H"}}
or directly as func:
{sprintf("Hallo %s, it is now %s o'clock","Someone out there",$some_date|date_format:"%H")} |
|
| Back to top |
|
g00fy Smarty Rookie
Joined: 07 Dec 2005 Posts: 19
|
Posted: Sun Dec 06, 2009 4:17 pm Post subject: |
|
|
| Or that of course, but I needed to work with the %year etc, because "%s" is too abstract to be used at i10n in combination with endusers. |
|
| Back to top |
|
bimal Smarty Elite

Joined: 19 Apr 2007 Posts: 419 Location: Kathmandu, Nepal
|
Posted: Mon Dec 14, 2009 8:07 pm Post subject: PHP Default sprintf |
|
|
Using PHP's default sprintf would be better, when we are working with unknown number of parameters.
If we write a plugin in like sprintf ourselves, we have to handle the number of parameters in advance.
| Code: | | {'Hello %s, The time is: %s'|sprintf:$name:$time} |
If sprintf is a PHP default, then, we can write any number of paramters seprated with a colon ( : ).
And g00fy's attempt is respectable - but I suggest to rename this, as sprintf is already available. _________________ Skype: pbimal
To hire instantly as a freelancer - https://www.odesk.com/o/profiles/users/_~~657b70cc7f2c616a/
Visit my website - http://bimal.org.np/ for more articles
800+ screenshots to learn about website mistakes: http://mistakes.sanjaal.com/ |
|
| Back to top |
|
|
|
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
|