Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Commit

Permalink
Add support for custom template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Jan 5, 2020
1 parent 7fbfb95 commit a16f27b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ templateFactory:
containerParam: %param%
```

### Custom template functions

Similarly to filters, you can also define callbacks for your custom template functions:

```yaml
templateFactory:
functions:
doStuff: [@someService, doStuff]
```

### Configuration from another `CompilerExtension`

Some extensions may need to install a Latte filter, or inject a parameter / service into template. This can be done in `beforeCompile()` phase by customizing setup of `TemplateConfigurator`.
Expand All @@ -73,6 +83,7 @@ Some extensions may need to install a Latte filter, or inject a parameter / serv
$templateConfigurator = $containerBuilder->getByType(Nepada\TemplateFactory\TemplateConfigurator::class);
$containerBuilder->getDefinition($templateConfigurator)
->addSetup('addFilter', ['filterName', $callback])
->addSetup('addFunction', ['functionName', $callback])
->addSetup('addProvider', ['provider', $value])
->addSetup('addParameter', ['parameter', $value])
->addSetup('addParameter', ['parameter', '@someService']);
Expand Down
5 changes: 5 additions & 0 deletions src/Bridges/TemplateFactoryDI/TemplateFactoryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function getConfigSchema(): Nette\Schema\Schema
'parameters' => Nette\Schema\Expect::array(),
'providers' => Nette\Schema\Expect::array(),
'filters' => Nette\Schema\Expect::array()->items('callable'),
'functions' => Nette\Schema\Expect::array()->items('callable'),
]);
}

Expand Down Expand Up @@ -54,6 +55,10 @@ public function beforeCompile(): void
foreach ($config->filters as $name => $filter) {
$templateConfigurator->addSetup('addFilter', [$name, $filter]);
}

foreach ($config->functions as $name => $function) {
$templateConfigurator->addSetup('addFunction', [$name, $function]);
}
}

}
18 changes: 18 additions & 0 deletions src/TemplateFactory/TemplateConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class TemplateConfigurator
/** @var callable[] */
private array $filters = [];

/** @var callable[] */
private array $functions = [];

private ?Nette\Localization\ITranslator $translator = null;

/**
Expand Down Expand Up @@ -64,6 +67,17 @@ public function addFilter(string $name, callable $filter): self
return $this;
}

/**
* @param string $name
* @param callable $function
* @return static
*/
public function addFunction(string $name, callable $function): self
{
$this->functions[$name] = $function;
return $this;
}

public function configure(Nette\Application\UI\ITemplate $template): void
{
if (! $template instanceof Nette\Bridges\ApplicationLatte\Template) {
Expand All @@ -84,6 +98,10 @@ public function configure(Nette\Application\UI\ITemplate $template): void
$template->addFilter($name, $filter);
}

foreach ($this->functions as $name => $function) {
$latte->addFunction($name, $function);
}

$template->setParameters($this->parameters);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Bridges/TemplateFactoryDI/Fixtures/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ extensions:
templateFactory:
filters:
lower: NepadaTests\Bridges\TemplateFactoryDI\Fixtures\Filters::lower
functions:
lower: Nette\Utils\Strings::lower
providers:
fooProvider: bar
parameters:
Expand All @@ -17,6 +19,7 @@ services:
templateFactory.templateConfigurator:
setup:
- addFilter(upper, NepadaTests\Bridges\TemplateFactoryDI\Fixtures\Filters::upper)
- addFunction(upper, Nette\Utils\Strings::upper)
- addProvider(applicationProvider, @application.application)
- addParameter(application, @application.application)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class TemplateFactoryExtensionTest extends TestCase

// translator
Assert::same(MockTranslatorFactory::TRANSLATED_MESSAGE, $latte->invokeFilter('translate', ['message']));

// functions
Assert::same('LOREM ipsum', $template->renderToString('{=upper(Lorem)} {=lower(Ipsum)}'));
}

protected function setUp(): void
Expand Down

0 comments on commit a16f27b

Please sign in to comment.