Data files with html content - eleventy

I'm making a nice simple html page using external data... here's the bit that's causing me grief:
<section class="faq_main">
{% for section in faq.sections %}
<h3>{{ section.heading }}</h3>
{% for question in section.questions %}
<article class="faq_question">
<a id="hide_{{ section.code }}_{{ question.code }}"
href="#hide_{{ section.code }}_{{ question.code }}"
class="hide"><span class="faq_toggler">+</span> {{ question.question }}</a>
<a id="show_{{ section.code }}_{{ question.code }}"
href="#show_{{ section.code }}_{{ question.code }}"
class="show"><span class="faq_toggler">-</span> {{ question.question }}</a>
<div class="details">
{{ question.answer }}
</div>
</article>
{% endfor %}
<p> </p>
{% endfor %}
</section>
... and the matching faq.json file:
{
"sections" : [
{ "heading" : "Section the first",
"code" : "gn",
"questions" : [
{
"question": "What do questions need?",
"code" : "1",
"answer": "An answer"
},
{
"question": "Is this also a question?",
"code" : "2",
"answer": "Yes, it is"
},
{
"question": "Is this junk data?",
"code" : "a",
"answer": "Yes"
},
]
},
{
"heading": "Another section",
"code" : "f",
"questions": [
{
"question": "Can I have html in my answer?",
"code" : "2",
"answer": "<ul>\n<li>First, json needs newlines escaped to be newlines</li>\\n<li>Eleventy seems to 'sanitize' the string</li>\\n</ul>"
},
{
"question": "question b",
"code" : "b",
"answer": "answer b"
},
{
"question": "question c",
"code" : "or",
"answer": "answer c"
},
]
}
]
}
.... and the rendered text for the answer in question is:
<ul> <li>First, json needs newlines escaped to be newlines</li>\n<li>Eleventy seems to 'sanitize' the string</li></ul>
Do I have any options here? Is there a way to allow [even a subset of] html elements into the page?
(and yes, the CSS does the clever show/hide descriptions using the '+'/'-' symbols - all that side of things works just lovely)

It should work by inserting the symbols directly into the quotes in the json. Use Non-breaking spaces for indentation, and unicode points for dots.
Otherwise, the framework is broken.

