GoLang Issues displaying JSON results on HTML template - json

Context:
Here is Struct:
type ReverseWhoisResponse struct {
DomainsCount int `json:"domainsCount"`
DomainsList []string `json:"domainsList"`
}
After doing everything that needs to be done, this is served to via:
c.HTML(200, "example.html", gin.H{
"total": ReverseWhoisResponse.DomainsCount,
"list": ReverseWhoisResponse.DomainsList,
})
The first part, of displaying raw JSON works just by
<pre>
<span class="inner-pre">
{{ .list}}
</span>
</pre>
Issue:
However, now i'm trying to display a parsed field as well - so that i can view both raw JSON in one tab, and parsed results in another. This is how i'm attempting to do it:
<ul>
{{range .DomainsList }}
<li>
<span>{{ .list }}</span>
</li>
{{end}}
</ul>
This is because it returns a list of URLs
E.G
example1.com
example2.com
example3.com
I'm trying to parse it so it looks decent on screen so i can actually utilize it.
At the moment, i'm not getting any errors - but i'm seeing the RAW Json in the tab, but not seeing anything in Parsed tab, any ideas what's going on or some documentation i can follow for this?

The issue was outlined by #spencerconnaughton.
My issue was that i was misusing ranges.
instead of:
<ul>
{{range .DomainsList }}
<li>
<span>{{ .list }}</span>
</li>
{{end}}
</ul>
It should have been:
<ul>
{{range .list }}
<li>
<span>{{ . }}</span>
</li>
{{end}}
</ul>

Can you share the JSON you're using as input? If {{.list}} displays raw JSON, that suggests that ReverseWhoisResponse.DomainsList contains raw JSON, which suggests it isn't being parsed as you expect. Assuming your second template code block is the parsed tab, and that's receiving the the same "total" and "list" parameters as the raw JSON block, are you sure it would be passed .DomainsList and not just {.list}? Also, in the range function, do you mean to use {{.}} instead of {{.list}}? Disclaimer: I have no experience with gin, so I'm guessing on some of this stuff. :)

Related

Sample code containing expression like {{ $title }} cause blank page in Laravel

I built a blog in Laravel to post some programming tutorials, but I faced a problem; when I have some expression (sample code) like below
#foreach($items as $item)
<div>{{ $item->name }}</div>
#endforeach
or if I have Angular sample code
<div *ngFor="let item of items">{{ item.name }}</div>
the above sample codes cause a blank page,
in the console, there is no error, but in the development tool element tab body tag is empty. look at this link https://coffeequery.com/posts/loop-in-angular I looked everywhere to find a solution but couldn't.
I think the browser trying to evaluate this expression, what should I do to tell the browser to ignore it?
thanks.
The problem is in the following code:
<code><div *ngFor="let item of items">#{{item.name}}</div></code>
Your Vue.js instance tries to evaluate the curly brackets and silently throws an error, which results in displaying empty #app element. I suggest using v-text attribute instead:
<code v-text="'<div *ngFor="let item of items">{{item.name}}</div>'"></code>

Handlebars to show one value or another depending on URL tag?

