Is it possible to insert html dependent on user's cookie in the AMP page?

427 Views Asked by At

We want to dynamically insert HTML in our web page. We are using mustache to add the dynamic content. The content is dynamic and conditional. To handle conditional logic, we are using mustache i.e.

json: {
    name: "James",
    isJames: true
}

Then in the template you can have:

{{#isJames}}
    //insert HTML for James
{{/isJames}}

{{^isJames}}
    //insert HTML for NOT James
{{/isJames}}

We have different HTML template for the above conditions saved in our database. Also, the HTML returned depends on the user's cookie. Is there any way by which we can directly insert HTML returned from the server in our AMP page?

1

There are 1 best solutions below

0
On

I assume that you are using mustache inside amp-list. You can insert HTML using triple-mustache. I recommend using the following format:

json: {
    flag: true,
    content: <your html here>,
}

And in mustache:

{{#flag}}
    {{{content}}}
{{/flag}}

Regarding cookies, you have no access over cookies while in AMP page, however if the amp-list is calling the same domain as the one which set the cookies, the browser will automatically send them in the request header. This way you can access them in your server to generate dynamic HTML. Although for user identification AMP recommends using 'Reader Id' (see AMP-access)