Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

In this Discussion

Ready to contribute?

Amazing! Sign our contributors' agreement and then join us on GitHub.

Update for critical security issue in PHPMailer included in release Vanilla 2.3.1

Is there a way to get the post counts with commas?

This discussion is related to the ReusableStats addon.

As my number of posts is rather large, is there a way to have them output with commas in the thousands place to make it easier to read?

Comments

  • edited August 21

    A hack would be

    {math equation="floor($posts/1000)"},000
    

    This simply takes the floor of your post count divided by 1000 and then the string ",000" after it. This doesn't really do what I originally set out to do but it does get a better result in the form or an easier to read number that approximates post count. I'll call it a win. :self-high-five:

    R_J
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator
  • It rounds decimals but it doesn't change a number from 356981 to 356,981. You could use it to divide 356981 by 1000 and round the decimals and then multiply by 1000 to get a nice round number but then you risk rounding up the end sum. So in our example it would make the end result 357000 instead of 356000 and the commas would still be missing.

    R_J
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    There is a modifier which would do exactly what you need: {$posts|number_format}

    If you add that to your template, you will get an error though: "modifier 'number_format' not allowed by security setting". There are three things I've learned about modifiers when I looked for a solution to your problem (I learn while I'm helping, that's a reason why I'm doing that, by the way)
    1. You can use any php function as a modifier
    2. There are security settings which restrict usage to count function only.
    3. What I've known before is, that you can register your own smarty functions. If you need something more complex, that is a mighty tool. The third thing I've learned is that you can also register custom modifiers and it is as simple as adding

    public function gdn_smarty_init_handler($sender) {
        $sender->register_modifier('number_format', 'number_format');
    }
    

    to your themehooks file. After you have done so, you can add {$posts|number_format} to your theme.

    hgtonight
  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    You might have noticed already that there are no Smarty blocks or any template chunks in Vanilla like you might expect it from looking at other Smarty-using software. That is, because theming in Vanilla is normally done more in a developer way.

    You have your themehooks file and change the output by hooking into events. Although that might sound complicated, you would find it quite easy after a very short learning curve. It is also a lot more powerful. And if you want to, you can use it as a great start into becoming a developer.

    It's completely okay, that you build your theme with Smarty. But if your question has been more detailed (like "I want to show the post count below/next to/whereever; how can I do?) the answer would have been

    public function something_depending_on_the_target($sender, $args) {
        echo ...
    }
    

    But that was just as an information. It's your decision which way you go. Hooking into the events is the preferred approach in Vanilla, though.

    mickeybondhgtonight
Sign In or Register to comment.