Skip to content

Commit

Permalink
Merge pull request #30 from xp-forge/feature/inherit-partials
Browse files Browse the repository at this point in the history
Add possibility to inherit partials
  • Loading branch information
thekid authored Oct 29, 2023
2 parents 94830df + 0e53723 commit 922e962
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/php/com/handlebarsjs/Nodes.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
use com\github\mustache\NodeList;
use lang\IllegalArgumentException;

/** @test com.handlebarsjs.unittest.NodesTest */
class Nodes extends NodeList {
private $partials= [];

/**
* Declares a partial and returns the nodes associated with the name.
*
* @param string|com.handlebarsjs.Quoted $partial
* @param ?com.github.mustache.NodeList $nodes
* @return com.github.mustache.NodeList
* @param ?parent|self $nodes
* @return self
* @throws lang.IllegalArgumentException
*/
public function declare($partial, $nodes= null) {
Expand All @@ -23,18 +24,33 @@ public function declare($partial, $nodes= null) {
throw new IllegalArgumentException('Partial names must be strings or Quoted instances, have '.typeof($partial));
}

return $this->partials[$name]= $nodes ?? new NodeList();
if ($nodes instanceof parent) {
return $this->partials[$name]= new self($nodes->nodes);
} else {
return $this->partials[$name]= $nodes ?? new self();
}
}

/**
* Inherits partials from a given parent
*
* @param self $parent
* @return self
*/
public function inheriting(self $parent) {
$this->partials= $parent->partials;
return $this;
}

/** @return [:com.github.mustache.NodeList] */
/** @return [:self] */
public function partials() { return $this->partials; }

/**
* Returns a partial with a given name, or NULL if this partial does
* not exist.
*
* @param string $partial
* @return ?com.github.mustache.NodeList
* @return ?self
*/
public function partial($partial) {
return $this->partials[$partial] ?? null;
Expand Down
48 changes: 48 additions & 0 deletions src/test/php/com/handlebarsjs/unittest/NodesTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php namespace com\handlebarsjs\unittest;

use com\github\mustache\VariableNode;
use com\handlebarsjs\Nodes;
use test\{Assert, Before, Test};

class NodesTest {
private $partial;

#[Before]
public function partial() {
$this->partial= new Nodes([new VariableNode('test')]);
}

#[Test]
public function can_create() {
new Nodes();
}

#[Test]
public function partial_null() {
Assert::null((new Nodes())->partial('test'));
}

#[Test]
public function partials_empty() {
Assert::equals([], (new Nodes())->partials());
}

#[Test]
public function declare_partial() {
$fixture= new Nodes();
$fixture->declare('test', $this->partial);

Assert::equals($this->partial, $fixture->partial('test'));
Assert::equals(['test' => $this->partial], $fixture->partials());
}

#[Test]
public function inheriting_partials() {
$parent= new Nodes();
$parent->declare('test', $this->partial);
$child= (new Nodes())->inheriting($parent);

Assert::equals($this->partial, $child->partial('test'));
Assert::equals(['test' => $this->partial], $child->partials());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public function template() {
Assert::equals($template, $partial->template());
}

#[Test]
public function options() {
$options= ['mount' => new Quoted('/')];
$partial= new PartialNode(new Lookup('test'), $options);
Assert::equals($options, $partial->options());
}

#[Test]
public function string_representation() {
Assert::equals(
Expand Down

0 comments on commit 922e962

Please sign in to comment.