Skip to content

Commit

Permalink
feature #13264 URL manipulations as a Twig extension (fabpot)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Jan 10, 2015
2 parents f3f561a + e91965a commit 72a6ff3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/Compiler/ExtensionPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ public function process(ContainerBuilder $container)
if ($container->has('fragment.handler')) {
$container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
}

if ($container->has('request_stack')) {
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
}
}
}
3 changes: 3 additions & 0 deletions Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<argument type="service" id="fragment.handler" />
</service>

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

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

0 comments on commit 72a6ff3

Please sign in to comment.