# \[Gelöst\] Eine Variable per assign() an das Template geben (bei ajaxcall)

**URL:** <https://forum.shopware.com/t/gelost-eine-variable-per-assign-an-das-template-geben-bei-ajaxcall/56661>\
**Category:** Programmierung\
**Created:** [23. November 2018 um 20:39 UTC](https://forum.shopware.com/t/gelost-eine-variable-per-assign-an-das-template-geben-bei-ajaxcall/56661 "2018-11-23T20:39:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![karlharris](https://avatars.discourse-cdn.com/v4/letter/k/58956e/32.png) [@karlharris](https://forum.shopware.com/u/karlharris)\
**Post date:** [23. November 2018 um 20:39 UTC](https://forum.shopware.com/t/gelost-eine-variable-per-assign-an-das-template-geben-bei-ajaxcall/56661/1 "2018-11-23T20:39:00Z")

</div>

Hallo Leute,

weiß nicht ob das vielleicht eine blöde Frage ist, aber weiß jemand wieso der&nbsp;hier, per assign(), übergebene String nicht im Template ankommt?

```
    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            'Enlight_Controller_Action_PreDispatch_Frontend_Checkout' => 'beforeAddArticle',
            'Enlight_Controller_Action_PostDispatchSecure_Frontend_Checkout' => 'afterAddArticle'
        ];
    }

    /**
     * @param \Enlight_Event_EventArgs $args
     */
    public function beforeAddArticle(\Enlight_Event_EventArgs $args)
    {
        $controller = $args->get('subject');
        $request = $controller->Request();
        $view = $controller->View();

        if($request->getActionName() == 'addArticle')
        {
            $view->assign('test', 'DIES IST EIN TEST');
            echo '';
            print_r(get_class_methods(get_class($view)));
            echo '';
        }
    }

    /**
     * @param \Enlight_Event_EventArgs $args
     */
    public function afterAddArticle(\Enlight_Event_EventArgs $args)
    {
        $controller = $args->get('subject');
        $request = $controller->Request();
        $view = $controller->View();

        if($request->getActionName() == 'addArticle')
        {
            $view->assign('test', 'DIES IST EIN TEST');
        }
    }

```

&nbsp;

Und in&nbsp;der ajax\_add\_article.tpl:

```
    {block name='checkout_ajax_add_error'}
        {$smarty.block.parent}
        
            {include file="frontend/_includes/messages.tpl" type="info" content="test->{$test}"}
        
    {/block}

```

&nbsp;

Aber die Ausgabe sieht dann leider so aus:

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/e/ec52e8de589429662483a9f6dc6f3c8a59a70cb3.png)

&nbsp;

Danke im Voraus für etwaige Ideen :))

---

<div class="post-metadata">

**Author:** ![shyim](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/shyim/32/7681_2.png) [@shyim](https://forum.shopware.com/u/shyim)\
**Post date:** [23. November 2018 um 21:03 UTC](https://forum.shopware.com/t/gelost-eine-variable-per-assign-an-das-template-geben-bei-ajaxcall/56661/2 "2018-11-23T21:03:34Z")

</div>

In der addArticle action werden bloß die Artikel hinzugefügt und dann wird forwarded. Bei einem forward gehen alle Template Variabeln weg. Du musst wenn dann die Variable nach dem Forward assignen 🙂

---

<div class="post-metadata">

**Author:** ![karlharris](https://avatars.discourse-cdn.com/v4/letter/k/58956e/32.png) [@karlharris](https://forum.shopware.com/u/karlharris)\
**Post date:** [24. November 2018 um 00:12 UTC](https://forum.shopware.com/t/gelost-eine-variable-per-assign-an-das-template-geben-bei-ajaxcall/56661/3 "2018-11-24T00:12:11Z")

</div>

Hi @Shyim‍,

vielen Dank für den Ansatz&nbsp; ![Thumb-Up](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/7fcda44f782696668a3d093569d15e88105769e1.png "Thumb-Up")&nbsp;ich bin dem Ganzen weiter gefolgt und irgendwie endet es hier:

```
    /**
     * Ajax add article action
     *
     * This action will get redirected from the default addArticleAction
     * when the request was an AJAX request.
     */
    public function ajaxAddArticleAction()
    {
        die('#######');
        // Empty but can't be removed for legacy reasons
    }

```

Da hänge ich nun fest (das die() ist natürlich&nbsp;von mir&nbsp; ![Grin](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/70033cde8e8ff79cfa538293ae71318d6f788423.png "Grin") )&nbsp;Weißt du wie es von da aus weiter geht?

Schönes Wochenende erstmal 🙂

---

<div class="post-metadata">

**Author:** ![shyim](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/shyim/32/7681_2.png) [@shyim](https://forum.shopware.com/u/shyim)\
**Post date:** [24. November 2018 um 10:51 UTC](https://forum.shopware.com/t/gelost-eine-variable-per-assign-an-das-template-geben-bei-ajaxcall/56661/4 "2018-11-24T10:51:39Z")

</div>

Dump dir mal&nbsp;$request-\>getActionName() dann solltest du wissen welche Action wirklich aufgerufen wird

---

<div class="post-metadata">

**Author:** ![karlharris](https://avatars.discourse-cdn.com/v4/letter/k/58956e/32.png) [@karlharris](https://forum.shopware.com/u/karlharris)\
**Post date:** [29. November 2018 um 14:27 UTC](https://forum.shopware.com/t/gelost-eine-variable-per-assign-an-das-template-geben-bei-ajaxcall/56661/5 "2018-11-29T14:27:38Z")

</div>

Hallo Leute,

da hab ich den Wald vor lauter Bäumen nicht gesehen&nbsp; ![Grin](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/70033cde8e8ff79cfa538293ae71318d6f788423.png "Grin")&nbsp;die Action nach der man abfragen muss in dem Fall ist genau die mit der man in der Methode ajaxAddArticleAction() landet - damit klappts:

```
if($request->getActionName() === 'ajax_add_article')
{
    $view->assign('test', 'DIES IST EIN TEST');
}

```

Danke für die Hilfe @Shyim‍
