Concatenate string with Cheetah

477 views
Skip to first unread message

Julen Landa Alustiza

unread,
Dec 28, 2015, 6:58:22 AM12/28/15
to weewx...@googlegroups.com
A question regarding cheetah templates.

Is posible to concatenate string with cheetah?

I want to generate json for chartjs with cheetah templates.

Now I have the same for statement ten times to get each of the values and I wanna reduce it to a unique for, but I would need to concatenate string with cheetah and don't know if it's possible

My idea is something like that:

#for $day in $month.days
$outTemp_max_data += $day.outTemp.max + ', '
$barometer_max_data += $day.barometer.max + ', '
#end for

And the use $outTemp_max_data and $barometer_max_data on the template.

Is this possible?

--
Julen Landa Alustiza <ju...@zokormazo.info>

gjr80

unread,
Dec 28, 2015, 7:22:00 AM12/28/15
to weewx-user
Hi,

The following (untested) should work

#for $day in $month.days
#set $outTemp_max_data += $day.outTemp.max + ', '
#set $barometer_max_data += $day.barometer.max + ', '
#end for

Be aware though that depending on your skin.conf/weewx.conf settings $day.outTemp.max might include units eg 35.5 ^C (where ^ represents the degree symbol - have worked out how to type that on the iPad) which you may or may not want. If you don't want units you might want to use .formatted or .nolabel (refer formatting options in the Customisation Guide)

You might also want to consider using the python json library, it has the advantage of taking care of the json encoding and decoding for you, in particular it takes care of converting python None values into json Null values. The downside though is that your code can become more complex, in which case it might be worth considering a weewx search list extension.

Gary

Julen Landa Alustiza

unread,
Dec 28, 2015, 7:39:00 AM12/28/15
to weewx...@googlegroups.com
Thanks!

This works for me:

#set $data_outTemp_avg = ''
#set $data_windchill_avg = ''
#for $hour in $day.hours
#if $hour.outTemp.avg.raw
#set $data_outTemp_avg += str($hour.outTemp.avg.raw) + ','
#end if
#if $hour.windchill.avg.raw
#set $data_windchill_avg += str($hour.windchill.avg.raw) + ','
#end if
#end for

I'm not using nolabel() because I have a comma separated decimal locale and need dot separated decimals for js, althrought this outputs very large decimals, I'll have to look for a better workaround but it works for now ;)


--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages