<commit_before># endobox

_Simple PHP template engine._

---

## About

_endobox_ is a really simple template engine that uses native PHP as syntax.

## Highlights

- Simple, concise API
- [Chaining](#chaining) and [Merging](#merging)
- Shared data across templates
- [Nesting](#nesting)
- Native PHP syntax
- [Markdown](https://github.com/erusev/parsedown "using Parsedown") and
[Markdown Extra](https://github.com/erusev/parsedown-extra "using Parsedown Extra") support

## Usage

### Template file extensions

_endobox_ decides how to render a template according to the _file extension_:

- `.html` is rendered as a __static__ template and returned as is.

- `.php` is eval()'d as a __PHP__ template.

- `.md` (`.mdx`) is parsed as a __Markdown__ (Extra) template.

- `.md.php` (`.mdx.php`) is first eval()'d as __PHP__ template then parsed as __Markdown__ (Extra) template.

### Examples

#### _Hello world_

```php
$endobox = new endobox\Engine('path/to/templates');

$box = $endobox('hello'); // omit extension

echo $box->render([ 'subject' => 'world' ]); // assign data directly via render
```

The template `hello.php` could look like this:

```php
<h1>Hello <?php= $subject ?></h1>
```

#### _Chaining_

```php
$first = $endobox('first');
$second = $endobox('second');
$third = $endobox('third');

$first($second)($third); // chaining boxes

echo $first->render();
```

Concatenate some boxes together and print the result.

> Note that data is __not__ shared between chained boxes. (See [Merging](#merging) for more info.)

#### _Assign data_

```php
$box->assign([ 'foo' => 'bar' ]);
```

The array keys will appear as _simple variables_ inside the templates.

> __Make sure to use valid PHP variable names as keys when assigning data!__

The assigned data is _shared_ between all templates of the same box.

```php
echo $box->render([ 'qux' => 'xyz' ]); // this is also possible
```

`render()` accepts an optional data array.

#### _Accessing data from within template_

`template.php`

```php
<p>
<?php= $foo ?>
</p>
```

Data is accessible via simple variables where the variable names correspond to the assigned array keys.

#### _Nesting_

```php
$outer = $endobox('outer');
$inner = $endobox('inner');

$outer->assign([ 'foo' => $inner ]);
```

You can assign another template box like you'd do with simple data.

#### _Merging_

Merging is combining multiple boxes into one single box and make all of their data shared.

```php
$first = $endobox('first')->assign('foo' => 'bar');
$second = $endobox('second');

$first->merge($second);

echo $first->render(); // first second
```

`$first` and `$second` are now merged into one box.

Both the first and second template have now access to the assigned data.

Note that `$first` is now the merged box whereas `$second` remains unchanged.

> The key difference between _merging_ and [_chaining_](#chaining) is that the former will result in one single box
containing the combined templates with all data being shared whereas the latter will only link several boxes together
while each box remains isolated and still has its own data.

##### A shortcut...

```
$box = $endobox('first')('second'); // create box with multiple templates

$box->assign('foo' => 'bar');

echo $box->render();
```

This is equivalent to the code above.

##### Merge a chain

```
$first = $endobox('first');
$second = $endobox('second');
$third = $endobox('third');

$first($second)($third); // chaining

$first->merge(); // merging
```

Merge a chain of boxes into one box.

#### _Markdown_

`template.md.php`
<commit_msg>Some fixes in README<commit_after># endobox

_Simple PHP template engine._

---

## About

_endobox_ is a really simple template engine that uses native PHP as syntax.

## Highlights

- Simple, concise API
- [Chaining](#chaining) and [Merging](#merging)
- Shared data across templates
- [Nesting](#nesting)
- Native PHP syntax
- [Markdown](https://github.com/erusev/parsedown "using Parsedown") and
[Markdown Extra](https://github.com/erusev/parsedown-extra "using Parsedown Extra") support

## Usage

### Template file extensions

_endobox_ decides how to render a template according to the _file extension_:

- `.html` is rendered as a __static__ template and returned as is.

- `.php` is eval()'d as a __PHP__ template.

- `.md` (`.mdx`) is parsed as a __Markdown__ (Extra) template.

- `.md.php` (`.mdx.php`) is first eval()'d as __PHP__ template then parsed as __Markdown__ (Extra) template.

### Examples

#### _Hello world_

```php
$endobox = new endobox\Engine('path/to/templates');

$box = $endobox('hello'); // omit extension

echo $box->render([ 'subject' => 'world' ]); // assign data directly via render
```

The template `hello.php` could look like this:

```php
<h1>Hello <?php= $subject ?></h1>
```

#### _Chaining_

```php
$first = $endobox('first');
$second = $endobox('second');
$third = $endobox('third');

$first($second)($third); // chaining boxes

echo $first->render();
```

Concatenate some boxes together and print the result.

> Note that data is __not__ shared between chained boxes. (See [Merging](#merging) for more info.)

#### _Assign data_

```php
$box->assign([ 'foo' => 'bar' ]);
```

The array keys will appear as _simple variables_ inside the templates.

> __Make sure to use valid PHP variable names as keys when assigning data!__

The assigned data is _shared_ between all templates of the same box.

```php
echo $box->render([ 'qux' => 'xyz' ]); // this is also possible
```

`render()` accepts an optional data array.

#### _Accessing data from within template_

`template.php`

```php
<p>
<?php= $foo ?>
</p>
```

Data is accessible via simple variables where the variable names correspond to the assigned array keys.

#### _Nesting_

```php
$outer = $endobox('outer');
$inner = $endobox('inner');

$outer->assign([ 'foo' => $inner ]);
```

You can assign another template box like you'd do with simple data.

#### _Merging_

Merging is combining multiple boxes into one single box and make all of their data shared.

```php
$first = $endobox('first')->assign('foo' => 'bar');
$second = $endobox('second');

$first->merge($second);

echo $first->render(); // first second
```

`$first` and `$second` are now merged into one box.

Both the first and second template have now access to the assigned data.

Note that `$first` is now the merged box whereas `$second` remains unchanged.

> The key difference between _merging_ and [_chaining_](#chaining) is that the former will result in one single box
containing the combined templates with all data being shared whereas the latter will only link several boxes together
while each box remains isolated and still has its own data.

##### A shortcut...

```php
$box = $endobox('first')('second'); // create box with multiple templates

$box->assign('foo' => 'bar');

echo $box->render();
```

This is equivalent to the code above.

##### Merge a chain

```php
$first = $endobox('first');
$second = $endobox('second');
$third = $endobox('third');

$first($second)($third); // chaining

$first->merge(); // merging
```

Merge a chain of boxes into one box.

#### _Markdown_

`template.md.php`

{'fraction_non_alphanumeric': 0.15203695926081479, 'fraction_numerical': 0.0005599888002239956, 'mean_word_length': 4.4701378254211335, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 55, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1474844', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2190, 'n_words': 855}