Skip to content

Commit

Permalink
Add support for OctoberCMS / WinterCMS (#27)
Browse files Browse the repository at this point in the history
* Update Configuration.php

Add support for hint paths configuration

* Update ComponentTokenParser.php

Is enabled hint path support, prepare the hint path

* update hint param handle style, add test

* update readme for OctoberCMS and WinterCMS

* update readme for OctoberCMS and WinterCMS

* update readme for OctoberCMS and WinterCMS

* fixes

---------

Co-authored-by: Giorgio Pogliani <giorgiopogliani94@gmail.com>
  • Loading branch information
marcomessa and giorgiopogliani authored Oct 1, 2024
1 parent c25a903 commit 66d0409
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,44 @@ final class TwigEnvironmentConfigurator
}
```

### OctoberCMS / WinterCMS

In OctoberCMS / WinterCMS you need to hook into ```cms.page.beforedisplay``` event inside your plugin's boot method in order to access twig instance.
Then you can use your plugin hint path to choose a views subfolder as component's folder.

## Usage
es.
```
plugins
|__namespace
|__pluginname
|__views
|__components
|__button.htm
```

```php
public function boot(): void
{
Event::Listen('cms.page.beforeDisplay', function ($controller, $url, $page) {
$twig = $controller->getTwig();

Configuration::make($twig)
->setTemplatesPath('namespace.pluginname::components', hint: true)
->useCustomTags()
->setup();
});
}
```

then in your htm files

```
<x-button>...</x-button>
```

Alle features like subfolders are supported, like ```<x-forms.input></x-forms.input>``` will refer to ```plugins/namespace/pluginname/views/forms/input.htm```


The components are just Twig templates in a folder of your choice (e.g. `components`) and can be used anywhere in your Twig templates. The slot variable is any content you will add between the opening and the close tag.

Expand Down
10 changes: 9 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Configuration

protected string $templatesPath = 'components';

protected bool $needsHintPath = false;

protected bool $isUsingTemplatesExtension = true;

protected string $templatesExtension = 'twig';
Expand All @@ -40,9 +42,10 @@ public static function make(Environment $twig): Configuration
* @param string $path
* @return Configuration
*/
public function setTemplatesPath(string $path): self
public function setTemplatesPath(string $path, bool $hint = false): self
{
$this->templatesPath = rtrim($path, DIRECTORY_SEPARATOR);
$this->needsHintPath = $hint;

return $this;
}
Expand All @@ -52,6 +55,11 @@ public function getTemplatesPath(): string
return $this->templatesPath;
}

public function getNeedsHintPath(): bool
{
return $this->needsHintPath;
}

public function useTemplatesExtension(bool $isUsing = true): self
{
$this->isUsingTemplatesExtension = $isUsing;
Expand Down
11 changes: 8 additions & 3 deletions src/TokenParser/ComponentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ public function getComponentPath(string $name)

$componentPath = rtrim($this->configuration->getTemplatesPath(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name;

if ($this->configuration->isUsingTemplatesExtension()) {
$componentPath .= '.' . $this->configuration->getTemplatesExtension();
if ($this->configuration->getNeedsHintPath()) {
$componentPath = rtrim($componentPath, '.');
$componentPath = str_replace('/', '.', $componentPath);
} else {
if ($this->configuration->isUsingTemplatesExtension()) {
$componentPath .= '.' . $this->configuration->getTemplatesExtension();
}
}

return $componentPath;
}

public function parse(Token $token): Node
{
list($variables, $name) = $this->parseArguments();
[$variables, $name] = $this->parseArguments();

$slot = $this->parser->subparse([$this, 'decideBlockEnd'], true);

Expand Down
50 changes: 50 additions & 0 deletions tests/ComponentTokenParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Performing\TwigComponents\Tests;

use Performing\TwigComponents\Configuration;
use Performing\TwigComponents\TokenParser\ComponentTokenParser;
use PHPUnit\Framework\TestCase;

class ComponentTokenParserTest extends TestCase
{
public function testGetComponentPathWithHintPath()
{
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
$twig = new \Twig\Environment($loader);

$config = Configuration::make($twig)
->setTemplatesPath('mynamespace.myplugin::components', hint: true)
->setTemplatesExtension('twig')
->useCustomTags();

$this->assertTrue($config->getNeedsHintPath());
$this->assertEquals($config->getTemplatesPath(), 'mynamespace.myplugin::components');
$this->assertEquals($config->getTemplatesExtension(), 'twig');

$parser = new ComponentTokenParser($config);
$componentPath = $parser->getComponentPath('test/component');
$this->assertEquals('mynamespace.myplugin::components.test.component', $componentPath);
}

public function testGetComponentPathWithoutHintPath()
{
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
$twig = new \Twig\Environment($loader);

$config = Configuration::make($twig)
->setTemplatesPath('_components', hint: false)
->setTemplatesExtension('twig')
->useCustomTags();

$this->assertFalse($config->getNeedsHintPath());
$this->assertEquals($config->getTemplatesPath(), '_components');
$this->assertEquals($config->getTemplatesExtension(), 'twig');

$parser = new ComponentTokenParser($config);

$componentPath = $parser->getComponentPath('test/component');

$this->assertEquals('_components/test/component.twig', $componentPath);
}
}

0 comments on commit 66d0409

Please sign in to comment.