Skip to content
Permalink
Browse files

added macros {varType} and {templateType}

  • Loading branch information
dg committed Jan 4, 2020
1 parent 02ab8e4 commit 32cc5a37e2d275f462f10ec794f5b1a4727ccc22
Showing with 99 additions and 0 deletions.
  1. +35 −0 src/Latte/Macros/CoreMacros.php
  2. +28 −0 tests/Latte/CoreMacros.templateType.phpt
  3. +36 −0 tests/Latte/CoreMacros.varType.phpt
@@ -67,6 +67,9 @@ public static function install(Latte\Compiler $compiler): void

$me->addMacro('class', null, null, [$me, 'macroClass']);
$me->addMacro('attr', null, null, [$me, 'macroAttr']);

$me->addMacro('varType', [$me, 'macroVarType'], null, null, self::ALLOWED_IN_HEAD);
$me->addMacro('templateType', [$me, 'macroTemplateType'], null, null, self::ALLOWED_IN_HEAD);
}


@@ -510,4 +513,36 @@ public function macroContentType(MacroNode $node, PhpWriter $writer)
return $writer->write('if (empty($this->global->coreCaptured) && in_array($this->getReferenceType(), ["extends", null], true)) header(%var);', "Content-Type: $node->args");
}
}


/**
* {varType type $var}
*/
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}
*/
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.');
}
}
}
@@ -0,0 +1,28 @@
<?php

/**
* Test: {templateType}
*/

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

Assert::exception(function () use ($latte) {
$latte->compile('{templateType}');
}, Latte\CompileException::class, 'Missing class name in {templateType} macro.');

Assert::exception(function () use ($latte) {
$latte->compile('{if true}{templateType stdClass}{/if}');
}, Latte\CompileException::class, '{templateType} is allowed only in template header.');

Assert::noError(function () use ($latte) {
$latte->compile('{templateType stdClass}');
});
@@ -0,0 +1,36 @@
<?php

/**
* Test: {varType}
*/

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

Assert::exception(function () use ($latte) {
$latte->compile('{varType}');
}, Latte\CompileException::class, 'Unexpected content%a%');

Assert::exception(function () use ($latte) {
$latte->compile('{varType type}');
}, Latte\CompileException::class, 'Unexpected content%a%');

Assert::exception(function () use ($latte) {
$latte->compile('{varType type var}');
}, Latte\CompileException::class, 'Unexpected content%a%');

Assert::exception(function () use ($latte) {
$latte->compile('{varType $var type}');
}, Latte\CompileException::class, 'Unexpected content%a%');

Assert::noError(function () use ($latte) {
$latte->compile('{varType type $var}');
});

0 comments on commit 32cc5a3

Please sign in to comment.
You can’t perform that action at this time.