Skip to content

Commit aa638d3

Browse files
committed
Add custom twig blocks which will be added to the twig templates
1 parent fef2bfd commit aa638d3

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/Compiler.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class Compiler
4747

4848
protected $stripWhitespace = true;
4949

50+
/** @var string[] */
51+
protected $rawBlocks = [];
52+
5053
public function __construct(DOMDocument $document, LoggerInterface $logger)
5154
{
5255
$this->builder = new TwigBuilder();
@@ -56,6 +59,7 @@ public function __construct(DOMDocument $document, LoggerInterface $logger)
5659
$this->components = [];
5760
$this->banner = [];
5861
$this->properties = [];
62+
$this->rawBlocks = [];
5963

6064
$this->logger->debug("\n--------- New Compiler Instance ----------\n");
6165
}
@@ -79,11 +83,20 @@ public function convert(): string
7983
{
8084
$templateElement = $this->document->getElementsByTagName('template')->item(0);
8185
$scriptElement = $this->document->getElementsByTagName('script')->item(0);
86+
$twigBlocks = $this->document->getElementsByTagName('twig');
8287

8388
if ($scriptElement) {
8489
$this->registerProperties($scriptElement);
8590
}
8691

92+
if($twigBlocks->count()){
93+
foreach($twigBlocks as $twigBlock){
94+
/** @var DOMText $twigBlock */
95+
$this->rawBlocks[] = trim($twigBlock->textContent);
96+
}
97+
}
98+
99+
87100
if (!$templateElement) {
88101
throw new Exception('The template file does not contain a template tag.');
89102
}
@@ -92,6 +105,9 @@ public function convert(): string
92105
$resultNode = $this->convertNode($rootNode);
93106
$html = $this->document->saveHTML($resultNode);
94107

108+
if(count($this->rawBlocks)){
109+
$html = implode("\n", $this->rawBlocks) . "\n" . $html;
110+
}
95111
$html = $this->addVariableBlocks($html);
96112
$html = $this->replacePlaceholders($html);
97113

tests/CompilerTwigBlockTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Paneon\VueToTwig\Tests;
4+
5+
class CompilerTwigBlockTest extends AbstractTestCase
6+
{
7+
/** @test */
8+
public function registersProperties()
9+
{
10+
$component = file_get_contents(__DIR__.'/fixtures/twig-block/twig-block.vue');
11+
$expected = file_get_contents(__DIR__.'/fixtures/twig-block/twig-block.twig');
12+
13+
$compiler = $this->createCompiler($component);
14+
15+
$actual = $compiler->convert();
16+
17+
$this->assertEqualHtml($expected, $actual);
18+
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% set myCustomComputedProperty = false %}
2+
<div>
3+
<div>
4+
{% if myCustomComputedProperty %}
5+
<p>Show something</p>
6+
{% endif %}
7+
</div>
8+
</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<div>
3+
<div>
4+
<p v-if="myCustomComputedProperty">Show something</p>
5+
</div>
6+
</div>
7+
</template>
8+
9+
<twig>
10+
{% set myCustomComputedProperty = false %}
11+
</twig>
12+
13+
<script>
14+
export default {
15+
computed: {
16+
myCustomComputedProperty(){
17+
return Math.random() > 1;
18+
},
19+
}
20+
}
21+
</script>

0 commit comments

Comments
 (0)