Python Template Jinja2: Reuse the Macro, Preserve Zero

HTML for this example

Select the code and use your copy shortcut. Changes in this preview are temporary.

<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Python Template Jinja2: Reuse the Macro, Preserve Zero</title><style>*{box-sizing:border-box}body{margin:0;color:var(--ink,#233342);background:var(--bg,#eef2f3);font:17px/1.6 system-ui,sans-serif}main{max-width:980px;margin:auto;padding:clamp(16px,4vw,38px)}h1{font:700 clamp(30px,5vw,49px)/1.12 Georgia,serif;margin:10px 0 20px;max-width:800px}h2{font-size:24px;line-height:1.3;margin:0 0 15px}h3{font-size:19px;margin:12px 0}p{margin:0 0 16px}.eyebrow{font-weight:750;letter-spacing:.08em;text-transform:uppercase;font-size:16px}header{padding:10px 0 25px;border-bottom:3px solid var(--accent,#24586d);margin-bottom:24px}.lede{font-size:20px;max-width:760px}.panel{background:var(--paper,#fff);padding:clamp(18px,3vw,28px);border:1px solid var(--line,#c6d1d8);border-radius:12px;margin:20px 0}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:20px}.grid>*{min-width:0}.row{display:flex;gap:12px;flex-wrap:wrap;align-items:center;margin:16px 0}.stack>*{margin:12px 0}button{font:700 17px/1.4 system-ui,sans-serif;cursor:pointer;color:white;background:var(--accent,#24586d);border:2px solid var(--accent,#24586d);border-radius:7px;padding:11px 17px;max-width:100%;overflow-wrap:anywhere}button.secondary{background:transparent;color:var(--ink,#233342)}button[aria-pressed=true]{box-shadow:inset 0 0 0 3px #fff;outline:2px solid var(--ink,#233342)}button:disabled{opacity:.55;cursor:default}input,textarea,select{font:17px/1.5 system-ui,sans-serif;color:inherit;background:white;border:1px solid #778591;padding:10px 12px;border-radius:6px;max-width:100%;width:100%;min-width:0}input[type=checkbox]{width:22px;height:22px}textarea{min-height:110px;resize:vertical}label{display:block;font-weight:700;margin:12px 0 7px}button:focus-visible,input:focus-visible,textarea:focus-visible,summary:focus-visible,[tabindex]:focus-visible{outline:3px solid #aa3d70;outline-offset:4px}.result{padding:18px;border-left:5px solid var(--accent,#24586d);background:var(--soft,#e5eff2);white-space:pre-wrap;overflow-wrap:anywhere;margin:20px 0}.result:empty{display:none}.hint{font-size:16px;color:var(--muted,#435968)}table{border-collapse:collapse;width:100%;table-layout:fixed;margin:16px 0}th,td{text-align:left;vertical-align:top;overflow-wrap:anywhere;padding:12px 8px;border-bottom:1px solid var(--line,#c6d1d8)}caption{text-align:left;font-weight:750;margin:8px 0}details{padding:18px;border:1px dashed #7b8b96;margin:20px 0}summary{cursor:pointer;font-weight:750}pre{font:16px/1.6 ui-monospace,monospace;white-space:pre-wrap;overflow-wrap:anywhere;background:#162735;color:#f5f8fa;padding:18px;border-radius:8px}canvas,svg{display:block;max-width:100%;height:auto}footer{margin:28px 0;font-size:16px}.badge{font-weight:750;color:var(--accent,#24586d)}.metric{font:700 38px/1.3 Georgia,serif}.board{display:grid;gap:5px;max-width:440px;margin:20px auto}.board button{padding:7px;min-height:45px}.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)}@media(max-width:560px){.grid{grid-template-columns:1fr}main{padding:16px}th,td{padding:10px 5px}.row button{flex-grow:1}.metric{font-size:32px}}
:root{--bg:#eeeef4;--paper:#fff;--accent:#62518a;--soft:#eae3f5;--ink:#3e3354}dl{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr);gap:14px;font-size:21px}dt{font-weight:750;overflow-wrap:anywhere}dd{margin:0;overflow-wrap:anywhere}.expected{border:2px dashed #9b8ab6}</style></head><body><main><header><p class="eyebrow">Macro contract inspector · Expected output</p><h1>Python Template Jinja2: Reuse the Macro, Preserve Zero</h1><p class="lede">One imported macro, two calls, ordinary text and a legitimate integer zero.</p><p class="hint">Browser-side source and expected-output inspector. It does not execute Python or Jinja; native environment and render tests remain separate.</p></header><section class="panel"><h2>Compare the supplied input revisions</h2><div class="row"><button id="v1" type="button">V1 · A&amp;B / Count 0</button><button id="v2" type="button">V2 · Tag-like label</button><button id="missing" type="button">Missing count scenario</button><button id="reset" type="button" class="secondary">Reset V1</button></div><p id="status" class="result" role="status"></p><pre id="arguments"></pre><section id="expected" class="panel expected"><h2>Expected visible text · Not engine output</h2><dl><dt id="firstTerm"></dt><dd id="firstValue"></dd><dt id="secondTerm"></dt><dd id="secondValue"></dd></dl></section><h2>Expected relevant serialization</h2><pre id="serialized"></pre></section><section class="panel"><h2>Copyable native template source</h2><pre id="templates"></pre><h2>Explicit environment setup</h2><pre id="environment"></pre><p>Actual Python version, installed Jinja version, loader resolution and native render tests: Pending.</p></section><footer>Render the copied templates with Jinja and compare the literal label and retained zero.</footer></main><script>
(()=>{'use strict';const $=id=>document.getElementById(id);
let revision='V1';const macro='{% macro summary(label, value) -%}\n<dt>{{ label }}</dt>\n<dd>{{ value }}</dd>\n{%- endmacro %}',caller='{% import "macros.html" as ui %}\n<dl>\n{{ ui.summary(first_label, first_value) }}\n{{ ui.summary(second_label, second_value) }}\n</dl>';const escape=s=>String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&#34;').replace(/'/g,'&#39;');function show(){const data={first_label:revision==='V2'?'<em>Note</em>':'A&B',first_value:'Ready',second_label:'Count',second_value:0};if(revision==='Missing')delete data.second_value;$('arguments').textContent=JSON.stringify(data,null,2);$('templates').textContent='macros.html\n'+macro+'\n\nindex.html\n'+caller;$('environment').textContent='from jinja2 import Environment, DictLoader, select_autoescape, StrictUndefined\n\ntemplates = {"macros.html": '+JSON.stringify(macro)+', "index.html": '+JSON.stringify(caller)+'}\nenv = Environment(loader=DictLoader(templates),\n    autoescape=select_autoescape(enabled_extensions=("html",),\n        default_for_string=True, default=False),\n    undefined=StrictUndefined)\n# Validate required keys/types before rendering. Zero is valid.\n# Missing/null required values hold the render for clarification.\n# Native call after validation:\n# env.get_template("index.html").render(**context)';const missing=!Object.prototype.hasOwnProperty.call(data,'second_value')||data.second_value===null;$('expected').hidden=missing;$('serialized').textContent=missing?'HELD · Required second_value is missing; no invented output.':'<dl>\n<dt>'+escape(data.first_label)+'</dt>\n<dd>Ready</dd>\n<dt>Count</dt>\n<dd>0</dd>\n</dl>';$('firstTerm').textContent=missing?'':data.first_label;$('firstValue').textContent=missing?'':'Ready';$('secondTerm').textContent=missing?'':'Count';$('secondValue').textContent=missing?'':'0';$('status').textContent=missing?'Missing input: second_value [TO CONFIRM]. Expected preview held. Native render not performed.':revision+' expected snapshot. Zero retained; ordinary labels escaped in expected serialization. Native Jinja verification pending.';}for(const[k,v]of [['v1','V1'],['v2','V2'],['missing','Missing'],['reset','V1']])$(k).addEventListener('click',()=>{revision=v;show();});show();
})();
</script></body></html>