How can I hide and show a section using a conditional statement and PDF generator? - pdf

Using craft CMS I am adding an option to be able to hide a section from a PDF.
The PDF is created using PHP and a library called Snappy.
The light-switch is turned off (0) by default.
I have added the conditional statement and the light-switch, tested by wrapping other sections of the site (not part of the PDF) inside the if condition.
When turning the light-switch on and off I am able to hide and show sections.
The PDF does not seem to be working that way. The section is always hidden even when the light-switch is turned on.
I don't understand how the backend of the PDF works so is it something to do with how PDFs are generated?
Also it's showing blank pages where the content "would" be which is not desired.
//====Master PDF layout
{% extends 'abc/pdf/_base.html' %}
{% block content %}
{% include 'abc/pdf/_section1.html' %}
{% include 'abc/pdf/_section2.html' %}
{% include 'abc/pdf/_section3.html' %}
{% include 'abc/pdf/_section4.html' %}
{% include 'abc/pdf/_section5.html' %}
{% endblock %}
//==== Section with condition (abc/pdf/_section4.html)
{% if (entry.showsection4 is defined and entry.showsection4) and dateRanges | length %}
<div class="page">
<div class="page__inner">
A bunch of content in section 4 that I want to hide and show
</div>
</div>
{% endif %}

Related

How to use a variable name inside of liquid shortcode to render a section file in a template file for Shopify?

Objective:
I want to render a Shopify section inside of a template using a variable as the section file's name. i.e.
Example
Template: templates/parent.liquid
Section: sections/child.liquid
Attempting to render (include) child.liquid inside parent.liquid when variableName = 'child'
context: *child* could be anything -- in my theme's specific use case, I'm assigning variableName = page.handle. If page.handle matches an existing product vendor AND sections/[page.handle].liquid exists, I am includuing it in the page.
WHY? I want to avoid a hardcoded list of available section-files in lieu if this proposed progammatic approach of dynamic filenames derived from the current page.
The following code would go inside templates/parent.liquid
this works (hardcoded quoted strings):
BUT, I want to avoid hard-coded filenames at all costs.
{% section 'child' %}
these all throw an error (any form of dynamic filename):
{% section variableName %}
{% section {{ variableName }} %}
{% section 'variableName' %}
{% section "'" + variableName + "'" %}
{% section variableName %}
{% section "{{ variableName }}" %}
{% section [variableName] %}
edit more errors
{% render variableName %} Syntax error in tag 'render' - Template name must be a quoted string
{% include variableName %} look for a snippet, not a section
{% include sections.variableName %} Liquid error: Argument error in tag 'include' - Illegal template name
{% include sections.[variableName] %} Liquid error: Argument error in tag 'include' - Illegal template name
Research
The closest article I've found via Google is https://community.shopify.com/c/Shopify-Design/Dynamic-Liquid-Variable-inside-Liquid-tag/td-p/162451
Pseudo Answer
I'm searching for a working solution along the lines of:
{% include sections.[page.handle] %}
Use include or render
passing variable to section is not possible
Question asked here
Document
Variables created outside sections are not available within sections.
Likewise, variables created in sections are not available outside
sections. If a section includes a snippet, the snippet has access to
the variables in the section.
using variable as a filename will not work for render. It only works for include
For Example
{% assign fileName = "product-" | append: product.handle %}
{% capture productLinkContent %}
{% include fileName %}
{% endcapture %}
{% unless productLinkContent contains "Liquid error: Could not find asset " %}
{% include fileName %}
{% else %}
do something else
{% endunless %}
so, In your template/parent.liquid call {% section 'parent-template' %}
and in parent-template.liquid use include like above.
The reason is that you need to make sure that the section files exist so that it does not break shopify customizer, and there is no way to check that using liquid code right now.
Here is another solution if you really want to stick with the variable solution, is that you pass the file with if-else / case - when statement;
{% assign fileName = "product-" | append: product.handle %}
{% case fileName %}
{% when 'product-a' %}
{% section 'product-a' %}
{% when 'product-b' %}
{% section 'product-b' %}
{% else %}
do something else
{% endcase %}

how to properly query in collections page in shopify liquid?

