# Variable im Smarty Plugin zuweisen?

**URL:** <https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108>\
**Category:** Programmierung\
**Created:** [29. März 2018 um 15:15 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108 "2018-03-29T15:15:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexG](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@AlexG](https://forum.shopware.com/u/AlexG)\
**Post date:** [29. März 2018 um 15:15 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108/1 "2018-03-29T15:15:37Z")

</div>

Wie ist es möglich in einer [Smarty template function](https://www.smarty.net/docs/en/plugins.functions.tpl) eine Variable zuzuweisen?

Beispiel: Ich möchte auf meiner index.tpl die Variable {$foo} darstellen?

**myTheme/frontend/index/index.tpl**

```
{extends file='parent:frontend/index/index.tpl'}

{block name='frontend_index_after_body'}
    {$smarty.block.parent}
    {foobar}
    {$foo}
{/block}

```

**myTheme/\_private/smarty/function.foobar.php**

```
assign('foo', 'bar');
}
?>

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![Shopwareianer](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Shopwareianer](https://forum.shopware.com/u/Shopwareianer)\
**Post date:** [29. März 2018 um 16:15 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108/2 "2018-03-29T16:15:02Z")

</div>

[https://developers.shopware.com/designers-guide/smarty/](https://developers.shopware.com/designers-guide/smarty/)  
[Template Functions | Smarty](https://www.smarty.net/docs/en/plugins.functions.tpl)

Im Template Block dann ausgeben mit&nbsp;{foobar}.

---

<div class="post-metadata">

**Author:** ![AlexG](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@AlexG](https://forum.shopware.com/u/AlexG)\
**Post date:** [29. März 2018 um 16:33 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108/3 "2018-03-29T16:33:03Z")

</div>

Es geht ja nicht darum einfach eine Ausgabe zu haben sonder eine Variable über das Smarty Plugin festzulegen. Die Smarty Documentation ist auch im ersten Post verlinkt.

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [29. März 2018 um 16:59 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108/4 "2018-03-29T16:59:49Z")

</div>

Wenn du es wie folgt aufrufst&nbsp; kann du auf {$meineVariable} zurückgreifen statt {foobar}.

```
{foobar assign="meineVariable"}
...
{$meineVariable}
...
{$meineVariable->arrayfeld}

```

Macht aber nur Sinn wenn du in der Function ein Array zurückgibst.&nbsp;Ansonsten wäre mich nicht bekannt dass man etwas anderes anstellen&nbsp;kann ausser “return”.

---

<div class="post-metadata">

**Author:** ![AlexG](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@AlexG](https://forum.shopware.com/u/AlexG)\
**Post date:** [29. März 2018 um 18:09 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108/5 "2018-03-29T18:09:38Z")

</div>

> [@brettvormkopp schrieb:](https://forum.shopware.com/profile/6611/brettvormkopp "brettvormkopp")
> 
> Wenn du es wie folgt aufrufst&nbsp; kann du auf {$meineVariable} zurückgreifen statt {foobar}.
> 
> {foobar assign=„meineVariable“}  
> …  
> {$meineVariable}  
> …  
> {$meineVariable-\>arrayfeld}
> 
> Macht aber nur Sinn wenn du in der Function ein Array zurückgibst.&nbsp;Ansonsten wäre mich nicht bekannt dass man etwas anderes anstellen&nbsp;kann ausser „return“.

Ein Array zurückgeben wäre natürlich auch eine Idee. Das ich da selbst nicht drauf gekommen bin ![Blush](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/2f2885a69a3ab7fc5454d32cd96e7133e5bcd558.png "Blush")  
Also gibt es keine Möglichkeit über das Smarty Plugin die Variable zuzuweisen?

---

<div class="post-metadata">

**Author:** ![AlexG](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@AlexG](https://forum.shopware.com/u/AlexG)\
**Post date:** [30. März 2018 um 09:48 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108/6 "2018-03-30T09:48:15Z")

</div>

Folgender Code scheint einwandfrei zu funktionieren:

**myTheme/frontend/index/index.tpl**

```
{extends file='parent:frontend/index/index.tpl'}

{block name='frontend_index_after_body'}
    {$smarty.block.parent}
    {foobar assign="myVar"}
    {$myVar.output}
{/block}

```

**myTheme/\_private/smarty/function.foobar.php**

```
assign($params['assign'], 'bar');

    // for arrays
    $template->assign($params['assign'], ['output' => 'bar']);
}
?>

```

&nbsp;

---

<div class="post-metadata">

**Author:** ![brettvormkopp](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/brettvormkopp/32/7788_2.png) [@brettvormkopp](https://forum.shopware.com/u/brettvormkopp)\
**Post date:** [30. März 2018 um 13:22 UTC](https://forum.shopware.com/t/variable-im-smarty-plugin-zuweisen/52108/7 "2018-03-30T13:22:59Z")

</div>

Ah super. Meine erstes Beispiel war nicht so korrekt, habe jetzt aber mal meine alte instagram smarty-function ausgebuddelt. Wie du es hier machst ist es voll korrekt und sogar noch besser, bei dir muss man das Array nicht erst in einem foreach auflösen!
