# Twig Extension returns Twig/Markup not Object

**URL:** <https://forum.shopware.com/t/twig-extension-returns-twig-markup-not-object/87122>\
**Category:** Shopware 6 (German)\
**Created:** [24. April 2021 um 15:15 UTC](https://forum.shopware.com/t/twig-extension-returns-twig-markup-not-object/87122 "2021-04-24T15:15:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [24. April 2021 um 15:15 UTC](https://forum.shopware.com/t/twig-extension-returns-twig-markup-not-object/87122/1 "2021-04-24T15:15:30Z")

</div>

Hey,

I wrote a little Twig Extension, which I pass a product ID and the context as a parameter and it will return me a SalesChannelProductEntity to use it later in the Template, which is done in another class called GetProductsByID and the method getProduct.

```
public function getFunctions()
    {
        return [
            new TwigFunction('getProductByID', [$this, 'TwigGetProductByID']),
        ];
    }

    public function TwigGetProductByID($productID, SalesChannelContext $salesChannelContext):SalesChannelProductEntity
    {
        if ($this->salesChannelProductRepository instanceof SalesChannelRepositoryInterface)
        {
            if ($salesChannelContext instanceof SalesChannelContext)
            {   
                $productByID = new GetProductsByID($this->salesChannelProductRepository, $salesChannelContext); //Instance of my Class to get the rroduct
                $product = $productByID->getProduct($productID); // Call the function to return the product
                print_r($product); //this prints the whole SalesChannelProductEntity object fine
                return $product;
            }
        } 
    }

```

The call in the template with the product ID and the actual SalesChannelContext as parameter:

```
{% set product1 %}
{{ getProductByID('2a88d9b59d474c7e869d8071649be43c', context) }}
{% endset %}
{{ dump(product1) }}

```

The print\_r() before the return outputs me the whole filled SalesChannelProductEntity object.  
But when I set my variable product1 in my template and dump it, it will display just the product name and the charset in a Twig/Markup.

```
Twig\Markup {#41665 ▼
  -content: "Main product"
  -charset: "UTF-8"
}

```

Any idea what I’m doing wrong? This is my first plugin attempt and I’m racking my brain for days.

Best regards  
Alex

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [24. April 2021 um 15:26 UTC](https://forum.shopware.com/t/twig-extension-returns-twig-markup-not-object/87122/2 "2021-04-24T15:26:27Z")

</div>

This dumps me the full object. So the problem is just the way I assign the object to a variable.

`{{ dump( getProductByID('2a88d9b59d474c7e869d8071649be43c', context) ) }}`

How do I assign the return of my Twig Extension to a variable in the correct way?

Best regards  
Alex

---

<div class="post-metadata">

**Author:** ![AlexBS](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AlexBS](https://forum.shopware.com/u/AlexBS)\
**Post date:** [24. April 2021 um 15:27 UTC](https://forum.shopware.com/t/twig-extension-returns-twig-markup-not-object/87122/3 "2021-04-24T15:27:07Z")

</div>

Bitte in die englische Rubrik verschieben, ich war mir ganz sicher, das in der englischen Forumssparte angelegt zu haben, Sorry!!
