1- <!-- filepath: d:\Code\hextra-notes\layouts\shortcodes\math\calc3.html -->
2- {{ $id := printf "calc_%d" now.UnixNano }}
3- {{ $formula := .Get "formula" | default "a + b" }}
4-
5- < div id ="{{ $id }} ">
6- < label for ="formula-{{ $id }} "> Formula:</ label >
7- < input type ="text " id ="formula-{{ $id }} " value ="{{ $formula }} ">
1+ {{/* Generate a unique ID for this instance */}}
2+ {{- $uid := .Page.Scratch.Get "addCounter" | default 0 -}}
3+ {{- .Page.Scratch.Set "addCounter" (add $uid 1) -}}
84
9- {{ range $key, $value := .Params }}
10- {{ if ne $key "formula" }}
11- < label for ="{{ $key }}-{{ $id }} "> {{ $key }}:</ label >
12- < input type ="number " id ="{{ $key }}-{{ $id }} " value ="{{ $value }} ">
13- {{ end }}
5+ {{ $formula := .Get "formula" | default "a + b" }}
6+ < div id ="calc-{{$uid}} " class ="calc-box ">
7+ {{ range $key, $value := .Params }}
8+ {{ if ne $key "formula" }}
9+ < label >
10+ {{ $key }}:
11+ < input type ="number " id ="calc-{{$uid}}-{{$key}} " value ="{{ $value }} ">
12+ </ label >
13+ < br >
1414 {{ end }}
15-
16- < button onclick ="calculate_{{ $id }}() "> Calculate</ button >
17- < p > Result: < span id ="result-{{ $id }} "> </ span > </ p >
15+ {{ end }}
16+ < label >
17+ Formula:
18+ < input type ="text " id ="calc-{{$uid}}-formula " value ="{{ $formula }} ">
19+ </ label >
20+ < br >
21+ < button type ="button " onclick ="calcSolve('calc-{{$uid}}') "> Solve</ button >
22+ < div id ="calc-{{$uid}}-result "> Result: </ div >
1823</ div >
1924
2025< script >
21- function calculate_ { { $id } } ( ) {
22- try {
23- const formulaInput = document . getElementById ( 'formula-{{ $id }}' ) . value ;
24- const vars = { } ;
25- { { range $key , $value := . Params } }
26- { { if ne $key "formula" } }
27- vars [ "{{ $key }}" ] = parseFloat ( document . getElementById ( '{{ $key }}-{{ $id }}' ) . value ) || 0 ;
28- { { end } }
29- { { end } }
26+ function calcSolve ( boxId ) {
27+ const box = document . getElementById ( boxId ) ;
28+ const inputs = box . querySelectorAll ( 'input[type="number"]' ) ;
29+ let formula = box . querySelector ( 'input[type="text"]' ) . value ;
30+ inputs . forEach ( input => {
31+ const varName = input . id . split ( '-' ) . pop ( ) ;
32+ formula = formula . replace ( new RegExp ( '\\b' + varName + '\\b' , 'g' ) , input . value || '0' ) ;
33+ } ) ;
34+ try {
35+ // For public sites, use a math parser instead of eval for safety!
36+ const result = eval ( formula ) ;
37+ box . querySelector ( '[id$="result"]' ) . textContent = "Result: " + result ;
38+ } catch {
39+ box . querySelector ( '[id$="result"]' ) . textContent = "Result: Error in formula" ;
40+ }
41+ }
42+ </ script >
3043
31- const result = new Function ( ...Object . keys ( vars ) , 'return ' + formulaInput + ';' ) ( ...Object . values ( vars ) ) ;
32- document . getElementById ( 'result-{{ $id }}' ) . textContent = result ;
33- } catch ( error ) {
34- document . getElementById ( 'result-{{ $id }}' ) . textContent = "Invalid Formula" ;
35- console . error ( "Error in calculate_{{ $id }}():" , error ) ;
36- }
44+ < style >
45+ .calc-box {
46+ border : 1px solid # ccc ; border-radius : 8px ; padding : 1em ; margin : 1em 0 ; max-width : 320px ;
47+ background : # f9f9f9 ; font-family : system-ui, sans-serif;
3748}
38- </ script >
49+ .calc-box label { display : block; margin-bottom : 0.5em ; }
50+ .calc-box input { margin-left : 0.5em ; }
51+ .calc-box button { margin-top : 0.5em ; }
52+ .calc-box div [id $= "result" ] { margin-top : 0.5em ; font-weight : bold; color : # 007800 ; }
53+ </ style >
0 commit comments