View previous topic :: View next topic |
Author |
Message |
robbo2000 Smarty Rookie
Joined: 02 Nov 2016 Posts: 14
|
Posted: Wed Nov 02, 2016 7:24 pm Post subject: |
|
|
Hallo,
ich möchte eine Google Karte mit mehreren Markern auf meiner Webseite implementieren. Die Daten für die Marker kommen aus der Datenbank (ca. 200 Einträge) und werden an das Template als mehrdimensionales Array übertragen (ID, name, Ort, lat, lng). Mir gelingt es aber nicht dieses Array in Javascript zu verwenden, das ich in meinem Template implementiert habe.
Habe schon verschiedene Ansätze versucht, bin aber immer gescheitert, wie z. B. mit sowas in der Art:
Code: |
var locations = new Array();
locations = {$arraylist};
oder auch
locations = {$arraylist|json_encode};
|
Im Forum bin ich auch immer wieder über eine Google Maps API für Smarty gestolpert. Ist das noch aktuell? Kann mit bitte jemand zu den beiden Punkten weiter helfen?
Ich setzte das aktuelle Smarty 3.1.30 ein
Viele Grüße
Robbo |
|
Back to top |
|
Grizzly Smarty Pro
Joined: 15 Apr 2011 Posts: 172 Location: Germany
|
Posted: Thu Nov 03, 2016 6:28 am Post subject: |
|
|
Was genau kommt denn dann bei "locations" raus (z. B. in der Konsole über console.log)?
Außerdem ist es zu empfehlen den Smarty und JS Code mittels
Code: | {literal}Hier dein JS Code{/literal}{HierDeinSmartyCode} |
zu trennen.
Also so:
Code: |
<script>
{literal}
var locations = {/literal}{$arraylist|json_encode}{literal};
{/literal}
</script>
|
|
|
Back to top |
|
robbo2000 Smarty Rookie
Joined: 02 Nov 2016 Posts: 14
|
Posted: Thu Nov 03, 2016 8:35 am Post subject: |
|
|
Guten Morgen Grizzly,
vielen Dank für deine schnelle Antwort und Entschuldigung, dass mein Post keinen Betreff hat. Dieser ist immer wieder gelöscht worden.
Zu meinem Code. Das hier...
Code: |
<script type="text/javascript">
{literal}
var locations = {/literal}{$allCoords|json_encode}{literal};
console.log(locations);
{/literal}
</script>
|
...führt zu folgender Meldung:
Code: |
SyntaxError: expected expression, got ';'
var locations = ;
|
Viele Grüße & einen schönen Tag! |
|
Back to top |
|
Grizzly Smarty Pro
Joined: 15 Apr 2011 Posts: 172 Location: Germany
|
Posted: Thu Nov 03, 2016 12:26 pm Post subject: |
|
|
Hallo,
versuch mal:
Code: |
<script type="text/javascript">
{literal}
var locations = {/literal}{$allCoords|@json_encode}{literal};
console.log(locations);
{/literal}
</script>
|
es ändert sich bzw. es wurde hinzugefügt das @-Zeichen also sprich: {$allCoords|@json_encode} |
|
Back to top |
|
robbo2000 Smarty Rookie
Joined: 02 Nov 2016 Posts: 14
|
Posted: Thu Nov 03, 2016 12:30 pm Post subject: |
|
|
Hallo Grizzly,
danke für den Hinweis. Das habe ich auch schon versucht. Leider ohne Erfolg.
Viele Grüße |
|
Back to top |
|
Grizzly Smarty Pro
Joined: 15 Apr 2011 Posts: 172 Location: Germany
|
Posted: Thu Nov 03, 2016 12:42 pm Post subject: |
|
|
Was ist denn in dem $allCoords drinnen, ist es überhaupt befüllt?
Was kommt raus, wenn Du im PHP-Code einfach die encode Methode aufrufst und den output in eine Variabel speicherst, welches Du an das template übergibst!? |
|
Back to top |
|
robbo2000 Smarty Rookie
Joined: 02 Nov 2016 Posts: 14
|
Posted: Thu Nov 03, 2016 3:45 pm Post subject: |
|
|
Hallo Grizzly,
Vielen Dank für deine Hilfe. Jetzt habe ich es hinbekommen.
Gelernt habe ich folgendes:
Javascript kann nicht mit PHP-Arrays arbeiten, sondern benötigt JSON.
JSON hat nicht funktioniert wegen falschem Zeichensatz. Wenn ich PDO entsprechend einstelle, dann kann ich das Query-Ergebnis als JSON ans Template übertragen und dann in Javascript weiter verarbeiten.
{Literal} kann ich scheinbar weglassen.
Weiter geht’s. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Nov 03, 2016 4:36 pm Post subject: |
|
|
Pardon my interruption, but forcibly preventing errors is the worst possible idea.
OP's problem seems to be that his $allCoords is not always populated, thus producing empty output from json_encode().
You might get better results with JSON_FORCE_OBJECT, and/or you will need a check if your $allCoords is actually an array.
Encoding non-array values with json_encode is possible, but not always portable. |
|
Back to top |
|
robbo2000 Smarty Rookie
Joined: 02 Nov 2016 Posts: 14
|
Posted: Fri Nov 11, 2016 7:47 pm Post subject: |
|
|
Thank you. Works fine, too. |
|
Back to top |
|
|