Skip to content

Commit

Permalink
Merge pull request #2 from xp-forge/feature/logging
Browse files Browse the repository at this point in the history
Logging
  • Loading branch information
thekid authored Feb 14, 2021
2 parents b2fc8e1 + c9a1cc1 commit c03cbab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Handlebars for XP web frontends change log

## ?.?.? / ????-??-??

## 0.4.0 / 2021-02-14

* Merged PR #2: Logging. Using the development webserver, this will show
the debug page - for production, the content will be written to the
server's standard output.
(@thekid)

## 0.3.0 / 2021-02-13

* Add support for milliseconds resolution in timestamps to `date` helper,
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@ The `date` helper accepts anything the `util.Date` class accepts as constructor
{{date 1613209181279 timestamp="ms"}}
{{date created}}
{{date created format="d.m.Y"}}
```
```

### Logging

The `log` helper will echo the arguments passed to it:

```handlebars
{{log user}}
{{log "User profile:" user}}
```

When using the development webserver, this shows the debug page:

![Debug page](https://user-images.githubusercontent.com/696742/107873960-89cdc800-6eb6-11eb-954b-8b00324cce74.png)

In production environments, logs will end up on the server's standard output:

![Console output](https://user-images.githubusercontent.com/696742/107874105-838c1b80-6eb7-11eb-8c7e-ee257ef1d92d.png)
9 changes: 8 additions & 1 deletion src/main/php/web/frontend/Handlebars.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use com\github\mustache\TemplateLoader;
use com\handlebarsjs\{HandlebarsEngine, FilesIn};
use util\Date;
use util\{Date, Objects};

/**
* Handlebars-based template engine for web frontends.
Expand All @@ -22,6 +22,13 @@ class Handlebars implements Templates {
public function __construct($templates) {
$this->backing= (new HandlebarsEngine())
->withTemplates($templates instanceof TemplateLoader ? $templates : new FilesIn($templates))
->withLogger(function($args) {
echo ' ';
foreach ($args as $arg) {
echo is_string($arg) ? $arg : Objects::stringOf($arg, ' '), ' ';
}
echo "\n";
})
->withHelper('encode', function($in, $context, $options) {
return rawurlencode($options[0] ?? '');
})
Expand Down
9 changes: 9 additions & 0 deletions src/test/php/web/frontend/unittest/HandlebarsTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,13 @@ public function all($expr, $expected) {
'empty' => [],
]));
}

#[Test]
public function logging_echoes_content() {
ob_start();
$this->transform('{{log "User:" user}}', ['user' => ['id' => 'test']]);
$logged= ob_get_clean();

Assert::equals(" User: [\n id => \"test\"\n ] \n", $logged);
}
}

0 comments on commit c03cbab

Please sign in to comment.