[Smarty] string_format issues

Associate
Joined
21 May 2003
Posts
1,365
I'm writing a reporting page which is basically an account statement, and I want to use string_format to pad a balance column but I can't get it to work.

At the minute i've got this:
Code:
   CR 245.50
   CR 312.00
    CR 45.30
     CR 5.20
       75.50

But what i'd like is:
Code:
CR    245.50
CR    312.00
CR     45.30
CR      5.20
       75.50

I've tried using
Code:
{$statement.newBalance|string_format:"CR %.2f"}

and following the docs here: http://uk.php.net/sprintf but I can't seem to specify a width...

Also, if it just prints spaces then the browser will ignore all but the first, so how do I get it to print   or is there an alternative?
 
Doesn't work unfortunately.

I've now got:
Code:
<td><pre>CR{$statement.newBalance|string_format:"%20.2f"}</pre></td>

Which is preserving the spaces but it doesn't seem to be monospaced as they still aren't perfectly aligned.

I could just use a new table column to hold the CR but that would be messy.
 
I think you'll have to use either the non-breaking space (&nbsp;) to pad the values, or use the CSS white-space: pre; property.
 
Last edited:
The statement is already in a table, but having a whole seperate column just to say if it's a credit or not seems a bit overkill and not really that semantic, the CR is part of the balance field really imo.

Code:
pre{
   font-familiy: monospace;
}

...

<td><pre>CR{$statement.newBalance|string_format:"%15.2f"}</pre></td>

The above is working, but courier new is a bit ugly :( and the <pre> tag is forcing the rows to double in height for some reason :eek:
 
Thanks for the tip on white-space: pre; I can use that and remove the extra pre tags meaning the row height isn't effected.

Now using:

Code:
.preElement
{
	white-space: pre;
	font-family: monospace;
}

...

<td class="preElement">CR{$statement.newBalance|string_format:"%15.2f"}</td>

And it's all fine apart from courier :( is there a way to force arial /verdana to monospace?
 
LazyManc said:
is there a way to force arial /verdana to monospace?
Not that I'm aware of because they're not monospace fonts, meaning you'd have to adjust the spacing of each character depending on its width (which is variable in a non-monospace font).
 
use CR after the value? it'll keep it all aligned without needing another column (albeit not how you wanted it...it still makes sense)
 
Sic said:
use CR after the value? it'll keep it all aligned without needing another column (albeit not how you wanted it...it still makes sense)

Can't do that cause there will be some balances which aren't in credit, so i'd have to pad those with whitespace too and end up back at square one.

In the end i've just admitted defeat and gone for the extra table column.
 
Back
Top Bottom