Pretty Print Raw MiniJinja JSON Data On A Template
I use MiniJinja^mj^^ to create the templates for my site. The output data comes in from Serde^se^^ in a JSON like syntax. I use this snippet to pretty print it so it's easier to work with:
-
this is new code to look at:
Code
Code
<input type="hidden" id="neo_string" value="{% autoescape true %}{{ page.all_sections() }}{% endautoescape %}" />
<pre id="neo_formatted"></pre>
<script>
const updateIt =
=> { neo_formatted.innerText = JSON.stringify
JSON.parse ,
null,
2
)
}
document.addEventListener
</script>
Notes
This goes in a template that receives data in a `page`` context variable
The data is placed in a hidden input element to start with then scrubbed and output in the `pre`` tag when the page is loaded
I have optional values that come across in the origial data as `None`` without quotes. The script adds the qutoes to diminish parsing issues