question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hidden variable passed in via Sprig 'does not exist' when set to a comma seperated string.

See original GitHub issue

Question

I am trying to pass in a hidden variable passed in via Sprig. However, I am getting back the error that _sections 'does not exist' when _sections is set to a comma separated string and passed into the sprig template.

Here is a shortened version of my code:

template.twig

{% set sections = ['features', 'blog', 'researchPapers'] %}

{{ sprig('_includes/_sprig/search-megamenu.twig', {
  _sections: sections|join(','),
  nameSpace: 'technical',
  placeholderText: 'Search technical articles',
  allOptionsLabel: 'All Sections'
}) }}

sprig.twig

{% set section = section ?? '' %}

{% set alllowedSectionHandles = _sections|split(',') %}
{% set allowedSections = alllowedSectionHandles|map(handle => craft.app.sections.sectionByHandle(handle)) %}

{% set section = section in alllowedSectionHandles ? section : alllowedSectionHandles %}

  {% if alllowedSectionHandles %}
    <div>
      <label for="section" class="sr-only">Directory</label>
      <div class="max-w-lg rounded-md shadow-sm sm:max-w-xs">
        <select
          sprig
          id="section"
          name="section"
          class="pr-6 {{ classes.input }}"
        >
          <option value="">All Sections</option>
           {% for allowedSection in allowedSections %} 
           <option value="{{ allowedSection.handle }}" {{ allowedSection.handle == section ? 'selected' }}> 
           {{ allowedSection.name }} 
           </option> 
           {% endfor %} 
        </select>
      </div>
    </div>
  {% endif %}

When I dump dd(_sections) I get the array outputted. if I pass in just a single string as _section. All is ok and works.

What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
bencrokercommented, May 11, 2021

No problem and good luck!

0reactions
terryuptoncommented, May 11, 2021

@bencroker - I think I have found the problem (me). I have stripped it all down and your code is working just fine. I am working on rebuilding it back up and I think the problem is a result of an include I had not updated yet (doh). I will test and reveal shortly. But I think this is the cause. Apologies for taking up your time…

Read more comments on GitHub >

github_iconTop Results From Across the Web

RequestParam variables return a set of comma separated ...
This is a bug in your JSP page. You likely have a hidden and an input with the same name. This results in...
Read more >
Spring Security: Authentication and Authorization In-Depth
You can use this guide to understand what Spring Security is and how its core features like authentication, authorization or common exploit ...
Read more >
Web on Servlet Stack - Spring
String that is passed to the context instance (specified by contextClass ) to indicate where contexts can be found. The string consists potentially...
Read more >
Jdbi 3 Developer Guide
While there is some ORM-like functionality, Jdbi goes to great length to ensure that there is no hidden magic that makes it hard...
Read more >
Describing Parameters - Swagger
In Swagger, a path parameter is defined using in: path and other attributes as necessary. The parameter name must be the same as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found