SOLVED: Pretty print JSON using Golang templates - html

I am making a website using go templates. I need to print a variable containing JSON data to the screen. I'd like to pretty print it to the screen and haven't had any luck with that.
In flask I am able to do the following to accomplish this decently well:
{% autoescape false %} {{stringwithjsondata | safe | replace(",", "<br>")}} {% endautoescape %}
and was wondering if I could do something similar here.
I'm using the html/template package for the website.
EDIT: I over complicated this to eternity, I just used <pre> tags in the end with strings.replace to make it look good

Related

Can I generate a references index/ book index from markdown to html (ideally in a static site)?

For an academic project, I would like to make an index. You know, that boring list of words that indicates in which pages every word listed is. This : https://www.pdfindexgenerator.com/what-is-a-book-index/. But for a website.
My goal is, let's say, from Markdown, to generate HTML pages. I would love to do this with a static site, because the content won't evolve every day, and it appears to me that I'd have to parse all the content anyway. Maybe the solution is just using a wiki.
Here's how I would have done it : you write a bunch of text into page.md, inside this text you identify a [word] that you want to see in your index with a specific markup. And then you mention this same [word] with the same markup into otherpage.md.
Then, the generator extracts all the marked words, makes a list, and generates a page with the links to all the references to each marked word.
Word:
page.html
otherpage.html
A reference index. Yay.
What I want is like a simpler version of LaTeX's MakeIndex. Like, closer to this https://wordpress.org/plugins/lexicographer/, but not for definitions, only for internal references.
Pandoc seems to not be supporting indices, maybe because MakeIndex is very complex (but indices are actually, so well, that's fair play) or just because it's made for page numbers and not html links.
So :
I know indices are actually complicated stuff. It's impossible to fully
automatize. My only goal here is to be able to tag the words as I
write and having some computer help to make the listing at the end and render a neat HTML page
with all the links because this part is really boring (like MakeIndex does). But maybe just this
part is impossible and I'd be fine with this.
Is this already implemented somewhere, if it's not impossible? There is plenty of static sites and wikis and stuff, maybe someone thought about it before me, as indices are academic stuff used for CENTURIES. Maybe there's a plugin or a piece of software I just don't know.
I would appreciate just pointers to know where to go to do this if its doable. There is a start here How to generate (book) indexes? but it's too little for me to understand what to do next.
Thanks a lot <3
Using Pelican you can use tags that way.
You can add the following to your index.html template to loop through existing tags :
{% if tags %}
{% for tag, articles in tags %}
{{tag }} :
<ul>
{%for article in articles%}
<li>{{ article.title }}</li>
{% endfor %}
</ul>
{% endfor %}
{% endif %}
Then you will get the following result :
You can't directly tag your text the way you showed though. You'll have to add the tag line in your article's headings :
Title: mytitle
Date: 2020-05-19
Tags: firsttag, othertag
...
You can add this to your index.html template or to tags.html, as you see fit.

Prevent Liquid from parsing of certain files in Jekyll

I am using Jekyll + Liquid + Markdown to generate static html pages. Consequently, this is really a question specific to the Jekyll framework and Liquid template generator because there is syntax that conflicts with Liquid in some of my Markdown files.
Is there a quick and dirty work-around I can use for the time being in order to prevent Liquid from parsing certain files?
Use Raw:
Raw temporarily disables tag processing. This is useful for generating content (eg, Mustache, Handlebars) which uses conflicting syntax.
{% raw %}
In Handlebars, {{ this }} will be HTML-escaped, but
{{{ that }}} will not.
{% endraw %}

Adding a variable in a Liquid template for multilingual?

I have a Jekyll project and I'm trying to implement multilingual functionality.
My data files were stored in _data/my_data.yml, and accessed in the templates via the {% for data in site.data.my_data %} Liquid template tag.
I have now copied and translated my data files into _data/en/my_data.yml and _data/it/my_data.yml and created two .md files, using the same template, with respective lang attributes in the front matter.
I am trying to dynamically access the correct data based on this lang attribute however it's throwing all sorts of errors.
Things I've tried
{% for data in site.data.[page.lang].my_data %}
{% for data in site.data[page.lang]my_data %}
{% for data in site.data.{{page.lang}}.my_data %}
Any ideas?
In case anyone runs into this in the future, solved it with pretty much the only combination I hadn't tried yet:
{% for data in site.data[page.lang].my_data %}

Is it possible in gh-pages Jekyll to have {{content}} expand without evaluating liquid tags?

I'm trying to use GitHub Pages for my project's documentation, but it includes generated html files that turn out to have illegal liquid tags. I don't need any expansion beyond the _layout itself, but as far as I can tell, any {% ... %} tags in the articles' content themselves are also evaluated and there seems to be no way to suppress this, other than adding {% raw %}...{% endraw %} around the entire contents of every single article.
Is there any way to do this at the call site? Something along the lines of {{ content | unrendered }} would be excellent.
Note: this seems to be the opposite problem to many others, who are using page.content in a pre-render context and wanting it to be rendered; I've tried page.content but as far as I can tell it's exactly the same in my case, so no dice.
page.content was raw in the jekyll 2.x era. Now its rendered content.
You can use a hook plugin to add a page.raw field on any page.
Jekyll::Hooks.register :pages, :pre_render do |document|
document.data['raw'] = document.content
end
If you want to do the same on posts and collections items, use a documents hook :
Jekyll::Hooks.register :documents, :pre_render do |document|
Note :
In :pre_render hooks document.content contains raw content
In :post_render hooks document.content contains rendered content

How do I reuse HTML snippets in a django view

I am working on a django project (my first), and in one of my views, I have a sophisticated html snippet with JS weaved within it. I would like to reuse this "component" somewhere else in the same view. Is there a way of achieving this? Please let me know if this design is faulty to begin with?
Use the {% include '/my/common/template.html' %} templatetag.
Loads a template and renders it with
the current context. This is a way of
"including" other templates within a
template.
The template name can either be a
variable or a hard-coded (quoted)
string, in either single or double
quotes.
I know it's an old one but maybe someone is gonna have use of this answer.
There's also the inclusion tag. It's like the include tag, only you can pass it arguments and process it as a seperate template.
Put this in my_app/templatetags/my_templatetags.py:
#register.inclusion_tag('my_snippet.html')
def my_snippet(url, title):
return {'url': url, 'title': title}
and then my_snippet.html can be:
{{ title }}
then, to use this snippet in your templates:
{% load my_templatetags %}
{% my_snippet "/homepage/" "Homepage" %}
More info:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags-inclusion-tags
Not sure, if you like to reuse your HTML in different templates (rendered by different views). If so, look into Django's template inheritance mechanism:
The most powerful -- and thus the most complex -- part of Django's template engine is template inheritance. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override.
You should try Django custom template tags. This way you will keep your snippets in an external file and then call them easily by something like {{ your_custom_tag }}. It's a very convenient method for working with reusable chunks of xhtml markup. You can even use arguments with these custom tags, something like {{ your_custom_tag|image:"logo.png" }}.
You can learn more about custom tags here.