I am trying to modernize some code, and could use ...
# cfml-beginners
j
I am trying to modernize some code, and could use some help. How can I change this code to cfscript? The code below is processing a form, which is not shown, because it's just a basic form. Thanks in advanced for any help!
Copy code
<cfif #form.conversionType# is "CtoF">   <cfinvoke component="convertTemp" method="ctof" returnvariable="newtemp" temp=#form.temperature#>
        <cfoutput>#form.temperature# degrees Celsius is #newtemp# degrees Farenheit.</cfoutput>
    <cfelseif #form.conversionType# is "FtoC">
        <cfinvoke component="convertTemp" method="ftoc" returnvariable="newtemp" temp=#form.temperature#>
    <cfoutput>#form.temperature# degrees Fahrenheit is #newtemp# degrees Celsius.</cfoutput>
</cfif>
a
Which part is giving you problems? What have you tried? How did it not work?
c
It would indeed be helpful to know what you already tried and what is not working. In general, I would recommend putting most of the logic into the CFC . Then you can just make sure the form values exist and call a CFC method. So the code in the CFM could be something like this:
Copy code
// if conversionType exists, the form was sent. Make sure temperature is a numeric value
    if( structKeyExists( form, 'conversionType') && isNumeric( form.temperature ) ) {
        conversionResult = new convertTemp.convert( direction=form.conversionType, temperature=form.temperature )

        writeOutput( '#form.temperature# degrees #conversionResult.from# is #conversionResult.newTemp# #<http://conversionResult.to#|conversionResult.to#>.' );
    }
In the example above, the CFC method would return a struct with the output values. In case you don't need the values later in the request, you could just as well have it return a complete output string.
p
If modernizing it; convert it to jquery or something all handled on the UI side. https://www.codementor.io/blog/temperature-converter-cpcfuc9e14
j
@johnbarrett this is a good resource as well. https://modern-cfml.ortusbooks.com/
j
That is the cheap and dirty way to do it. Mind you it is probably not the best way LOL
a
I checked John's code on cfscript.me, and it probably made it worse if anything.
m
As a simple mockup I faked the CFC into just an object with functions, but it would work the same way. https://trycf.com/gist/62f83a419668a6acecb327bf9874b79f/acf2016?theme=monokai