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

Array Key überprüfen

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


Joined: 01 Jan 2015
Posts: 3

PostPosted: Thu Jan 01, 2015 3:34 pm    Post subject: Array Key überprüfen Reply with quote

Hallo zusammen,

ich möchte mit Smarty überprüfen, ob in einem Array ein Key existiert. Mein Aufbau sieht im Moment so aus:

Ich habe ein dreidimensionales $array, wo in der ersten Dimension Zahlen eingetragen sind. Ich habe z.B.: $array['1'], $array['17'], $array['19']
$array bekomme ich von einer PHP-Datei, die ich mit $smarty->assign zugewiesen habe.
Code:

{counter start=-1 skip=1 print=false assign=count}
{section name=seiten start=0 loop=24}
{counter}
<div>{$count}</div>
{/section}


Jetzt möchte ich die Anzeige z.B. nur haben, wenn der Key im Array auch existiert. Versucht habe ich folgendes:

Code:

{counter start=-1 skip=1 print=false assign=count}
{section name=seiten start=0 loop=24}
{counter}
{if $count|array_key_exists:$array}
<div>{$count}</div>
{/if}
{/section}


Dies gibt mir jetzt allerdings keine Ausgabe. Dann habe ich noch versucht eine Eigene Funktion zu schreiben:


Code:
function smarty_modifier_lookup($count, $array) {
  if (array_key_exists($count, $array)) {
    return true;
  }else{
    return false;
  }
}

Code:

{if $count|lookup:$array}

Aber damit funktioniert es auch nicht. Dann habe ich mir aus spaß die Funktion umgebaut, um zu sehen, was bei der Funktion von $count ankommt:

Code:
function smarty_modifier_lookup($count, $array) {
return $count;
}

Code:

{$count|lookup:$array}


Die Ausgabe war seltsamerweise immer die Zahl 7. Wenn ich die Funktion in Smarty anders aufrufe, nämlich ohne dem Array:

Code:

{$count|lookup}


Bekomme ich die richtige Zahl ausgegeben, also 0 - 23. Ich weiß jetzt persönlich nicht mehr weiter und ich hoffe, mir kann von euch jemand helfen. Vielen Dank schon mal Smile
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jan 01, 2015 3:56 pm    Post subject: Reply with quote

1. Don't use aray_key_exists(), unless you want to test for keys with null values.
Use isset(). It's faster.
2. Check, if you're actually sending the right variables to template.
In fact, all the "if the key exists in source data" are best evaluated on your code side, rather than in template.
So template could just render the data passed to it without implementing half of the business logic on top of your code.


Last edited by AnrDaemon on Thu Jan 01, 2015 4:02 pm; edited 1 time in total
Back to top
View user's profile Send private message
marcelb
Smarty n00b


Joined: 01 Jan 2015
Posts: 3

PostPosted: Thu Jan 01, 2015 4:00 pm    Post subject: Reply with quote

I also tried isset() but it doesn't work.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jan 01, 2015 4:04 pm    Post subject: Reply with quote

Sorry, I was catching up with translation and editing the answer. Please check the edited part.
Back to top
View user's profile Send private message
marcelb
Smarty n00b


Joined: 01 Jan 2015
Posts: 3

PostPosted: Thu Jan 01, 2015 4:25 pm    Post subject: Reply with quote

Thank you for reply.
Yes, I send the right variables to the Template.
You are right, I will evaluate the key on the code Side.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Jan 01, 2015 11:41 pm    Post subject: Reply with quote

Grundsätzlich funktioniert die Lösung mit array_key_exists() und isset().
Daher enthält $array wohl nicht die erwarteten Werte.

Setze mal $smarty->debugging = true; und prüfe was das template wirklich sieht.
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