Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » cant get Requirements::javascriptTemplate to work

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

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

Page: 1
Go to End
Author Topic: 957 Views
  • theoldlr
    Avatar
    Community Member
    103 Posts

    cant get Requirements::javascriptTemplate to work Link to this post

    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!

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: cant get Requirements::javascriptTemplate to work Link to this post

    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...

  • theoldlr
    Avatar
    Community Member
    103 Posts

    Re: cant get Requirements::javascriptTemplate to work Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: cant get Requirements::javascriptTemplate to work Link to this post

    Glad to be of help

    957 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.