Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Display Ausgabe über Variable -> wird nicht geparst :(

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
pcollins
Smarty Rookie


Joined: 01 Dec 2004
Posts: 5

PostPosted: Wed Dec 01, 2004 7:16 am    Post subject: Display Ausgabe über Variable -> wird nicht geparst :( Reply with quote

Guten Morgen,
ich bin echt am verzweifeln.
Hier mein Problem:

Ich bekomme aus eine Funktion mein Template zurück und diese möchte ich nun per Smarty ausgeben. Hier meine Script ( ist verkürzt ):

Code:

$string   ='{if $name == "Fred" || $name == "Wilma"}geht {else} geht nicht{/if}';

eval("\$template = \"{$template_string}\";");
$smarty->assign('template_string', $string);
$smarty->display("var:template");


Nun wird mir zwar $string ausgegeben, aber die IF-Funktion wird nicht beachtet, d.h. der String wird mir so komplett ausgegeben.
Wenn ich nun aber den String in ein File packe und mit $smarty->display("file:template,tpl"); einlese, funktioniert alles.

Einer eine Idee wo der Fehler ist ?

mfg
Back to top
View user's profile Send private message
T.
Smarty Regular


Joined: 22 Aug 2004
Posts: 69
Location: AT

PostPosted: Wed Dec 01, 2004 8:55 am    Post subject: Reply with quote

Das Problem liegt einfach daran, dass Smarty deinen übergeben Text ($String) als Ausgabetext verwendet und nicht als Smarty-Funktion, wie du es gerne hättest.

Ich bilde mir ein im englischen Supportforum was zu dem Thema gelesen zu haben, hab jetz leider auf die schnelle nichts gefunden.

mfg Thomas
_________________
Oppossom - Heavy rock from austria
Back to top
View user's profile Send private message Visit poster's website
pcollins
Smarty Rookie


Joined: 01 Dec 2004
Posts: 5

PostPosted: Wed Dec 01, 2004 9:01 am    Post subject: Reply with quote

mh, eine idee wonach ich suchen könnte ?
Ich suche wirklich schon seit 2 Tagen hier im Forum nach einer Lösung und finde einfach nichts.
Back to top
View user's profile Send private message
T.
Smarty Regular


Joined: 22 Aug 2004
Posts: 69
Location: AT

PostPosted: Wed Dec 01, 2004 9:07 am    Post subject: Reply with quote

Wenn ich ne gute Idee hätt, hätt ichs schon gefunden Wink

Aber es kommt sicher noch jemand im Topic vorbei, der schlauer ist als ich Wink
_________________
Oppossom - Heavy rock from austria
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Dec 01, 2004 9:48 am    Post subject: Reply with quote

du könntest {eval} verwenden : http://smarty.php.net/manual/en/language.function.eval.php

aber um mal rasmus lerdorf zu zitieren:
"If eval() is the answer, you're almost certainly asking the wrong question."

der meinte da zwar das php-eval, die aussage passt aber auch wunderbar auf das smarty-eval.


warum hast du den template-code in einem string? wo kommt der string her?
ich würde hier lieber die ursache bekämpfen, als die symptome.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
pcollins
Smarty Rookie


Joined: 01 Dec 2004
Posts: 5

PostPosted: Wed Dec 01, 2004 10:34 am    Post subject: Reply with quote

Also der Template String kommt aus einer Funktion.
Die ist schon sehr Aufwendig ( Cached selber, einige Templates kommen aus der DB und andere liegen als File vor ). Ausserdem ist das ganze mit einem Editor für die Templates verbunden und daher möchte ich hier auch erstmal nichts ändern.

Ok, zurück zum problem Wink

mein Code sieht nun so aus:
Code:

$smarty->assign("name",$_GET['name']);
$string = "<br>{if \$name=='hans'}hans{else}kein hans{/if}";
$smarty->_compile_source('evaluated template', $string, $_var_compiled);
$smarty->_eval('?>' . $_var_compiled);


Das Funktioniert ja auch so weit so gut.
Doch nun ein neues Problem:
Ich muß vor einem $-Zeichen nun immer ein \ setzen.
Addslahes geht hier nicht, weil nur die " und ' maskiert werden.

Ok, ich könnte mir str_replace arbeiten, aber hat jemand eine andere Idee ?

mfg
Back to top
View user's profile Send private message
pcollins
Smarty Rookie


Joined: 01 Dec 2004
Posts: 5

PostPosted: Thu Dec 02, 2004 9:10 am    Post subject: Reply with quote

Ahoi,

so, bin nun ein kleines Stück weiter:

Code:

$smarty->assign("name",$_GET['name']);
$smartyvar =  "{if \$name == 'Fred' || \$name == 'Wilma'}Name vorhanden{else}Kein Name{/if}";
$vartpl = "{$smartyvar}";
$smarty->display('var:vartpl');


Das Funktioniert ja auch wunderbar. Nun möchte ich aber eine andere Ausgabe machen. Dazu habe ich einfach diesen Block nochmal kopiert, in die Variable $smartyvar einen anderen Text hineinkopiert und dann darunter gesetzt.

Hier nun der komplette Quellcode:

Code:

$smarty->assign("name",$_GET['name']);
$smartyvar =  "{if \$name == 'Fred' || \$name == 'Wilma'}Name vorhanden{else}Kein Name{/if}";
$vartpl = "{$smartyvar}";
$smarty->display('var:vartpl');

$smarty->assign("name",$_GET['name']);
$smartyvar =  "test";
$vartpl = "{$smartyvar}";
$smarty->display('var:vartpl');


Doch leider funktioniert das nicht. Es wird mir der Inhalt der ersten $smartyvar ausgeben, und das 2 mal. D.h ich bekomme "Kein NameKein Name" zurück. Irgendwie wird die 2. $smartyvar nicht beachtet.

Einer eine Idee ?
Back to top
View user's profile Send private message
T.
Smarty Regular


Joined: 22 Aug 2004
Posts: 69
Location: AT

PostPosted: Thu Dec 02, 2004 9:14 am    Post subject: Reply with quote

hm, hm...

also wenn du zweimal
Code:

$smarty->display('var:vartpl');


machst, dann wird das template zweimal durchlaufen, dadurch kommts das du zweimal ne Ausgabe hast. normalerweis hat man nur ein $smarty-> display pro Datei.
_________________
Oppossom - Heavy rock from austria
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Dec 02, 2004 9:50 am    Post subject: Reply with quote

pcollins wrote:
Doch leider funktioniert das nicht. Es wird mir der Inhalt der ersten $smartyvar ausgeben, und das 2 mal. D.h ich bekomme "Kein NameKein Name" zurück. Irgendwie wird die 2. $smartyvar nicht beachtet.


in dem anderen thread zu dem thema sagt boots:
boots wrote:
... So forget about this resource, its just a little demonstration is all


bitte beherzige das. diese komische var-resource ist nicht für den produktiv-einsatz gedacht bzw. geeignet.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP