Created
February 14, 2020 15:07
-
-
Save milo/cc56677e670c4f1434cb884ca3f503f5 to your computer and use it in GitHub Desktop.
Latte 2.7 macros bacported for Latte 2.6
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
# Installation | |
latte: | |
macros: | |
- App\UI\LatteBackportMacros::install |
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 | |
/** | |
* I'm excited from PhpStorm Latte plugin update and support of new {varType} and {templateType} macros prepared for | |
* a new Latte 2.7 release. But I'm still using Latte 2.6. This class is a temporary backport of this macros before | |
* I upgrade to 2.7. | |
* | |
* About plugin updates: https://forum.nette.org/en/32907-upgrades-in-latte-plugin-for-phpstorm | |
*/ | |
namespace App\UI; | |
use Latte\CompileException; | |
use Latte\Compiler; | |
use Latte\Helpers; | |
use Latte\MacroNode; | |
use Latte\Macros\MacroSet; | |
use Latte\PhpWriter; | |
use Nette; | |
class LatteBackportMacros extends MacroSet | |
{ | |
use Nette\SmartObject; | |
public static function install(Compiler $compiler) | |
{ | |
$me = $me = new static($compiler); | |
$me->addMacro('varType', [$me, 'macroVarType'], null, null, self::ALLOWED_IN_HEAD); | |
$me->addMacro('templateType', [$me, 'macroTemplateType'], null, null, self::ALLOWED_IN_HEAD); | |
} | |
/** | |
* {varType type $var} | |
* @author https://github.com/nette/latte/commit/4210d5edafdfa8aafd8ee8ee1fe43706a17fe137 | |
*/ | |
public function macroVarType(MacroNode $node, PhpWriter $writer) | |
{ | |
if ($node->modifiers) { | |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); | |
} | |
$type = $node->tokenizer->fetchWord(); | |
$variable = $node->tokenizer->fetchWord(); | |
if (!$type || !$variable || !Helpers::startsWith($variable, '$')) { | |
throw new CompileException('Unexpected content, expecting {varType type $var}.'); | |
} | |
} | |
/** | |
* {templateType ClassName} | |
* @author https://github.com/nette/latte/commit/4210d5edafdfa8aafd8ee8ee1fe43706a17fe137 | |
*/ | |
public function macroTemplateType(MacroNode $node, PhpWriter $writer) | |
{ | |
if (!$this->getCompiler()->isInHead()) { | |
throw new CompileException($node->getNotation() . ' is allowed only in template header.'); | |
} elseif ($node->modifiers) { | |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); | |
} elseif (!($type = $node->tokenizer->fetchWord())) { | |
throw new CompileException('Missing class name in {templateType} macro.'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment