Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

cant get Requirements::javascriptTemplate to work


Go to End


4 Posts   1944 Views

Avatar
theoldlr

Community Member, 103 Posts

12 January 2011 at 11:08am

Edited: 12/01/2011 11:10am


function init(){
         parent::init();
        //The Javascript is required in the templates
        //Requirements::javascript("http://maps.google.com/maps/api/js?sensor=false");
        //Requirements::javascript("mysite/javascript/gmap.js");  
        Requirements::javascriptTemplate("mysite/javascript/gmap.js",
            array(
                "gmap_lat" => $this->Latitude,
                "gmap_long" => $this->Longitude,
                "gmap_zoom" => $this->Zoom,
                "gmap_marker" => $this->Marker,
                "gmap_info" => $this->Info,
                "gmap_marker_title" => $this->MarkerTitle,
                "gmap_info_window" => $this->InfoWindow
            )); 
}
gmap.js:


jQuery(document).ready(function(){

            var latlng = new google.maps.LatLng(gmap_lat, gmap_long);
            var myOptions = {
                zoom: gmap_zoom,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            if(gmap_marker == true){
                var map_marker = new google.maps.Marker({
                    position: latlng,
                    map: map,
                    animation: google.maps.Animation.DROP,
                    title: gmap_marker_title    
                });
            }

            if(gmap_info == true){
                var contentString = gmap_info_window;

                var infowindow = new google.maps.InfoWindow({
                    content: contentString
                });

                google.maps.event.addListener(map_marker, 'click', function() {
      infowindow.open(map,map_marker);
    });

            }


});

When viewing page source, no variables are being substituted and I get javascript errors at the first instance of a variable in firebug.

TIA!

Avatar
martimiz

Forum Moderator, 1391 Posts

13 January 2011 at 1:05am

I've not tested this, but looking at the code in Requirements_Backend::javascriptTemplate(), I think these vars in the javascript files are in reality php-like 'placeholders', on which a textual search and replace is performed. Also they should be prefixed by a '$'.

So, supposing $this->lattitude = 1.111 and $this->longitude = 2.222, the following:

var latlng = new google.maps.LatLng('$gmap_lat', '$gmap_long');

would result in

var latlng = new google.maps.LatLng('1.111', '2.222');

Maybe this will work... :-)

Avatar
theoldlr

Community Member, 103 Posts

13 January 2011 at 3:02am

Martimiz,

2nd time you helped me out this week. Thank you, it worked perfectly. I was missing the '$'s in the javascript. Thank you!!

To make this crystal clear for anyone else who comes across this, also note that you can't use variables that contain the name of another because of the string replacement--I had to modify this in the code originally posted. For example:

"gmap_marker" => $this->Marker,
"gmap_marker_title" => $this->MarkerTitle

it will replace $gmap_marker_title with the value of gmap_marker with "_title" at the end.

Avatar
martimiz

Forum Moderator, 1391 Posts

13 January 2011 at 6:03am


Glad to be of help :-)