Smarty: bir değişken olarak dize değerlendirmek için nasıl

2 Cevap php

Benim şablonuna atanan değişken 123 auto oluşturulan ve aynı zamanda {$ autoValue} olarak şablonuna atanan burada {$ testVariable_123} olduğunu

Yani benim şablonda ben yukarıda değişkenin değerini almak istiyorum.

{assign var="anotherValue" value="testVariable_"|cat:$autoValue}

Ben yerine değeri {$ anotherValue} yazdırmak Yani ben dize 'testVariable_123' olsun

Herhangi bir yardım takdir ...

2 Cevap

You need to create a variable whose value is {$testVariable_123}
Then you can call {eval} on it.

The problem is, I couldn't find a way to do this in a good manner.
Everything that looks reasonably good doesn't work and the options that work are ugly.

Belki uygulama tasarımında bazı değişiklikler düşünecektim?

Burada çalışma başardı ne:

# file.php:

  $smarty->assign("autoValue", 123);
  $smarty->assign("testVariable_123", "foo");

  //Option 1
  $smarty->assign("anotherValue", "{\$testVariable_123}");
  //Option 2
  $smarty->assign("rb", '}'); // Hack to get the right bracket } withou Smarty parsing it.
  //Option 3
  $smarty->assign("mask", '{$testVariable_%s}'); // pass the full string_format "mask" directly from PHP

# file.tpl

1) Uses the $anotherValue from PHP:
Plain: {$anotherValue}
Evaled: {eval var=$anotherValue}

2) Build the string on Smarty itself:
{assign var="yetAnotherValue" value=$autoValue|string_format:"{\$testVariable_%s$rb"}
Plain: {$yetAnotherValue}
Evaled: {eval var=$yetAnotherValue}

3) Build the string using the mask from php:
{assign var="enoughOfValue" value=$autoValue|string_format:$mask}
Plain: {$enoughOfValue}
Evaled: {eval var=$enoughOfValue}

Çoğunlukla, sorun Smarty bir dize ortasında olsa bile bir kapanış ayraç} veya $ değişken göz ardı edecektir. \ Ile kaçan ne çalışmaz.

Eğer çalışırsanız:

{assign var="yetAnotherValue" value="{\$testVariable_$autoValue}"}

o sonunda "} görmezden gibi Smarty deyimi dikkate alacaktır:

{assign var="yetAnotherValue" value="{\$testVariable_$autoValue}

and it will evaluate the $testVariable even tho it was supposed to be escaped.
So we will end up with {\123 as the value :(

Everything I tried ended up stumbling on that issue. If you find a better way, please, make sure you share it here :)

{assign var="myVar" value="{\$testVariable"|cat:$autoValue|cat:$smarty.rdelim}
{eval var=$myVar}

Benim için çalıştı.