amp-html: Use query param in amp-list src - html

I have an amp page that is called on:
https://expample.amp.com?id=1234abc
(Not the real url.)
<amp-list src="https://some.external-url.com/?dataid=NEEDS_ID"
layout="responsive"
items="Result"
width="100"
height="100"
>
<template type="amp-mustache">
<p>{{FullName}}</p>
</template>
</amp-list>
I want my id query param to be injected into the src of the amp-list tag at NEEDS_ID.
I already checked the QUERY_PARAM docs but couldn't understand what exactly I'm supposed to do.
I've already tried src="https://some.external-url.com/?dataid=QUERY_PARAM(id)" but that didn't work.
Any pointers/references?
My silly-and-dumb solution to this was writing an EJS node server, that parses a <%= query.id %> in that url and then sends that page to the client. Is that the correct way to do it?

I do strongly suppose that the call to the Url independent to the AMP Page, results in valid json data.
If this is the case, you may face following problems:
CORS
your result data is somehow corrputed
As for me, this saved me a lot of time:
public function getData(){
echo header('Access-Control-Allow-Origin: https://amp-YOUR-SITE');
echo header('Access-Control-Allow-Credentials: true');
echo json_data;
}

https://expample.amp.com?slug=1234abc
<amp-list id="time"
layout="fixed-height"
height="18"
src="https://api.exemple.com/posts?slug=QUERY_PARAM(slug)"
binding="refresh"
data-amp-replace="QUERY_PARAM"
single-item
items=".">
<template type="amp-mustache">
<p>{{title}}</p>
<p>{{content}}</p>
</template>
</amp-list>

Related

How can I automatically bind amp state (from remote source)?

My amp page has a state as follows:
<amp-state id="remoteData" src="https://remoteDataurl.com">
<script type="application/json">
{
"name": "Foo",
"email": "foo#bar.com"
}
</script>
</amp-state>
I would like to bind the response to an element in the page:
<p [text]="'Hello ' + remoteData.name"> ... </p>
<p [text]="'Your email is ' + remoteData.email"> ... </p>
The text is actually already bound, but it will not refresh until an event occurs on the page.
How can I get it to refresh the state automatically?
I believe <amp-state> does not give you this functionality.
My solution for now is to use <amp-list> with the following attributes.
<amp-list
layout="fixed-height"
height="80"
src="https://remoteDataurl.com"
single-item
items="."
>
<template type="amp-mustache">
<p> Hello {{ name }} </p>
<p> Your email is {{ email }} </p>
</template>
</amp-list>
You can do it that way.
<amp-state
id = "product"
src="https://www.alectrico.cl/listas/designer&productos.json">
</amp-state>
The state "product" now it's linked to some url from outside.

amp-list not printing returned data from the connected JSON file

I'm trying to implement amp-list to allow a different currency depending on where the user is from. I've implemented the amp-list element and created a JSON file (which is a CORS url), containing the data using the correct syntax.
The amp-list however is not printing the data, and instead printing a blank space. The HTML template is:
<amp-list width="auto"
height="50"
layout="fixed-height"
src="/amp-price/57938">
<template type="amp-mustache">
<p class="price align-center {{test}}">{{price}}</p>
</template>
</amp-list>
And the JSON response is:
{"items": [{"price": "\u00a321.59", "test": "test"}]}
But the rendered HTML is:
<amp-list width="auto" height="50" layout="fixed-height" src="/amp-price/57938" class="i-amphtml-element i-amphtml-layout-fixed-height i-amphtml-layout-size-defined i-amphtml-layout" aria-live="polite" style="height: 50px;">
<template type="amp-mustache">
<p class="price"> - </p>
</template>
<div class="i-amphtml-fill-content i-amphtml-replaced-content" role="list"></div></amp-list>
The JSON response has all the correct AMP headers, and I'm not getting any AMP errors in the console.
I've also followed the src link in the page source and it goes to the correct URL.
Is there something simple I'm missing?
I had the same problem and I tried almost everything. The amp-list in safari 10 doesn't load the json endpoint.
But the solution for me is to add credentials="include" into my amp-list tag:
the final amp-list:
<amp-list credentials="include" id="smallcartsList" width="auto" height="#HeightRequest" [height]="CurrentCartList.items.length == 0 ? 5 : 50 * CurrentCartList.items.length" media="(max-width: 1199px)" layout="fixed-height" binding="no" src="/API/CartList/GetCartsList" [src]="cartlistsource" template="cartlistdetail"> </amp-list>
Sebastian Benz is correct I have use the same code and working fine
Here is working url
HEAD JS
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>
BODY HTML
<amp-list width="auto"
height="50"
layout="fixed-height"
src="https://jsonblob.com/api/jsonBlob/1f6f838d-25aa-11e8-8863-d99090d9ec78">
<template type="amp-mustache">
<p class="price align-center {{test}}">{{price}}</p>
</template>
</amp-list>
JSON DATA LINK

