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.

Nesting Sprig components when using component classes

See original GitHub issue

We’re building a shop (Commerce Pro) and using Sprig to render the overview pages using component classes. We’re also using Verbb’s Wishlist plugin so the customer can add products to their wishlist from an overview page.

The component class is working great. It gives us much more flexibility with filters. We’re using Sprig in combination with Alpine.js to trigger the filters and refresh.

But I’m having an issue when using the Wishlist plugin as a nested Sprig component. Even if I add the wishlist code itself to my overview template. When clicking the wishlist toggle button the Sprig action has an error.

However, this is working fine when ditching the component class and going for good ol’ Twig. So, I’m clearly missing something. Any idea what I’m doing wrong?

Craft CMS v3.7.45.1 Sprig v1.13.0

Error:

Argument 1 passed to craft\web\View::renderTemplate() must be of the type string, null given, called in /Volumes/.../vendor/putyourlightson/craft-sprig-core/src/controllers/ComponentsController.php on line 65

Books.php

class Books extends Component
{
    public $page = 1;
    public $limit;
    protected $_template = 'sprig/books';

    public function attributes()
    {
        $attributes = parent::attributes();

        $attributes[] = 'books';

        return $attributes;
    }

    public function getBooks()
    {
        $query = Craft::$app->getElements()->createElementQuery(Product::class);
        $query->limit($this->limit);
        $query->all();

        return $query;
    }
}

books/index.twig

{{ sprig('Books', { limit: 24 }, { id: 'books', 's-trigger': 'refresh' }) }}

{{ sprig.script }}

sprig/books.twig

{% set pageInfo    = sprig.paginate(books, page) %}
{% set pageResults = pageInfo.pageResults %}

{% if pageResults|length %}
    <div class="grid grid-cols-4 gap-8">
        {% for item in pageResults %}
            <ul>
                <li>{{ item.id }}</li>
                <li>{{ item.title }}</li>
                <li>
                    {{ sprig('sprig/wishlist', { bookId: item.id }, { id: 'books-wishlist-item-' ~ item.id }) }}
                </li>
            </ul>
        {% endfor %}
    </div>
{% endif %}

sprig/wishlist.twig

{% set item = craft.wishlist.item(bookId, null, 'wishlist') %}

<button sprig s-method="post" s-action="wishlist/items/toggle" s-vals="{{ { elementId: bookId }|json_encode }}">
    {% if item.inList %}
        Remove from wishlist
    {% else %}
        Add to wishlist
    {% endif %}
</button>

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
stevendierickcommented, Jul 1, 2022

Yes, the update resolves it. Works like a charm now! Thanks @bencroker!

0reactions
bencrokercommented, Jun 30, 2022

I see what was going on, fixed in https://github.com/putyourlightson/craft-sprig-core/commit/5c6b72e1f11e014d35b0d1b4931d90206a837244, you can test by running composer update. Let me know if that resolves it for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Sprig Components · Issue #6 · putyourlightson/craft-sprig
I'm using your "Load More" and "Add to Wishlist" recipes from your Sprig Cookbook. Everything is working great except when there's a nested...
Read more >
Sprig Cookbook - PutYourLightsOn
Sprig allows you to create reactive components from Twig templates. Below are some recipes to help you learn some of the possibilities that...
Read more >
What will be the spring bean creation flow for nested ...
When scanning classes Spring will check for dependencies to determine creation order. ClassB and ClassC will get created before ClassA but ...
Read more >
Introduction / Nested components • Svelte Tutorial
svelte file holds a component that is a reusable self-contained block of code that encapsulates HTML, CSS, and JavaScript that belong together. Let's...
Read more >
Step 4: Create a Nested Component - Salesforce Developers
As your component grows, you want to break it down to maintain granularity and encapsulation. This step walks you through creating a component...
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