Skip to content

Instantly share code, notes, and snippets.

@josephbergdoll
Created February 18, 2016 15:57
Show Gist options
  • Save josephbergdoll/5d4a9ea7f09aea5a4569 to your computer and use it in GitHub Desktop.
Save josephbergdoll/5d4a9ea7f09aea5a4569 to your computer and use it in GitHub Desktop.
<?php
// LiveReload script injection for those using grunt-watch
// This way you don't have to go to the localhost port
// Call this function once in the <head> of your layout.twig via {{ liveReload }}
// You can disable it by either removing or passing {{ liveReload(false) }}
// Add the local domain you are developing from by passing it as the second argument:
// {{ liveReload(true, 'example.dev') }}
namespace Craft;
class LiveReloadTwigExtension extends \Twig_Extension {
public function getName() {
return 'LiveReload Script Injection';
}
public function getFunctions() {
return array(
'liveReload' => new \Twig_Function_Method($this, 'liveReload', array('is_safe' => array('html'))),
);
}
public function liveReload($enable = true, $domain = null) {
if ($enable == true) {
$whitelist = array(
'127.0.0.1',
'::1',
'localhost',
);
if ($domain != null) {
$whitelist[] = $domain;
}
if(in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
echo '<script src="http://localhost:35729/livereload.js?snipver=1"></script>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment