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

if-Abfrage mit eigener Funktion funktioniert nicht

 
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
Samson
Smarty n00b


Joined: 23 Dec 2016
Posts: 3

PostPosted: Fri Dec 23, 2016 8:40 am    Post subject: if-Abfrage mit eigener Funktion funktioniert nicht Reply with quote

Ich habe folgende Funktion:
Code:

function smarty_function_catwatch($params, &$smarty)
{
   static $catwatch = array(); // Variablen aus Template werden hier gespeichert

   if(!isset($catwatch['adcat']))
   {
      // Array initialisieren
      $catwatch = array
      (
         'actcat' => 0,
         'adcat'  => 0
      );
   }
   
   // Wurden Variablen übergeben? Falls ja, dann lokal speichern
   if(isset($params['adcat']))
   {
      // Array aktualisieren
      $catwatch = array
      (
         'actcat' => (int)$params['actcat'], // ID der gerade angezeigten Kategorie
         'adcat'  => (int)$params['adcat'] // Kategorie der zu prüfenden Anzeige
      );
      $retval = null;
   }
   else
   {
      $retval = $catwatch['adcat'];
   }

   // Ausgabe
   print_r($catwatch);
   return $retval;

}

Im Template sieht es so aus:
Code:

            {catwatch actcat=$var1 adcat=$var2}

            {if catwatch == 30}
               OKAY 30
            {else}
            NICHT 30
            {/if}

Dieser Codeabschnitt wird in einer Schleife abgearbeitet und $var1 und $var2 mit Werten gefüllt. In der catwatch-Funktion will ich einige Prüfungen damit machen, was aber noch nicht integriert ist. Was mich wundert: Wenn der return-Wert von catwatch = 30 ist, trifft
Code:

{if catwatch == 30}

nicht zu. Ich lande immer im else-Zweig. Was ist denn an den beiden 30 so unterschiedlich?? Beide Dateien (Template, PHP-Skript) haben denselben Zeichensatz. Ich habe das Gefühl das ich nicht mit if meine Funktion abfragen darf.

Das ganze Smarty-Gedöns gehört zum Kleinanzeigenskript OxyClassifieds, Oxy benutzt Smarty als Template-Engine. Der Tag {php} ist in Templates nicht erlaubt. Deshalb versuche ich es jetzt mit einem Plugin, scheitere aber an 30 != 30 statt wie erwartet 30 == 30.
Back to top
View user's profile Send private message
Samson
Smarty n00b


Joined: 23 Dec 2016
Posts: 3

PostPosted: Fri Dec 23, 2016 11:56 am    Post subject: Reply with quote

Da die if-Abfrage nicht funktioniert, fülle ich jetzt in meiner Funktion eine Variable ($viewable) und kann die im Template abfragen:
Code:

{catwatch actcat=$var1 adcat=$var2}
{if $viewable == 30}
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Mon Jan 02, 2017 6:55 am    Post subject: Reply with quote

Das es nicht funktioniert ist irgendwie klar. Du lieferst in deiner Funktion zwar etwas zurück (mit return), jedoch nimmst du diesen Wert nirgendswo an.

Also wenn jemand die Funktion hat "BallSchmeissen" und der Ball wird geschmissen, heißt es nicht, dass ihn jemand auch fängt, um dann in den Korb zu schmeißen.

Wenn du dies - so wie du es oben beschrieben hast - machen willst, dann musst du in deiner Smarty Funktion eine Variable zuweisen, also sowas wie:

Code:

$smarty->assign("catwatch", $retval);


dies wäre sozusagen dann mit dem "return" zu ersetzen.

Ansonsten müsstest du in deinem Template (wenn du es mit dem return lassen willst) ein assign verwenden
Back to top
View user's profile Send private message Visit poster's website
Samson
Smarty n00b


Joined: 23 Dec 2016
Posts: 3

PostPosted: Mon Jan 02, 2017 6:57 pm    Post subject: Reply with quote

Danke für den Tip! So etwas wie ein return-Ersatz habe ich mir schon gedacht.
Back to top
View user's profile Send private message
mwaack
Smarty Rookie


Joined: 01 Mar 2017
Posts: 7

PostPosted: Sun May 07, 2017 10:39 am    Post subject: Reply with quote

Du kannst auch eine Klasse an eine Variable binden und dann in einem If Block benutzen.

Code:
class foo {
  function test($argument) {
    .....
    return true;
  }
}

$f = new foo();
$smarty-AssignByRef('foo',$f);


Code:
{if foo->test("test")}
...
{/if}
[/code]
Back to top
View user's profile Send private message
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