From b3218d2535ce80e00c8d9613fc69e70abb0797b7 Mon Sep 17 00:00:00 2001 From: John Robinson Date: Thu, 27 Jul 2023 11:16:35 -0400 Subject: [PATCH 1/2] v2.1.1 updates --- CHANGELOG.md | 8 ++++++++ README.md | 30 +++++++++++++++++------------- src/Veil.php | 16 ++++++++-------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cc21bf..e3ca6c2 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Fixed` for any bug fixes. - `Security` in case of vulnerabilities +## [2.1.1]- 2023.07.27 + +### Changed + +- Updated order template tags are processed to ensure comments are removed first. +- Updated template tags for `$data` array with default value to check if exists on array before returning string. +- Minor code cleanup. + ## [2.1.0]- 2023.04.05 ### Changed diff --git a/README.md b/README.md index e63c3c8..c396d40 100644 --- a/README.md +++ b/README.md @@ -61,19 +61,23 @@ This allows for functionality such as template inheritance (chaining files), inj The following template tags can be used in HTML and view files: -| Tag | Function | -|----------------------------------------------------|--------------------------------------------------------------------------------------------------------------| -| `@use:path/from/base` | Adds contents of another file | -| `@markdown:path/from/base` | Adds markdown of another file as HTML (see [markdown](#markdown)) | -| `@inject:type` | Injects content (see [inject](#inject)) | -| `@section:name` | Defines a section to be placed in a view (see [sections](#sections)) | -| `@place:name` | Places a defined section into the view (see [sections](#sections)) | -| `?@place:name` | Places an optionally defined section into the view (see [sections](#sections)) | -| `{{-- Comment —-}}` | Everything inside comment tags will be ignored and removed | -| `{{parameter.name}}` | Replaced with escaped value from the `$data` array in dot notation | -| `{{!parameter.name}}` | Replaced with unescaped (raw) value from the `$data` array in dot notation | -| {{parameter.name||string}} | Replaced with escaped value from the `$data` array in dot notation or default string if not existing | -| {{!parameter.name||string}} | Replaced with unescaped (raw) value from the `$data` array in dot notation or default string if not existing | +| Tag | Function | +|-----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------| +| `@use:path/from/base` | Adds contents of another file | +| `@markdown:path/from/base` | Adds markdown of another file as HTML (see [markdown](#markdown)) | +| `@inject:type` | Injects content (see [inject](#inject)) | +| `@section:name` | Defines a section to be placed in a view (see [sections](#sections)) | +| `@place:name` | Places a defined section into the view (see [sections](#sections)) | +| `?@place:name` | Places an optionally defined section into the view (see [sections](#sections)) | +| `{{-- Comment —-}}` | Everything inside comment tags will be ignored and removed | +| `{{parameter.name}}` | Replaced with escaped value from the `$data` array in dot notation | +| `{{!parameter.name}}` | Replaced with unescaped (raw) value from the `$data` array in dot notation | +| {{parameter.name||default}} | Replaced with escaped value from the `$data` array in dot notation or default string if not existing [*](#note) | +| {{!parameter.name||default}} | Replaced with unescaped (raw) value from the `$data` array in dot notation or default string if not existing [*](#note) | + +> ##### Note: +> +> The default value can be either a plaintext string, or another key on the `$data` array in dot notation. ##### Sections diff --git a/src/Veil.php b/src/Veil.php index 5d5246e..ea3d283 100644 --- a/src/Veil.php +++ b/src/Veil.php @@ -257,11 +257,15 @@ private function _requireToVar(string $file, /* @noinspection PhpUnusedParameter private function _processTemplateTags(string $html, array $data): string { + // -------------------- Remove comments {{-- COMMENT —-}} -------------------- + + $html = preg_replace("/{{--(.*?)--}}/s", '', $html); + // -------------------- Tag: @use: -------------------- preg_match_all("/@use:\S+/", $html, $tags); // Any non-whitespace - if (isset($tags[0]) && !empty($tags[0])) { // If a tag was found + if (!empty($tags[0])) { // If a tag was found foreach ($tags[0] as $tag) { @@ -290,7 +294,7 @@ private function _processTemplateTags(string $html, array $data): string preg_match_all("/@section:(.*?)@endsection/s", $html, $tags); - if (isset($tags[0]) && !empty($tags[0])) { // If a tag was found + if (!empty($tags[0])) { // If a tag was found foreach ($tags[0] as $tag) { @@ -335,7 +339,7 @@ private function _processTemplateTags(string $html, array $data): string preg_match_all("/@markdown:\S+/", $html, $tags); // Any non-whitespace - if (isset($tags[0]) && !empty($tags[0])) { // If a tag was found + if (!empty($tags[0])) { // If a tag was found foreach ($tags[0] as $tag) { @@ -368,10 +372,6 @@ private function _processTemplateTags(string $html, array $data): string } - // -------------------- Remove comments {{-- COMMENT —-}} -------------------- - - $html = preg_replace("/{{--(.*?)--}}/s", '', $html); - // -------------------- Insert parameters {{in.dot.notation}} -------------------- $dot_data = Arr::dot($data); @@ -394,7 +394,7 @@ private function _processTemplateTags(string $html, array $data): string $html = preg_replace_callback("/{{(.*?)\|\|(.*?)}}/", function ($match) use ($dot_data, $html) { - $replace = Arr::get($dot_data, $match[1], $match[2]); + $replace = Arr::get($dot_data, $match[1], Arr::get($dot_data, $match[2], $match[2])); // Attempt to get default from array before returning string return str_replace([ $match[0], // Unfiltered From b0a385c69b5e36a8ed4ae3ed29f2c66938867c2c Mon Sep 17 00:00:00 2001 From: John Robinson Date: Thu, 27 Jul 2023 11:20:58 -0400 Subject: [PATCH 2/2] README update --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c396d40..e6658d1 100644 --- a/README.md +++ b/README.md @@ -61,19 +61,19 @@ This allows for functionality such as template inheritance (chaining files), inj The following template tags can be used in HTML and view files: -| Tag | Function | -|-----------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------| -| `@use:path/from/base` | Adds contents of another file | -| `@markdown:path/from/base` | Adds markdown of another file as HTML (see [markdown](#markdown)) | -| `@inject:type` | Injects content (see [inject](#inject)) | -| `@section:name` | Defines a section to be placed in a view (see [sections](#sections)) | -| `@place:name` | Places a defined section into the view (see [sections](#sections)) | -| `?@place:name` | Places an optionally defined section into the view (see [sections](#sections)) | -| `{{-- Comment —-}}` | Everything inside comment tags will be ignored and removed | -| `{{parameter.name}}` | Replaced with escaped value from the `$data` array in dot notation | -| `{{!parameter.name}}` | Replaced with unescaped (raw) value from the `$data` array in dot notation | -| {{parameter.name||default}} | Replaced with escaped value from the `$data` array in dot notation or default string if not existing [*](#note) | -| {{!parameter.name||default}} | Replaced with unescaped (raw) value from the `$data` array in dot notation or default string if not existing [*](#note) | +| Tag | Function | +|-----------------------------------------------------|------------------------------------------------------------------------------------------------------------------------| +| `@use:path/from/base` | Adds contents of another file | +| `@markdown:path/from/base` | Adds markdown of another file as HTML (see [markdown](#markdown)) | +| `@inject:type` | Injects content (see [inject](#inject)) | +| `@section:name` | Defines a section to be placed in a view (see [sections](#sections)) | +| `@place:name` | Places a defined section into the view (see [sections](#sections)) | +| `?@place:name` | Places an optionally defined section into the view (see [sections](#sections)) | +| `{{-- Comment —-}}` | Everything inside comment tags will be ignored and removed | +| `{{parameter.name}}` | Replaced with escaped value from the `$data` array in dot notation | +| `{{!parameter.name}}` | Replaced with unescaped (raw) value from the `$data` array in dot notation | +| {{parameter.name||default}} | Replaced with escaped value from the `$data` array in dot notation or default value if not existing [*](#note) | +| {{!parameter.name||default}} | Replaced with unescaped (raw) value from the `$data` array in dot notation or default value if not existing [*](#note) | > ##### Note: >