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.
Related
I am trying to create my first Drupal 8 theme. On the frontpage, I want to list all articles with the title, summary, date posted, etc.
I more or less achieved this using views. Basically {{ page.content }} in twig, but found it to be really inflexible and I didn't really get the results that I wanted. What I want to do is just to iterate through all the articles and print each section of that article "manually". For instance:
{% for page in pages %}
{{ page.content.title }}
{{ page.content.datePosted }}
{{ page.content.body }}
{% endfor %}
So that I can have more control of what is happening and not making a lot of configuration in the views module when deploying. What is the best soltuion to achieve this? Thanks!
I recommend to use Views. Configure you view (filter by content type, etc.), you have a lot of parameters to achieve what you need.
Set the view to show "Content" and give it a view mode (you can use the default teaser, full, or create your own view mode). Then you can create a custom template for this :
node--xxxx--teaser.html.twig
In order to know how to name your custom template file, enable Twig Debugging in sites/default/services.yml. Set the debug variable to true. And clear cache. Then, you will see in source code the template name suggestion like this :
<!-- FILE NAME SUGGESTIONS:
* node--1--full.html.twig
* node--1.html.twig
x node--oeuvre--full.html.twig
* node--oeuvre.html.twig
* node--full.html.twig
* node.html.twig
-->
In your twig, you can do that kind of templating :
{{ content.my_field_image[0] }}
<h2>{{ node.title.value }}</h2>
<p>{{ content.body }}</p>
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] }}
I have overridden the registration .twig files and it works except when the user doesn't enter matching passwords. How do I display any errors that are thrown by the registration process?
You can render your form errors by using form_errors function, like this:
{{ form_errors(form) }}
{{ form_errors(form.someField) }}
You should read this official documentation How to Customize Form Rendering to understand in deep how Symfony rendering your form view and how to custom the template as you want.
I'm using symfony2, I installed FosUserBundle and I created a bundle UserBundle as indicated in the FosUserBundle online docs. so far so good.
I also created another controller and I'm able to access the logged user information in this way:
$user = $this->container->get('security.context')->getToken()->getUser();
now imagine that in my website, for all the pages/controller, I need to display some user information, even a simple "Welcome MyUser" at the top of the page (so in base.html.twig). I don't want to replicate the line above in all the controllers, so where is the best place to get this information once and pass them to the base template?
For the example you gave, "Welcome MyUser" , you can get this var in twig template with
{% if is_granted("ROLE") %}
Hi {{ app.user.username }}
{% endif %}
This is, if you don't need logic
Also if you didn't knew it you can use heritage in twig, so that you can create a navbar.html.twig with this fosuser var in it, and then in all your templates do
{% extends "AcmeMyBundle::navbar.html.twig" %}
{% block body %}
....
{% endblock %}
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.