Skip to content

Commit 2c1bac0

Browse files
committed
add comment suppoer
1 parent 1b21f97 commit 2c1bac0

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Enhance your files with variables, conditional blocks and PHP functions as filte
1616
- [Templates Syntax Highlighting for VS Code](#templates-syntax-highlighting-for-vs-code)
1717
- [Usage](#usage)
1818
- [Template syntax](#template-syntax)
19+
- [Comments](#comments)
1920
- [Conditions](#conditions)
2021
- [Variables](#variables)
2122
- [Default values](#default-values)
@@ -37,6 +38,7 @@ Enhance your files with variables, conditional blocks and PHP functions as filte
3738
- Use [built-in filters](#built-in-filters) or provide custom functions
3839
- Use `<% if %>` blocks to conditionally parse the template
3940
- Use `<% else %>` blocks to provide an alternative content if the condition is not met
41+
- Include `<# comments #>` in the template files that are removed after patsing
4042
- Parse template files to string
4143
- Parse the entire file as a string
4244
- Provide a custom regex pattern to parse functions to use a custom syntax
@@ -69,6 +71,16 @@ To get syntax highlighting for template files (highlight `<% variable|placeholde
6971

7072
### Template syntax
7173

74+
#### Comments
75+
76+
You can use comments in your templates by using the following syntax:
77+
78+
```
79+
<# This is a comment #>
80+
```
81+
82+
Comments are ignored by the parser and will be removed from the parsed output.
83+
7284
#### Conditions
7385

7486
You can use conditions in your templates by using the `<% if %>` and `<% endif %>` tags. The condition must be a valid PHP expression that will be evaluated and if it returns `true`, the content between the tags will be included in the final output.

src/Parsem/Parser.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ final class Parser
2424
* 0: <% var='default'|filter:10,'arg','another' %> --> (full match)
2525
* 1: var --> (only variable name)
2626
* 2: ='default' --> (default value)
27-
* 3: |filter:10,'arg','another' --> (filter with args)
27+
* 3: |filter:10,'arg','another' --> (filter with args)
2828
* 4: filter --> (only filter name)
2929
*/
30-
public const string VARIABLE_PATTERN = '/<%\s?((?!endif|else)[a-zA-Z0-9_]+)(=.*?)?(\|([a-zA-Z0-9_]+?)(?:\:(?:(?:\\?\'|\\?")?.?(?:\\?\'|\\?")?,?)+?)*?)?\s?%>/m';
30+
public const VARIABLE_PATTERN = '/<%\s?((?!endif|else)[a-zA-Z0-9_]+)(=.*?)?(\|([a-zA-Z0-9_]+?)(?:\:(?:(?:\\?\'|\\?")?.?(?:\\?\'|\\?")?,?)+?)*?)?\s?%>/m';
3131

3232
/**
3333
* Matches:
@@ -39,10 +39,17 @@ final class Parser
3939
* 5: > --> (operator)
4040
* 6: 10 --> (right side)
4141
*/
42-
public const string CONDITION_PATTERN = '/(?<all><%\s?if\s(?<condition>(?<negation>!?)(?<left>\S+?)\s?(?<right>(?<operator>(?:<=|<|===|==|>=|>|!==|!=))\s?(?<value>.+?))?)\s?%>\n?)/m';
42+
public const CONDITION_PATTERN = '/(?<all><%\s?if\s(?<condition>(?<negation>!?)(?<left>\S+?)\s?(?<right>(?<operator>(?:<=|<|===|==|>=|>|!==|!=))\s?(?<value>.+?))?)\s?%>\n?)/m';
43+
44+
/**
45+
* Matches:
46+
* 0: <# comment #> --> (full match)
47+
* 1: comment --> (only comment)
48+
*/
49+
public const COMMENT_PATTERN = '/<#\s?(.+?)\s?#>/m';
4350

4451
/** @internal */
45-
private const string LITERALLY_NULL = '⚠︎__:-␀LITERALLY_NULL␀-:__⚠︎';
52+
private const LITERALLY_NULL = '⚠︎__:-␀LITERALLY_NULL␀-:__⚠︎';
4653

4754
/**
4855
* Parses a file to a PHP object, replacing all template variables with the provided `$arguments` values.
@@ -77,6 +84,7 @@ public static function parseString(mixed $string, array $arguments = [], bool $s
7784
if (!is_string($string)) return $string;
7885

7986
$string = static::parseConditions($string, $arguments);
87+
$string = static::removeComments($string);
8088

8189
preg_match_all($pattern ?? static::VARIABLE_PATTERN, $string, $matches);
8290
$args = [];
@@ -140,7 +148,7 @@ public static function parseConditions(string $string, array $arguments = [], in
140148
if (!$endMatches) {
141149
throw new RuntimeException("Missing <% endif %> tag.");
142150
}
143-
151+
144152
$conditionEnd = $endMatches[0][1];
145153
$replaceLength = $conditionEnd - $conditionStart + strlen($endMatches[0][0]);
146154

@@ -155,6 +163,11 @@ public static function parseConditions(string $string, array $arguments = [], in
155163
return static::parseConditions($string, $arguments, $conditionStart);
156164
}
157165

166+
public static function removeComments(string $string): string
167+
{
168+
return preg_replace(static::COMMENT_PATTERN, '', $string);
169+
}
170+
158171
/**
159172
* Converts a YAML, JSON or NEON file to a corresponding PHP object, replacing all template variables with the provided `$arguments` values.
160173
* @deprecated 3.2.0 __Will be removed in the next version.__ This was used for parsing JSON/YAML/NEON templates in v1 and is no longer needed in v2 and later.
@@ -272,7 +285,7 @@ public static function isValid(string $filename, ?string $contents = null): bool
272285
throw new RuntimeException("Failed to get template schema from remote server: " . $e->getMessage());
273286
}
274287
}
275-
288+
276289
return $validator->validate($parsed, $schema)->isValid();
277290
}
278291

@@ -412,7 +425,7 @@ protected static function getConditionResult(array $matches, array $arguments =
412425
if ($negation === '!') {
413426
$left = !$left;
414427
}
415-
428+
416429
if (isset($right)) {
417430
$right = static::transformConditionValue($right, $arguments);
418431
$result = static::getResultByOperator($left, $operator, $right);
@@ -529,7 +542,7 @@ protected static function parseElseTag(string &$string, int $elseStart, int $els
529542
$conditionEnd = $endMatches[0][1];
530543

531544
$elseBlock = substr($string, $elseStart + $elseTagLength, $conditionEnd - $elseStart - $elseTagLength);
532-
545+
533546
return $elseBlock;
534547
}
535548

0 commit comments

Comments
 (0)