Skip to content

Commit 72a6ff3

Browse files
committed
feature #13264 URL manipulations as a Twig extension (fabpot)
This PR was merged into the 2.7 branch. Discussion ---------- URL manipulations as a Twig extension | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs#4805 While working on the new asset component, I realized that the "absolute URL" feature was misplaced and would benefit from being exposed as a Twig function (composition is always a good thing). Then, I wondered if having a Twig function to generate a relative path (like done by the Routing component would also make sense). And here is the corresponding PR. ```jinja {# generate an absolute URL for the given absolute path #} {{ absolute_url('/me.png') }} {# generate a relative path for the given absolute path (based on the current Request) #} {{ relative_path('/foo/me.png') }} {# compose as you see fit #} {{ absolute_url(asset('me.png')) }} ``` As you can see, we require an absolute path for both functions (and we even add the leading slash if it is omitted), not sure if we want to do otherwise. ping @Tobion Commits ------- 0ec852d added a relative_path Twig function ee27ed8 added an absolute_url() Twig function
2 parents f3f561a + e91965a commit 72a6ff3

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

DependencyInjection/Compiler/ExtensionPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,9 @@ public function process(ContainerBuilder $container)
4242
if ($container->has('fragment.handler')) {
4343
$container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
4444
}
45+
46+
if ($container->has('request_stack')) {
47+
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
48+
}
4549
}
4650
}

Resources/config/twig.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
<argument type="service" id="fragment.handler" />
104104
</service>
105105

106+
<service id="twig.extension.httpfoundation" class="Symfony\Bridge\Twig\Extension\HttpFoundationExtension" public="false">
107+
<argument type="service" id="request_stack" />
108+
</service>
106109

107110
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
108111
<argument type="service" id="twig.form.renderer" />

0 commit comments

Comments
 (0)