Skip to content

Commit

Permalink
Merge pull request #5 from bayfrontmedia/dev
Browse files Browse the repository at this point in the history
v2.1.1 updates
  • Loading branch information
robinsonjohn authored Jul 27, 2023
2 parents 8f0c0d6 + b0a385c commit b730a91
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| <code>{{parameter.name&#124;&#124;string}}</code> | Replaced with escaped value from the `$data` array in dot notation or default string if not existing |
| <code>{{!parameter.name&#124;&#124;string}}</code> | 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 |
| <code>{{parameter.name&#124;&#124;default}}</code> | Replaced with escaped value from the `$data` array in dot notation or default value if not existing [*](#note) |
| <code>{{!parameter.name&#124;&#124;default}}</code> | Replaced with unescaped (raw) value from the `$data` array in dot notation or default value 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

Expand Down
16 changes: 8 additions & 8 deletions src/Veil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit b730a91

Please sign in to comment.