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



 
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
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Wed Nov 02, 2016 7:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Thu Nov 03, 2016 6:28 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Thu Nov 03, 2016 8:35 am    Post subject: Reply with quote

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
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Thu Nov 03, 2016 12:26 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Thu Nov 03, 2016 12:30 pm    Post subject: Reply with quote

Hallo Grizzly,

danke für den Hinweis. Das habe ich auch schon versucht. Leider ohne Erfolg.

Viele Grüße
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Thu Nov 03, 2016 12:42 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Thu Nov 03, 2016 3:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Nov 03, 2016 4:36 pm    Post subject: Reply with quote

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
View user's profile Send private message
robbo2000
Smarty Rookie


Joined: 02 Nov 2016
Posts: 14

PostPosted: Fri Nov 11, 2016 7:47 pm    Post subject: Reply with quote

Thank you. Works fine, too.
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