Skip to content

Commit

Permalink
Merge pull request #15 from vanilla/feature/has-children-function
Browse files Browse the repository at this point in the history
Add the hasChildren() function
  • Loading branch information
tburry committed Oct 25, 2017
2 parents 6d4110c + 3f9b4e0 commit a8d198b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Functions are called using the `functionName()` syntax. Ebi provides a set of de
| floor | Round fractions down. | [floor](https://secure.php.net/manual/en/function.floor.php)
| formatDate | Format a date. | [date_format](https://secure.php.net/manual/en/function.date-format.php)
| formatNumber | Format a number with grouped thousands. | [number_format](https://secure.php.net/manual/en/function.number-format.php)
| hasChildren | Checks if the component has children passed to it. Pass the name of a block to test for that specific child.
| htmlEncode | Convert special characters to HTML entities. | [htmlspecialchars](https://secure.php.net/manual/en/function.htmlspecialchars.php)
| join | Join the elements of an array into a string. | [implode](https://secure.php.net/manual/en/function.implode.php)
| lcase | Lowercase a string. | [strtolower](https://secure.php.net/manual/en/function.strtolower.php), [mb_strtolower](https://secure.php.net/manual/en/function.mb-strtolower.php)
Expand Down
8 changes: 8 additions & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ class Compiler {
public function __construct() {
$this->expressions = new ExpressionLanguage();
$this->expressions->setNamePattern('/[@a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A');
$this->expressions->register(
'hasChildren',
function ($name = null) {
return empty($name) ? 'isset($children[0])' : "isset(\$children[$name ?: 0])";
},
function ($name = null) {
return false;
});
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public function testWithAsBreadcrumbsExample() {
$this->assertEquals('3', $r);
}

/**
* Test the hasChildren function.
*/
public function testHasChildren() {
$r = $this->renderFixture('has-children');
$this->assertEquals('<div>child!</div>', trim($r));
}

/**
*
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/has-children.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x x-component="child-test"><div x-if="hasChildren()" x-children></div></x>
<child-test>child!</child-test>
<child-test />

0 comments on commit a8d198b

Please sign in to comment.