How do I check if data exists in node express app - node.js

am working on a node / express app and currently trying to check if some session data exists or not.
To call the session data I'm currently using {{ if data['username'] }}
which works fine. To user a conditional if the data exists the following seems to work:
{% if data['username'] %}
do this
{% endif %}
However I'm not sure how to do something if it DOESN'T exist. Such as
{% if data['username'] != '' %}
Any ideas appreciated! Thanks

I'm assuming you're using Nunjucks here based on the templating, and with that you can do:
{% if not data['username'] %}
{% endif %}

It depends on the templating language you’re using.
If it’s Twig you can use not or empty
{% if not data['username'] or data['username'] is empty %}
Do this
{% endif %}

Related

Dump(item) not working in Twig template drupal

I have twig template for default view
{% for item in items %}
{{ item.content }}
{% endfor %}
{{ item.content }} have values but when I tried to print the whole array like
{% for item in items %}
{{ item }}
{% endfor %}
It is not working. I have enabled (service.yml)
debug true
cache false
autoload true
but it's not working. Any help would be appreciated
Try and install the Devel plugin with its Kint extension. Once you have both running you could use: {{kint(item.content)}} to get a full list of all items within the content array.
Below an example of the kint() output.
As both are development plugins, they should only be used for debugging and not be installed on a production website.

Modifiable pagination on Shopify Collection

I was wondering if there was any way to make pagination user modifiable on the front-end? I've been searching for a solution, but can't find anything similar that addresses this specific problem.
I'm aware that at present, you can assign a limit to the pagination, for example:
{% paginate collection.products by 12 %}
What I'm trying to achieve here is the ability for the user to change the number of products viewable, per page, from a dropdown menu. So for example, the user chooses 24, the page refreshes with the revised pagination limit active on the page.
Any help getting started here would be greatly appreciated. Thanks!
You could create 2 alternate templates called 12 collection.12.liquid and 24 collection.24.liquid which would paginate by 12 and 24, respectively. All your templates will be below:
collection.liquid:
{% paginate collection.products by 20 %}
{% include ‘collection-template’ %}
{% endpaginate %}
collection.12.liquid:
{% paginate collection.products by 12 %}
{% include ‘collection-template’ %}
{% endpaginate %}
collection.24.liquid:
{% paginate collection.products by 24 %}
{% include ‘collection-template’ %}
{% endpaginate %}
Then you could create links to those other templates using the view parameter.
<a href=”http://myshop.com/collections/shirts?view=12”>12 per page</a>
<a href=”http://myshop.com/collections/shirts?view=24”>24 per page</a>
More detail, please refer this link here

Paginate Shopify blog with offset? (Liquid)

I'm trying to figure out how to paginate a blog starting at a specific offset. For example, the first page features posts 5-8, the second page 9-12, third page 13-16, and so on. This doesn't seem to work, but I'm looking for something along these lines -
{% paginate blog.articles offset:4 by 4 %}
{% for article in blog.articles
...
{% endfor %}
{% endpaginate %}
Are there any workarounds here?
I was trying to see if there's any way I could find a decent workaround in Liquid but there isn't, since you're offsetting a full page; it'd be perfectly possible to show 5-8 on the first page, but you'd need to show 9-16 on second page and so on and so forth. I'm afraid Shopify paginate won't help you here.
There could be specific workarounds for your situation; please give us more detail and we'll see. For example, if you always want to show the four latest blog articles on every page, and leave pagination handle the rest, a possible way would be to do something as per the following:
{% for article in blogs.news.articles limit: 4 %}
{{ article.title }}
{% endfor %}
{% paginate blog.articles by 4 %}
{% unless paginate.current_page == 1 %}
{% for article in blog.articles %}
{{ article.title }}
{% endfor %}
{% endunless %}
{% include 'pagination' %}
{% endpaginate %}
So outside of any pagination go through the handle to obtain the latest 4 to feature them, and if you're not on page 1, use pagination to print 4 more articles on each page.
If not, this is one of the scenarios where you should consider exposing your own service that internally calls and retrieves articles from the Shopify API as per your needs.
The offset should be specified in your loop parameters, not in pagination.

Combining optional block with element in twig

In defining a base template in twig, I want to reserve an area for special notifications, using a block. It could be an extra sidebar that may contain all kinds of things (calendar, or some extra information, whatever.) The default should be emtpy, but any child template may extend and fill it.
Now I want to have all those extensions be included in a <div class='special-sidebar'> if the block is extended, and not show anything if it is not included. Is there any way to define the containing element in the base template?
{# Base template #}
Content etc...
{% block special %} {# may be overridden by child template #}{% endblock %}
{# child template #}
{% block special %} Here, the special sidebar is filled! {% endblock %}
The base page should show just the content:
// base template:
Content etc...
And the child page:
// child template
Content etc...
<div class"special-sidebar"> Here, the special sidebar is filled! </div>
Where and how do I put the html for this? I could define it in every child, but that means you have to remember to use the correct html each time, I'd rather set this in the base template and not bother with it. But I don't want empty elements in my page if the block is not overridden.
You can solve it like this, if you want to display a certain block only if it has content. Hope, this is what you're looking for.
Example index.html.twig
{% set _block = block('dynamic') %}
{% if _block is not empty %}
{{ _block|raw }}
{% endif %}
Example part.html.twig
{% extends "index.html.twig" %}
{% block dynamic %}
Block content goes here.
{% endblock %}
Source: How to check if a block exists in a twig template - Symfony2

Twig - Do not interpret

I'm looking for a tag which would enable me to stop interpreting in a section, in a template.
So I'm looking for something that is similar to smarty's literal tag.
Thanks
I think you're looking for {% verbatim %} / {% endverbatim %}. In earlier versions these were called {% raw %} / {% endraw %}.
See also Twig Escaping.
In Twig, it is now verbatim, see http://twig.sensiolabs.org/doc/tags/verbatim.html

Resources