With Twig, how can we get domain from an email address which is a string?
<p>{{ app.user }}</p>
<p>{{ to_display_domain_name_here }}</p>
Where app.user is xyz#gmail.com
Want to retrieve domain gmail or gmail.com from this.
You can create a custom twig filter and use regex to get domain.
Custom filter: http://symfony.com/doc/current/templating/twig_extension.html
Regex example: https://gist.github.com/Avidid/6114724
The best way to do it, in my opinion, would be create a custom Twig extension. You can find more info about how to do it in the official documentation.
For a quick and dirty way I would probably use the split filter which actually behaves like an explode in Twig:
{{ app.user|split('#')[1] }}
Related
How do i get the id of the admin and object in a twig template using sonata admin?
I have tried admin.getId() and object.getId() but that didn't work.
Any ideas?
{{ admin.id }} or {{ object.id }}?
Are you sure the admin and object variables are objects?
Look at this page: http://twig.sensiolabs.org/doc/functions/attribute.html.
I'm looking to create a dynamic rel="canonical" tag in my application which pulls in the current URL but want to ensure any query parameters are removed. E.g http://www.example.com/test/?page=2 should have the canonical as http://www.example.com/test/, therefore {{ app.request.uri }} doesn't work as this pulls in ?page=2 as well.
Does anyone know how to pull in the absolute path of a page without the query parameters?
This will work,
{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}
I just tried to dump baseUrl and can confirm that it does not work.
However, this works:
{{ app.request.getSchemeAndHttpHost ~ app.request.baseUrl ~ app.request.pathInfo }}
I know, it's not pretty but it does the job :)
I want to have a link to web/uploads/documents
Normally I get link as
{{ path('name in route.yml') }}
But there is no link name for web/uploads/documents
How can acess there in twig file??
Just typ web/uploads/documents? E.g.
{{ foovar }}
I am trying to create a link for FOSUser Bundle.. It seems pretty straight forward, but for some reason i am having a problem..
i want to create a "Login" link inside my twig.
<a class="Link" href="{{ url('/login') }}">Login</a>
which should direct to
http://localhost:9911/Symfony/web/app_dev.php/login as defined in my controller.
Am I doing this incorrectly?
If you want to link to routes you can just use {{ path() }} or {{ url() }} and use the name of the route as argument. I was just about to tell you that you should read the documentation, but it actually doesn't mention the login-route. Who would have guessed?
You can find out the name of a route by looking into Resources/config/routing/*.yml. In your case the route is named fos_user_security_login, therefore linking to this route is as easy as using:
login
Does anybody know how to get the current URI in a Twig template?
I’ve read through the documentation and I’m unable to find the Twig function to do this.
{{ app.request.uri }}
If you want to read it into a view variable:
{% set uri = app.request.uri %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.