In my code, I'm calling the data being passed below in Handlebars.js
<section class="inner-wrapper">
{{#each data.header }}
<h1>{{title}}</h1>
{{/each}}
</section>
The value of title will either be "Results" or "Look" (stored in the json file) depending on the tag in the URL. If the URL is "websitename.com", it should show the "Results" title. If the URL is "websitename.com?tag=LO", it should show the "Look" title.
How do I write the handlebars template code to render one title or the other depending on the URLs above?

Reusable HTML component libraries in Django

I have a Django website with many pages all with their own unique content and markup (as opposed to a single template that is dynamically filled with data). These pages' templates are derived from a common "tool" template, which in turn derives from a "base" template. Each page also has a JS file which does the scripting for that page. Each pages data is spread around 3 or 4 locations:
the template
the page script (and maybe other page-specific statics)
a central "tool registry" which contains thing like the tool name, description and categories
for some tools, a set of template tags to do some HTML construction specific to that page (e.g. data to present in a table).
However, although each page has a unique layout and content, I still want to be able to share commonly used parametrised HTML "snippets" between pages. For example, one page has an input with a dropdown button that, using Bootstrap, looks like this:
<div class="input-group">
<span class="input-group-btn">
<a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
Common baudrates
<span class="caret"></span>
</a>
<ul class="dropdown-menu pull-right" id="common-baudrate-list">
<li><a href='#'>9600</a></li>
<li><a href='#'>115200</a></li>
</ul>
</span>
<input class="form-control" type="text" id="baudrate-input" val="115200">
</div>
I'd like to make a re-uasable parametrised function/template/tag which allows me to put in a structure like this given something like "group ID", "list of entries", "left/right button position", "default value".
The ways I see to do it so far are:
As a template, which has a fairly limited syntax, and make it hard to do something like a list of entries like this: ['Entry 1', 'Entry 2']
As a template, passing data into the context via the view (doesn't really appeal because I'd be spreading the pages contents around even more).
As a template tag, building the HTML as a big nasty string
As a template tag using an XML library like lxml, which is a pretty flexible method, but suffers from verbose syntax, over-complexity and I still have get data into the tag from the template!
None of these seem like a neat, re-usable and loosely coupled way to deal with this (for example, if I change up to Bootstrap 4 in future, I may need to re-write these components, and I'd rather have to do it just once). Having a library of components like this will also make future pages easier to construct. Is there a canonical "right" way to do this, or even a commonly used idiom for it?
Edit to show solution implementation
Using inclusion tags as answered below by Wolph, I avoided using a clunky construction like this
{% with [1,2,3] as items %}
{% my_tag items.split %}
{% endwith %}
as follows:
#register.inclusion_tag('components/this_component.html')
def input_with_dropdown(dropdown_title, dropdown_items, default_value, group_id, button_left, *args, **kwargs):
split_char = kwargs['split_char'] if 'split_char' in kwargs else ' '
return {
'dropdown_items': dropdown_items.split(split_char),
'dropdown_title': dropdown_title,
'group_id': group_id,
'default_value' : default_value,
'button_left': button_left
}
And then passing in the variables like this:
{% input_with_dropdown 'Common baudrates' '9600 48000 115200' 115200 'baudrate-input' 0 %}
Have you taken a look at inclusion tags? https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags
I believe that these would do exactly what you want, you can still specify parameters but you can just pass along a template to have it render really easily.
Example:
from django import template
register = template.Library()
#register.inclusion_tag('list_results.html')
def list_results(results):
return {
'results': results,
'count': len(results),
}
list_results.html:
Found {{ count }} results.<br>
<ul>
{% for result in results %}
<li>{{ result }}</li>
{% endfor %}
</ul>
Use templatetags and html template... Then you can reuse that HTML template...

Octopress HTML Includes with Arguments

I would like to include HTML snippets within blog posts and have the code in a single place. After perusing the framework I've been able to get this to work:
source
_includes
custom
foo.html
{% include custom/foo.html %}
My question: Is there a way that I can pass arguments to foo.html? I simply want to pass some strings in and output them in a formatted way and cannot figure out what I'm missing.
Thanks for your time.
You can't. But you can use some other way likes what theme does to do this.
First, set formatted style you want in custom/foo.html, for example, I add a customfooter in source/_includes/custom/footer.html:
<p>
Copyright © {{ site.time | date: "%Y" }} - {{ site.author }} -
<span class="credit">Powered by Octopress</span>
{{ site:customfooter }}
</p>
Then, set the string you want to set in _config.yml:
url: http://fann.im
customfooter: My custom footer, bala bala.
Hope this will help you.
There's no way to do this because Jekyll is static. By definition anything you could pass from one file to another file is a known value, because it must be defined at the time of site generation. Since you can't pass dynamic values, this sort of indirection doesn't make sense because you can just put the static value where it is going to be anyway.
If you have the use case of e.g. generating 10 chunks of output that are mostly similar but differ slightly, then use a plugin to accomplish the task of isolating those changes using variables. This gives you some flexibility while still putting the value into the template where it will be used.
Here's an example of a liquid tag that abstracts generating twitter bootstrap nav tabs with specific hrefs assigned.
module Jekyll
class XmlJsonTabsTag < Liquid::Tag
def initialize(tag_name, markup, tokens)
#methodname = markup.strip
super
end
def render(context)
#wondering what this syntax is? google "here document"
<<-HTML
<ul class="nav nav-tabs">
<li class="active">
JSON
</li>
<li>
XML
</li>
</ul>
HTML
end
end
end
Liquid::Template.register_tag('xmljsontabs', Jekyll::XmlJsonTabsTag)
Pass the value to use for #methodname in the liquid tag in the template:
{% xmljsontabs foo %}
Which outputs something like
<ul class="nav nav-tabs">
<li class="active">
JSON
</li>
<li>
XML
</li>
</ul>

symfony2 twig whitelist html tags

I pass a variable to my twig template in Symfony2, this variable may contain <br /> html tags, I have tried to create an extension (function), but the variable still gets escaped.
How can I output a twig variable that allows the <br /> tag? Is there a simple solution to just allow a whitelist of allowed tags in certain templates?
I've searched about twig sandboxes, but I'm not sure if that is my solution.
edit: I still want the variable to be escaped, but to allow exclusively the <br /> tag.
Actually, you can use native PHP function strip_tags by following:
{{ var|striptags('<br>')|raw }}
you can allow multiple tags with following code:
{{ var|striptags('<br><p>')|raw }}
You can do like that :
{{ text | striptags('<p><b><br') | raw }}
For instance,
<br>
won't escape
<br> and <br />
and
<p>
won't escape
<p> and </p>
etc.
Initially I thought it should be possible to write custom escaper strategies so you could do something like this:
{{ var|escape('html-custom') }}
Unfortunately it's not the case. Only available strategies are html and js. They're hard coded in the twig_escape_filter() function defined in a Twig_Extension_Core class file.
It seems that your only option is to write custom estension with a new filter:
{{ var|raw|customescape }}
Here's an example of custom twig extension and how to register it in Symfony: Symfony2 Twig extension
{{ var|striptags('<br>')|raw }}
works fine, but I don't know how to pass an array to the strip_tags php function with this twig filter.
both
{{ var|striptags(['<br>', '<b>'])|raw }}
and
{% set allow = ['<br>', '<b>'] %}
{{ var|striptags(allow)|raw }}
throw an "Array to string conversion" exception during the rendering of a template.
Be also carefull that strip_tags php function doesn't escape html attribute like "onclick".
{{ var|nl2br }}
and/or
{{ var|raw|nl2br }}
nl2br reference