how to deal with ng-src with invalid url in ng-repeat

I would like to show the picture of a user if there is a user in my object list (profileList), and default/error as defaultProfile.png when no user is found ({{item.userProfile}} is null)
I have searched for similar approaches such as
angularjs: ng-src equivalent for background-image:url(…)
and
empty ng-src doesn't update image
My approach to this problem is:
<div ng-repeat="item in profileList">
<div>
<img src='assets/img/defaultProfile.png' data-ng-src="http://example.com/{{item.userProfile}}.jpg" onerror="this.src='assets/img/defaultProfile.png'" />
</div>
<div>
I am able to show error photo however I am still getting error 500,
GET http://example.com/.jpg 500 (INTERNAL SERVER ERROR)
How to avoid getting http://example.com//.jpg?
Thanks a lot
Your current issue is ng-src is still compiled and evaluated to an invalid url when userProfile is undefined.
A simple solution is to use ternary and check for userProfile before deciding with url should be rendered:
<img ng-src="{{ item.userProfile ? 'http://example.com/' + item.userProfile + '.jpg'}} : 'assets/img/defaultProfile.png'" />
It will guarantee that you will always fetch the default image unless item.userProfile is available.
One approach is to use ng-if and ng-hide:
<div ng-repeat="item in profileList">
<div>
<img ng-if="item.userProfile"
data-ng-src="http://example.com/{{item.userProfile}}.jpg" />
<img ng-hide="item.userProfile"
src="assets/img/defaultProfile.png" />
</div>
<div>
When item.userProfile exists, show the ng-src and hide the default otherwise vice versa.
It works.
ng-show
will run no matter {{item.userProfile}} is null or not.
By changing it to
ng-if
Below code is working:
<div ng-repeat="item in profileList">
<div>
<img ng-if="item.userProfile"
data-ng-src="http://example.com/{{item.userProfile}}.jpg" />
<img ng-if="item.userProfile == null"
src="assets/img/defaultProfile.png" />
</div>
note that my profileList is:
$scope.profileList = {userProfile = null}
Thanks a lot

Why is HTML I put in a Mongodb document as JSON displaying as text on the webpage?

I have a mongo database with a document that contains some html in it, and when I try to take it and put it on a webpage, it just displays as the actual text and not the html. Here is the json with the html:
db.games.insert({
title: "Minecraft",
background: "/images/minecraft.jpg",
code: "<div id=\"gameBackround\" class=\"col-lg-2 popular-games view view-first\" style=\"background-image:url( /images/gameArt/minecraft.jpg )\"> <div class=\"mask\"> <h2>Minecraft</h2> <p>Amount of groups playing this title now: 11,075</p> Join Lobby </div> </div> <style> </style>"
})
and here is how I display it on the page:
<template name="example">
{{code}}
</template>
Use should use triple curly braces to escape html code returned from a helper.
<template name="example">
{{{code}}}
</template>
Here is an example.

How to output just html image tag when I have text and html in django template output?

I am using this to output,
{{ movie.img }}
and I get the output is supposed to be something like,
u'<img src="//upload.wikimedia.org/wikipedia/en/thumb/8/8a/Dark_Knight.jpg/220px-Dark_Knight.jpg" alt="" height="327" width="220" >\nTheatrical release poster'
How do I just output the html image part? I don't want the Theatrical release poster to appear in the output.
Since you are getting that as just text, your best solution would be to write a template filter that would strip content not in the <img> html tag.
If the object were a ImageField (or FileField), you can call on the url attribute only, {{ movie.img.url }}
update
Ok, here's a basic, probably too naive template filter for your use.
from django import template
from django.template.defaultfilters import stringfilter
import re
register = template.Library()
#register.filter(is_safe=True)
#stringfilter
def get_img_tag(value):
result = re.search("<.*?>", value)
if result:
return result.group()
return value
Use:
{{ movie.img|get_img_tag|safe }}