@@ -133,15 +133,36 @@ fn flush_delayed_helper<'reg, 'rc>(
133133 } )
134134}
135135
136+ fn sum_helper < ' reg , ' rc > (
137+ helper : & handlebars:: Helper < ' rc > ,
138+ _r : & ' reg Handlebars < ' reg > ,
139+ _ctx : & ' rc Context ,
140+ _rc : & mut handlebars:: RenderContext < ' reg , ' rc > ,
141+ writer : & mut dyn handlebars:: Output ,
142+ ) -> handlebars:: HelperResult {
143+ let mut sum = 0f64 ;
144+ for v in helper. params ( ) {
145+ sum += v
146+ . value ( )
147+ . as_f64 ( )
148+ . ok_or_else ( || RenderError :: new ( "invalid number in sum" ) ) ?;
149+ }
150+ write ! ( writer, "{sum}" ) ?;
151+ Ok ( ( ) )
152+ }
153+
136154const STATIC_TEMPLATES : Dir = include_dir ! ( "$CARGO_MANIFEST_DIR/sqlpage/templates" ) ;
137155
138156impl AllTemplates {
139157 pub fn init ( ) -> anyhow:: Result < Self > {
140158 let mut handlebars = Handlebars :: new ( ) ;
159+
141160 handlebars_helper ! ( stringify: |v: Json | v. to_string( ) ) ;
142161 handlebars. register_helper ( "stringify" , Box :: new ( stringify) ) ;
162+
143163 handlebars_helper ! ( default : |a: Json , b: Json | if a. is_null( ) { b} else { a} . clone( ) ) ;
144164 handlebars. register_helper ( "default" , Box :: new ( default) ) ;
165+
145166 handlebars_helper ! ( entries: |v: Json | match v {
146167 serde_json:: value:: Value :: Object ( map) =>
147168 map. into_iter( )
@@ -154,9 +175,18 @@ impl AllTemplates {
154175 . collect( ) ,
155176 _ => vec![ ]
156177 } ) ;
178+
157179 handlebars. register_helper ( "entries" , Box :: new ( entries) ) ;
158180 handlebars. register_helper ( "delay" , Box :: new ( delay_helper) ) ;
159181 handlebars. register_helper ( "flush_delayed" , Box :: new ( flush_delayed_helper) ) ;
182+
183+ handlebars_helper ! ( plus: |a: Json , b: Json | a. as_i64( ) . unwrap_or_default( ) + b. as_i64( ) . unwrap_or_default( ) ) ;
184+ handlebars. register_helper ( "plus" , Box :: new ( plus) ) ;
185+
186+ handlebars_helper ! ( minus: |a: Json , b: Json | a. as_i64( ) . unwrap_or_default( ) - b. as_i64( ) . unwrap_or_default( ) ) ;
187+ handlebars. register_helper ( "minus" , Box :: new ( minus) ) ;
188+
189+ handlebars. register_helper ( "sum" , Box :: new ( sum_helper) ) ;
160190 let mut this = Self {
161191 handlebars,
162192 split_templates : FileCache :: new ( ) ,
0 commit comments