Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ private function stripEventHandlers( DOMNode $node ) {
}
}

private function convertDataValueToString( $value ) {
if ( is_string( $value ) ) {
return $value;
}
return json_encode( $value );
}

/**
* @param DOMNode $node
* @param array $data
Expand All @@ -100,8 +107,7 @@ private function replaceMustacheVariables( DOMNode $node, array $data ) {

foreach ( $matches['expression'] as $index => $expression ) {
$value = $this->app->evaluateExpression( $expression, $data );

$text = str_replace( $matches[0][$index], $value, $text );
$text = str_replace( $matches[0][$index], $this->convertDataValueToString( $value ), $text );
}

if ( $text !== $node->textContent ) {
Expand Down
16 changes: 16 additions & 0 deletions tests/php/TemplatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,22 @@ public function testTemplateWithObjectValuedNonClassAttribute_ThrowsError() {
);
}

public function testMoustacheVariableWithArrayListTypeSubstitution() {
$result = $this->createAndRender(
'<p>{{ my.data.variable }}</p>',
[ 'my' => [ 'data' => [ 'variable' => [ 1, 2, 3 ] ] ] ]
);
$this->assertSame( '<p>[1,2,3]</p>', $result );
}

public function testMoustacheVariableWithArrayObjectTypeSubstitution() {
$result = $this->createAndRender(
'<p>{{ my.data.variable }}</p>',
[ 'my' => [ 'data' => [ 'variable' => [ "a" => "b", "c" => "d" ] ] ] ]
);
$this->assertSame( '<p>{"a":"b","c":"d"}</p>', $result );
}

/**
* @param string $template HTML
* @param array $data
Expand Down