Skip to content

Commit

Permalink
Merge branch 'release/4.0.0-beta.2' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 12, 2022
2 parents 3e5ad51 + 476a86a commit 06e8354
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Template Comments Changelog

## 4.0.0-beta.2 - 2022.03.12

### Added

* Added `excludeBlocksThatContain` config setting to allow excluding of template comments based on the `{% block %}` name

## 4.0.0-beta.1 - 2022.03.12

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-templatecomments",
"description": "Adds a HTML comment with performance timings to demarcate `{% block %}`s and each Twig template that is included or extended.",
"type": "craft-plugin",
"version": "4.0.0-beta.1",
"version": "4.0.0-beta.2",
"keywords": [
"craftcms",
"craft-plugin",
Expand Down
9 changes: 9 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
*/
'onlyCommentsInDevMode' => true,

/**
* @var array Don't add comments to template blocks that contain these strings (case-insensitive)
*/
'excludeBlocksThatContain' => [
'css',
'js',
'javascript',
],

/**
* @deprecated This is no longer used
* @var bool Whether or not to show comments for templates that are include'd
Expand Down
10 changes: 10 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ class Settings extends Model
*/
public bool $onlyCommentsInDevMode = true;

/**
* @var array Don't add comments to template blocks that contain these strings (case-insensitive)
*/
public array $excludeBlocksThatContain = [
'css',
'js',
'javascript',
];

/**
* @deprecated This is no longer used
* @var bool Whether or not to show comments for templates that are include'd
Expand Down Expand Up @@ -81,6 +90,7 @@ public function rules(): array
],
[
[
'excludeBlocksThatContain',
'allowedTemplateSuffixes',
],
ArrayValidator::class
Expand Down
13 changes: 12 additions & 1 deletion src/web/twig/tokenparsers/CommentBlockTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

namespace nystudio107\templatecomments\web\twig\tokenparsers;

use nystudio107\templatecomments\TemplateComments;
use nystudio107\templatecomments\web\twig\nodes\CommentBlockNode;
use Twig\Error\SyntaxError;
use Twig\Node\BlockNode;
use Twig\Node\BlockReferenceNode;
use Twig\Node\Node;
use Twig\Node\PrintNode;
Expand Down Expand Up @@ -41,7 +43,16 @@ public function parse(Token $token): BlockReferenceNode
throw new SyntaxError(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext());
}

$this->parser->setBlock($name, $block = new CommentBlockNode($name, new Node(array()), $lineno));
// Exclude certain blocks from being CommentBlockNodes
$blockClass = CommentBlockNode::class;
$settings = TemplateComments::$settings;
foreach ($settings->excludeBlocksThatContain as $excludeString) {
if (stripos($name, $excludeString) !== false) {
$blockClass = BlockNode::class;
}
}

$this->parser->setBlock($name, $block = new $blockClass($name, new Node(array()), $lineno));
$this->parser->pushLocalScope();
$this->parser->pushBlockStack($name);

Expand Down

0 comments on commit 06e8354

Please sign in to comment.