|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
marcelb Smarty n00b
Joined: 01 Jan 2015 Posts: 3
|
Posted: Thu Jan 01, 2015 3:34 pm Post subject: Array Key überprüfen |
|
|
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:
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 |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Jan 01, 2015 3:56 pm Post subject: |
|
|
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 |
|
marcelb Smarty n00b
Joined: 01 Jan 2015 Posts: 3
|
Posted: Thu Jan 01, 2015 4:00 pm Post subject: |
|
|
I also tried isset() but it doesn't work. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Jan 01, 2015 4:04 pm Post subject: |
|
|
Sorry, I was catching up with translation and editing the answer. Please check the edited part. |
|
Back to top |
|
marcelb Smarty n00b
Joined: 01 Jan 2015 Posts: 3
|
Posted: Thu Jan 01, 2015 4:25 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jan 01, 2015 11:41 pm Post subject: |
|
|
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 |
|
|
|
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
|