Skip to content

Commit 4d98bd9

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

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Twig/EntryFilesTwigExtension.php

Lines changed: 15 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

@@ -31,6 +32,7 @@ public function getFunctions(): array
3132
new TwigFunction('encore_entry_css_files', [$this, 'getWebpackCssFiles']),
3233
new TwigFunction('encore_entry_script_tags', [$this, 'renderWebpackScriptTags'], ['is_safe' => ['html']]),
3334
new TwigFunction('encore_entry_link_tags', [$this, 'renderWebpackLinkTags'], ['is_safe' => ['html']]),
35+
new TwigFunction('encore_entry_exists', [$this, 'entryExists']),
3436
];
3537
}
3638

@@ -58,6 +60,19 @@ public function renderWebpackLinkTags(string $entryName, string $packageName = n
5860
->renderWebpackLinkTags($entryName, $packageName, $entrypointName, $attributes);
5961
}
6062

63+
public function entryExists(string $entryName, string $entrypointName = '_default'): bool
64+
{
65+
try {
66+
$entrypointLookup = $this->getEntrypointLookup($entrypointName);
67+
$entrypointLookup->getJavaScriptFiles($entryName);
68+
$entrypointLookup->reset();
69+
70+
return true;
71+
} catch (EntrypointNotFoundException $e) {
72+
return false;
73+
}
74+
}
75+
6176
private function getEntrypointLookup(string $entrypointName): EntrypointLookupInterface
6277
{
6378
return $this->container->get('webpack_encore.entrypoint_lookup_collection')

0 commit comments

Comments
 (0)