XF 2.0 Custom BB code to display temperature in both F and C

Bonsai Coder

Active member
I am trying to write custom BBcode for my site that allows a user to enter a temperature and have it display in both C and F. For example:

[temp=C]0[/temp]

0 C / 32 F

I am using this code:
Rich (BB code):
<script type="text/javascript">if( ("{SIMPLETEXT}".substring(0,1)=="C")||("{SIMPLETEXT}".substring(0,1)=="c")){document.write("{TEXT}&deg; C / " +(({TEXT}*1.8) +32).toFixed(2) +"&deg; F " );}else {document.write("" +  (({TEXT}-32)/1.8).toFixed(2) + "&deg; C  / {TEXT}&deg F");}</script>

Simple replacement, no option parameter.

I can't seem to get it to work(?)
 
Last edited:
I would go with a php callback and an option parameter. JS seems to be ugly at this point.

I am an advanced neophyte when it comes to coding. Any additional information would be great :) Coding excerpts or just a similar example if you are aware of one. I know what you are suggesting I do... I just don't know how to do it :)
 
Last edited:
PHP:
<?php
namespace Your/Addon;

class TestBB {
   
    // do your conversion stuff here
    public static function convertTemp(float $temp, String $fromUnit) {
       
        return 1;
       
    }
   
    public static function renderTestBB($tagChildren, $tagOption, $tag, array $options, \XF\BbCode\Renderer\AbstractRenderer $renderer) {
       
        if($tagOption)
            // catch errors
            return self::convertTemp(floatval($tagChildren[0]), $tagOption);
        else
            return "some error or default stuff";
       
    }
}

28750153447_cb1dc7458e_o.png
 
Top Bottom