diff --git a/CHANGELOG.md b/CHANGELOG.md index f9c9515..fe1cd6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 9cb9ca6..90e92e9 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/config.php b/src/config.php index aafb6fd..2c72778 100644 --- a/src/config.php +++ b/src/config.php @@ -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 diff --git a/src/models/Settings.php b/src/models/Settings.php index 6a07439..4a3d6c8 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -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 @@ -81,6 +90,7 @@ public function rules(): array ], [ [ + 'excludeBlocksThatContain', 'allowedTemplateSuffixes', ], ArrayValidator::class diff --git a/src/web/twig/tokenparsers/CommentBlockTokenParser.php b/src/web/twig/tokenparsers/CommentBlockTokenParser.php index cc5b406..6b4b399 100644 --- a/src/web/twig/tokenparsers/CommentBlockTokenParser.php +++ b/src/web/twig/tokenparsers/CommentBlockTokenParser.php @@ -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; @@ -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);