Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2013 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4471616 to your computer and use it in GitHub Desktop.
Save anonymous/4471616 to your computer and use it in GitHub Desktop.
Escaping Silverstripe variables for use as Javascript strings.
<% if GeoJSON %>
<script type="text/javascript">
showGeoJson("$MyGeoJsonFile.Link");
</script>
<% end_if %>
@AshKyd
Copy link

AshKyd commented Jan 7, 2013

Better solution as per Custom Inline Script. Not quite functionally equivalent, but you get the point.

class MapPage_Controller extends Page_Controller {
    function init(){
        parent::init();

        // Load our JSON link, or false if it's not set.
        $GeoJSON = json_encode($this->MyGeoJsonFile() ? $this->MyGeoJsonFile()->Link() : false);

        // Include a custom script, using our vars above.
        Requirements::customScript(<<<JS
            showGeoJson($GeoJSON);
JS
        );
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment