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 }}
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>
I have a question about the translation of a twig template.
This is an actual way to translate.
{% trans into 'en_EN' %}Hello{% endtrans %}!,
Can I load directly the '' value from a database like this? (this returns an error).
{% trans into {{ app.user.lang }} %}Hello{% endtrans %}!,
or shall I use a controller to do it?
I'm now using the following function to translate:
{% with %}
{% set lang = app.user.lang %}
<button class="btn btn-outline-primary" type="button" data-toggle="dropdown">{% trans into lang %}Hello{% endtrans %}!, {{ app.user.username }}<i class="material-icons">arrow_drop_down</i></button>
{% endwith %}
But I'm not sure if is a good practice or not.
You don't need to use into, if you want to have translations based on the user's preferences, you add it through a listener or include the locale in the URL's, then all you have to do in your twig templates is {{ 'Hello' | trans }}
See: https://symfony.com/doc/current/translation/locale.html
I think your question is nor really clear. Normally you can define a default language in your configuration file and then change them with a language selector or something else.
Then you don't need that into.
Symfony 4 How to change language Step by Step
Here is a good example how to build a language switch and some tutorials. You should read the documentation for translation where the most things are explained.
https://symfony.com/doc/current/translation.html
When you really want to store your translations to your database you have to use a translation loader and extend them so you have to register a new service and load your translations from your database.
Symfony2 Database Translation Loader isn't executed
But i don't know if that was exactly your question.
I have a template views-view-fields.html.twig and created a view. I can access all the fields easily and also the link field. But it generates <a>. I want to extract link from this field.
I have searched SO and found some solution but they didn't work for me.
{{ fields.field_find_out_more_link[0]['#url'] }}
{{ fields.field_find_out_more_link.0['#url'] }}
{{ fields.field_find_out_more_link.url }}
Can anyone point me to right direction?
This is working for me in views-view-fields.html.twig for the link field :
text
Well, maybe I am a bit late but I think I found a proper answer to this.
In your View, you need to have a Link to Content field. Then, in the configuration of the field, you need to set to true the option "Show url as text". Finally, in your twig, you can set your button like:
Yeah
Hope this helps.
Based on Sébastien Gicquel answare i used it like this in a views-view-fields.html.twig template with a custom link field:
<a href="{{ fields.field_link.content|striptags|trim }}">
{{ fields.field_image.content }}
{{ fields.title.content }}
</a>
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'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 :)