Skip to content

Instantly share code, notes, and snippets.

@enumag
Last active August 29, 2015 14:19
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 enumag/bc8e56649f80e5aab75e to your computer and use it in GitHub Desktop.
Save enumag/bc8e56649f80e5aab75e to your computer and use it in GitHub Desktop.
ResourceMacro
<?php
namespace App\Latte;
use Latte\Compiler;
use Latte\Macros\MacroSet;
class ResourceMacro extends MacroSet
{
/**
* @param Compiler $compiler
*/
public static function install(Compiler $compiler)
{
$self = new static($compiler);
$self->addMacro('resource', 'echo ' . __CLASS__ . '::createHtmlTag(%node.word, $control->getPresenter()->getContext()->getParameters()["wwwDir"]);');
}
public static function createHtmlTag($file, $wwwDir)
{
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension === 'css') {
return '<link rel="stylesheet" href="' . $file . '?' . filemtime($wwwDir . $file) . '">';
} elseif ($extension === 'js') {
return '<script type="text/javascript" src="' . $file . '?' . filemtime($wwwDir . $file) . '"></script>';
}
throw new \Exception("Unknown file extension '$extension'.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment