Skip to content

Commit 08ff217

Browse files
committed
Add twig helper functions to check if entrypoint has files
1 parent 5a86aad commit 08ff217

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/Twig/EntryFilesTwigExtension.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Psr\Container\ContainerInterface;
1313
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
1414
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
15+
use Symfony\WebpackEncoreBundle\Exception\EntrypointNotFoundException;
1516
use Twig\Extension\AbstractExtension;
1617
use Twig\TwigFunction;
1718

@@ -29,6 +30,8 @@ public function getFunctions(): array
2930
return [
3031
new TwigFunction('encore_entry_js_files', [$this, 'getWebpackJsFiles']),
3132
new TwigFunction('encore_entry_css_files', [$this, 'getWebpackCssFiles']),
33+
new TwigFunction('encore_has_entry_js_files', [$this, 'hasWebpackJsFiles']),
34+
new TwigFunction('encore_has_entry_css_files', [$this, 'hasWebpackCssFiles']),
3235
new TwigFunction('encore_entry_script_tags', [$this, 'renderWebpackScriptTags'], ['is_safe' => ['html']]),
3336
new TwigFunction('encore_entry_link_tags', [$this, 'renderWebpackLinkTags'], ['is_safe' => ['html']]),
3437
];
@@ -46,6 +49,30 @@ public function getWebpackCssFiles(string $entryName, string $entrypointName = '
4649
->getCssFiles($entryName);
4750
}
4851

52+
public function hasWebpackJsFiles(string $entryName, string $entrypointName = '_default'): bool
53+
{
54+
try {
55+
$entrypointLookup = $this->getEntrypointLookup($entrypointName);
56+
$entrypointLookup->reset();
57+
58+
return count($entrypointLookup->getJavaScriptFiles($entryName)) > 0;
59+
} catch (EntrypointNotFoundException $e) {
60+
return false;
61+
}
62+
}
63+
64+
public function hasWebpackCssFiles(string $entryName, string $entrypointName = '_default'): bool
65+
{
66+
try {
67+
$entrypointLookup = $this->getEntrypointLookup($entrypointName);
68+
$entrypointLookup->reset();
69+
70+
return count($entrypointLookup->getCssFiles($entryName)) > 0;
71+
} catch (EntrypointNotFoundException $e) {
72+
return false;
73+
}
74+
}
75+
4976
public function renderWebpackScriptTags(string $entryName, string $packageName = null, string $entrypointName = '_default', array $attributes = []): string
5077
{
5178
return $this->getTagRenderer()

0 commit comments

Comments
 (0)