I have create many collections like All Products, New Releases, T-Shirts, Pants etc. And I have two categories of products like Men's and Women's. Some collections specifically for Men's and some collections specifically for Women's. I create two tags Men's and Women's. I create menu by collections query by tags. My my products collection url is like this:
/collections/all-products/mens
/collections/all-products/womens
/collections/new-releases/mens
/collections/new-releases/womens
/collections/bras/womens
I want to show some text and menu list when collection.url show /mens or /womens.
{% if collection.url contains mens %}
do something
{% endif %
Above condition not working. I know why not working because {{ collection.url }} provide /collections/all-products. {{ page.url }} will not work for collection object. I haven't find any suggestion or liquid code reference where show men's or women's products in collections page it will show specific text.
If use in loop it will work.
{% for product in collection.products %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
<h3>Mens Products</h3>
{% endif %}
{% endfor %}
{% endfor %}
Above code will not work for me because it's inside loop. I need to show outside of loop. I don't understand how to achieve it. here is the reference site. Below have image how I want.
Need help!
You have access to the current_tags, refer to docs: https://shopify.dev/docs/themes/liquid/reference/objects/current-tags
This will return an array of all the tags you are viewing at the moment (in case you are viewing more than one tag).
So your check will be:
{% if current_tags contains 'mens' %}
do something
{% endif %}
That's pretty much the just of it.
I really like how the menu is coming along! Here are some ideas you could consider for your category specific menu, if you've not gotten where you want yet.
For your url strategy, what you're looking for is handle. Handles are specific to liquid. https://shopify.dev/docs/themes/liquid/reference/basics/handle
You could make a custom collection template if those 2 categories need to be fairly different: https://shopify.dev/tutorials/customize-theme-create-alternate-templates. If you do that, then you can use template_prefix from the collection object.
Assign a variable outside your loop and then set it inside the loop like:
{% assign is_mens = false %}
{% for tag in product.tags %}
{% if tag contains 'mens' %}
{% assign is_mens = true %}
{% endif %}
{% endfor %}
then {% if is_mens %} or {% unless is_mens %} for your dynamic content, or a case statement to define content specific to categories in your menu.
Hope this helps!

How can I pass classes in to a section in liquid / shopify?

For example passing in a snippet
{% include 'icon-top', classes:'back-to-top__icon' %}
I can pass the class back-to-top__icon into icon-top snippet
<svg class="icon {{ classes }}" somesvg stuff here ></svg>
Doing the same with a section doesn't work - is there any way to do this in liquid?
Sections doesn't accept anything outside of the section file. You can look the section like a closed platform nothing comes inside or outside of the section.
The means that variables created outside/inside the section are not accessible inside/outside of it.
That said you can hack it slightly to achieve what you want.
For example:
The section file:
test.section.liquid
The section file code:
<div class="{{dummy_class}}"></div>
Then you call the section this way:
<div style="display: none;">
{% section 'test.section' %}
</div>
{% capture section_capture %}
{% section 'test.section' %}
{% endcapture %}
{{ section_capture | replace: '{{dummy_class}}', 'back-to-top__icon' }}
Clarification
You might be asking why are we calling the section two times?
When we call the section in a {% capture %} tag it's not shown in the admin panel that's why are showing it in a hidden div only to show it in the admin and we don't do anything else with it.
After that we capture the section in a variable section_capture, this will return the content of section and we can replace anything we want in there.
That's why we added this {{dummy_class}} dummy variable. It's wrapped in liquid but you can treat it as text and not liquid, so we can write it like so #dummy_class# as well.
After that we just target that string and replace it {{ section_capture | replace: '{{dummy_class}}', 'back-to-top__icon' }}

Shopify If Tag does not contain

What's the opposite of contains in Shopify Liquid Smarty tags?
I basically want to hide products from Search page that has the tag hideme
Example code : {% if product.tags contains 'hideme' %}. In this I want to use does not contain instead of contains
The opposite of {% if %} is {% unless %} so your code would change to:
{% unless product.tags contains 'hideme' %}
<blinky> stuff mmm good for cookie monster </blinky>
{% endunless %}

show item only on collection pages

on shopify, i want a line of code of code from theme.liquid to show only on collection pages regardless the template used (i have a few custom collection templates). how do i write the code to do this?
currently i'm using.
{% if template == 'collection.home' %}
<div class="filter_btn">
loremipsum
</div>
{% endif %}
{% if template == 'collection.featured' %}
<div class="filter_btn">
loremipsum
</div>
{% endif %}
similar duplicate of code above for other collection template
i will be adding new collections with new custom templates in the future, but that means i have to duplicate more of the same to cover new collection templates..
how can i have a line of code that covers all collection templates / collection pages
Use the contains operator instead:
{% if template contains 'collection' %}
I am likely a collection template
{% endif %}
See more about contains here:
http://docs.shopify.com/themes/liquid-documentation/basics/operators#contains