Skip to content

Commit

Permalink
update: add more tests and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 15, 2021
1 parent c81ed3f commit 3aac8e1
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 4 deletions.
71 changes: 69 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ The following statements are the same, can be used to print out variable values
{{ echo $name }}
```

More:

```php
{{ $name ?: 'inhere' }}
{{ $age > 20 ? '20+' : '<= 20' }}
```

> By default, the output result will be automatically processed through `htmlspecialchars`,
> unless disabled or manually used `raw` filter
Expand All @@ -130,15 +137,75 @@ First value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1
```

**if blocks**

only `if`:

```php
{{ if ($name !== '') }}
hi, my name is {{ $name }}
{{ endif }}
```

`if else`:

```php
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
```

`if...elseif...else`:

```php
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 50) }}
age >= 50.
{{ elseif ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
```

**for/foreach blocks**

`foreach`:

```php
tags:

{{ foreach($tags as $tag) }}
- {{ $tag }}

{{ endforeach }}
```

with keys:

```php
tags:

{{ foreach($tags as $index => $tag) }}
{{ $index }}. {{ $tag }}

{{ endforeach }}
```

**add comments**

```text
```php
{{# comments ... #}}{{ $name }} // inhere
```

multi lines:

```text
```php
{{#
this
comments
Expand Down
69 changes: 68 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ $t->addDirective($name, $handler);
{{ echo $name }}
```

更多:

```php
{{ $name ?: 'inhere' }}
{{ $age > 20 ? '20+' : '<= 20' }}
```

> 默认会自动通过 `htmlspecialchars` 将输出结果进行处理,除非设置了禁用或者手动使用 `raw` 过滤器
- 设置禁用输出过滤 `$t->disableEchoFilter()`
Expand All @@ -132,9 +139,69 @@ first value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1
```

**if 语句块**

`if` 语句:

```php
{{ if ($name !== '') }}
hi, my name is {{ $name }}
{{ endif }}
```

`if else` 语句:

```php
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
```

`if...elseif...else` 语句:

```php
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 50) }}
age >= 50.
{{ elseif ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
```

**for/foreach 语句块**

`foreach`:

```php
tags:

{{ foreach($tags as $tag) }}
- {{ $tag }}

{{ endforeach }}
```

with keys:

```php
tags:

{{ foreach($tags as $index => $tag) }}
{{ $index }}. {{ $tag }}

{{ endforeach }}
```

**模板中添加注释**

`{{# XX #}}` 包裹的内容将会当做注释忽略
`{{#``#}}` 包裹的内容将会当做注释忽略

```text
{{# comments ... #}}{{ $name }} // inhere
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Token
self::T_IF,
self::T_ELSEIF,
self::T_ELSE,
self::T_FOR,
self::T_FOREACH,
self::T_CASE,
self::T_SWITCH
Expand Down
40 changes: 39 additions & 1 deletion test/Compiler/PregCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,45 @@ public function testCompile_if_block():void
}
}

public function testCompileCode_ml_define():void
public function testCompile_for():void
{
$p = new PregCompiler();

$code = <<<'CODE'
{{ for ($i=0; $i< 5; $i++) }}
line: {{ $i }}
{{ endfor }}
CODE;
$compiled = $p->compile($code);
$this->assertEquals(<<<'CODE'
<?php for ($i=0; $i< 5; $i++): ?>
line: <?= $i ?>
<?php endfor ?>
CODE
,$compiled);
}

public function testCompile_foreach():void
{
$p = new PregCompiler();

$code = <<<'CODE'
{{ foreach($tags as $tag) }}
- {{ $tag }}
{{ endforeach }}
CODE;
$compiled = $p->compile($code);
$this->assertEquals(<<<'CODE'
<?php foreach($tags as $tag): ?>
- <?= $tag ?>
<?php endforeach ?>
CODE
,$compiled);
}

public function testCompile_ml_define():void
{
$p = new PregCompiler();

Expand Down

0 comments on commit 3aac8e1

Please sign in to comment.