Created
February 18, 2016 15:57
-
-
Save josephbergdoll/5d4a9ea7f09aea5a4569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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