Change {{ question.answer }} to {{ question.answer | safe }}
(see https://www.11ty.dev/docs/layouts/#prevent-double-escaping-in-layouts - it's clear, once you understand what it's saying :) )

Related

How to add dynamic classes to looped elements in angular

I have a looped list. I have to add class active when any block is clicked. I am not sure how to do it using [ngClass]. Please help me.
It is HTML code:
<div *ngFor="let cell of myData">
<div class="list-header">
<label>{{ cell.name }}</label>
</div>
<div class="list-group">
<a class="list-group-item list-group-item-action d-flex" *ngFor="let unit of cell.array" (click)= "onClick()" [ngClass]="{'active': this.active}">
<label>{{ unit }}</label>
</a>
</div>
</div>
my TS code:
myData = [
{
'name': 'abc',
'array': ["asass","From Mac","New", "test 1", "test 10", "test 2", "test 3", "test 4", "test 5", "test 6", "test 7", "test 8", "test 9" ]
},
{
'name': 'all types and options',
'array': ['Camera','del TYPE','Fan','hardware','icons','mobile','new asset type']
},
{
'name': 'am cat',
'array': ['am type','camera','new 423423']
},
{
'name': 'cat with no asset types, dec added',
'array': ['camera']
},
{
'name': 'cat with one asset type',
'array': ['camera']
},
{
'name': 'colors',
'array': ['pink', 'yellow']
}
];
<div class="my_class" (click)="clickEvent()"
[ngClass]="active ? 'success' : 'danger'">
Some content
</div>
active: boolean = false;
clickEvent(){
this.active = !this.active;
}
template:
<div *ngFor="let cell of myData">
<div class="list-header">
<label>{{ cell.name }}</label>
</div>
<div class="list-group">
<a class="list-group-item list-group-item-action d-flex" *ngFor="let unit of cell.array; let i = index" (click)= "cell.selectedIndex=i" [ngClass]= "{ 'active': cell.selectedIndex === i }" >
<label>{{ unit }}</label>
</a>
</div>
</div>

How to Parse Custom (without Key) response in AMP-autosuggest (Rails 5)

Anyone tells me how can I parse my response in AMP-list. In AMP didn't find any solution how to parse own custom API response.
My Code.
<div
class="autosuggest-container hidden"
[class]="(showDropdown && query) ?
'autosuggest-container' :
'autosuggest-container hidden'"
>
<amp-list
class="autosuggest-box"
layout="fixed-height"
height="120"
src="http://lmyadpi.com/api.json?q=a"
id="autosuggest-list"
>
<template type="amp-mustache">
<amp-selector
id="autosuggest-selector"
keyboard-select-mode="focus"
layout="container"
on="
select:
AMP.setState({
query: event.targetOption,
showDropdown: false
}),
autosuggest-list.hide"
>
{{#results}}
<div
class="select-option no-outline"
role="option"
tabindex="0"
on="tap:autosuggest-list.hide"
option="{{.}}"
>{{.}}</div>
{{/results}}
{{^results}}
<div class="select-option {{#query}}empty{{/query}}">
{{#query}}Sorry! We don't ship to your city 😰{{/query}}
</div>
{{/results}}
</amp-selector>
</template>
</amp-list>
</div>
</div>
<amp-state
id="allLocations"
src="http://lmyadpi.com/api.json?q=a"
"emptyAndInitialTemplateJson": [{
"query": "",
[[]]
}]
></amp-state>
My API response :
[[
"1234",
"value2",
""
],
[
"6321123",
"value3",
""
],
[
"43934322",
"value4",
""
],
[
"43660213",
"value5",
""
],
[
"54373228",
"value6",
""
],
[
"410721327",
"value7",
""
]]

AngularJS: service $http.get JSON file

I am very new to AngularJS. I have read several posts on this topic, but am still unable to get data from external JSON file.
here is my code
data.json
[
{
"id": 1,
"date": "20th August 2016",
"day": "Sat",
"in1": "10:30",
"out1": "02:30",
"in2": "04:00",
"out2": "08:00",
"in3": "",
"out3": "",
"total_hours": "8:00",
"status": "1"
},
{
"id": 2,
"date": "20th August 2016",
"day": "Sat",
"in1": "10:30",
"out1": "02:30",
"in2": "04:00",
"out2": "08:00",
"in3": "",
"out3": "",
"total_hours": "8:00",
"status": "1"
}
];
html
<table ng-table="tctrl.tableEdit" class="table table-striped table-vmiddle">
<tr ng-repeat="w in $data" ng-class="{ 'active': w.$edit }">
<td data-title="'ID'">
<span ng-if="!w.$edit">{{ w.id }}</span>
<div ng-if="w.$edit"><input class="form-control" type="text" ng-model="w.id" /></div>
</td>
...
</tr>
</table>
controller.js
.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) {
var data = tableService.data;
//Editable
this.tableEdit = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: data.length, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
})
service.js
.service('tableService', [function($http){
this.data = function() {
return $http.get('data/timesheet.json');
}
}])
I'm just trying to pull data from json file but it's not working and getting blank table. I don't know what is the issue. What I will get in this.data?

Example, how to use huge json in erlyDTL?

I have json
{
"_total": 824,
"_links": "self",
"top": [
{
"viewers": 80896,
"channels": 1177,
"game": {
"name": "League of Legends",
"_id": 21779,
}
},
{
"viewers": 31211,
"channels": 232,
"game": {
"name": "Dota 2",
"_id": 29595,
}
}
]
}
how best displayed "top" list? I try:
Tuple = jsx:decode(unicode:characters_to_binary(Json)),
[_, _, Top] = Tuple,
Games = element(2, Top);
but how using this in template?
{% for v in games %}
{{ v.viewers }}<br><br>
{{ v.channel }}<br><br>
{{ v.game.name }}<br><br>
{% endfor %}
not work, and then show {{ games.game.name }} ?
The code you did is fine, it just need a few updates, you shouldn't pass Games to the template, instead you should use top as the key, as an example, build a rebar file like:
%rebar.config
{deps, [
{erlydtl, ".*", {git, "git#github.com:erlydtl/erlydtl.git", "HEAD"}},
{jsx, ".*", {git, "git#github.com:talentdeficit/jsx.git", "HEAD"}}
]}.
then run:
rebar get-deps
rebar compile
Now define a template with the suggested update, named template.dtl:
{% for v in top %}
{{ v.viewers }}<br><br>
{{ v.channels }}<br><br>
{{ v.game.name }}<br><br>
{% endfor %}
Start an erlang shell:
erl -pa ./deps/erlydtl/ebin/ -pa ./deps/merl/ebin/ -pa ./deps/jsx/ebin/
and execute the commands which should display the template as expected
Json = "{
\"_total\": 824,
\"_links\": \"self\",
\"top\": [
{
\"viewers\": 80896,
\"channels\": 1177,
\"game\": {
\"name\": \"League of Legends\",
\"_id\": 21779,
}
},
{
\"viewers\": 31211,
\"channels\": 232,
\"game\": {
\"name\": \"Dota 2\",
\"_id\": 29595,
}
}
]
}".
Tuple = jsx:decode(unicode:characters_to_binary(Json)).
erlydtl:compile("./template.dtl", test).
test:render(Tuple).
which gives the expecte output
{ok,[[[<<"\n ">>,"80896",<<"<br><br>\n ">>,"1177",
<<"<br><br>\n\t">>,<<"League of Legends">>,<<"<br><br>\n">>],
[<<"\n ">>,"31211",<<"<br><br>\n ">>,"232",
<<"<br><br>\n\t">>,<<"Dota 2">>,<<"<br><br>\n">>]],
<<"\n">>]}
in few words, in your template, replace games with top and pass Tuple to the template

Displaying an object with Ruby on Rails

I have some JSON that looks like this. I have it stored and read into an object, #items.
[
{
{
"id": "A",
"description": "a_description"
},
{
"id": "B",
"description": "b_description"
}
},
{
{
"id": "A",
"description": "a_description"
},
{
"id": "B",
"description": "b_description"
}
}
]
My goal is to display a table with two columns, one labeled A and the other labeled B, in which each row gives the "a_description" and "b_description". I'm not sure how to go about doing this.
Ah, the ol' array of hashes and hashes of arrays problem.
To get around your "out of order" problem you first have to convert
{
"id": "A",
"description": "foo"
},
{
"id": "B",
"description": "bar"
}
into {"A" : "foo", "B" : "bar" }.
#new_items = #items.map do |item|
output = {}
item.each do |hash|
output.merge!(hash["id"] => hash["description"])
end
end
Then #new_items becomes (intentionally presented out of order since hash elements are not ordered)
[
{
"A": "a1_description",
"B": "b1_description"
},
{
"B": "b2_description",
"A": "a2_description"
}
]
From there, each line is simply a hash, so you can just dereference the value you need based on the column you're in.
#new_items.each do |item|
puts "#{item['A']} is paired with #{item['B']}"
end
Keys, of course could be retrieved dynamically if you don't want to hard code "A" and "B" using .keys
Something like this maybe
<tr><th>A</th><th>B</th></tr>
<% #items.each do |item| %>
<tr><td><%=item[0].description%></td><td><%=item[1].description%></td></tr>
<% end %>

Resources