I created a page of my VIEW - views-view-fields--MYFOOBAR.html.twig
I managed to output the URL of my image field via:
{{ file_url(row._entity.field_image.entity.uri.value) }}
Also I managed how to output normal data fields like text or number:
{{ row._entity.field_MYTEXTFIELD.value }}
Sorry for stupid question, but how can I get URL to my node? I need something like:
<a class="full-item-title" href="NODE URL">{{ row._entity.field_MyH1Text.value }}</a>
{# Link to node page. #}
{{ 'View node page'|t }}
Look at the official documentation: link
Related
I have some html buttons I want to render with twig. This is the HTML:
<button class="btn btn-primary">Edit worklog</button>
I created a method in php to return the HTML string above which I pass in with twig like this:
{{ html.editWorklogButton|raw }}
But when the button is rendered with raw it also renders {{ worklog.id }} and {{ worklog.customerid }} raw of course, losing the id's, giving me href to:
localhost/Worklog/editWorklog?worklogid={{worklog.id}}&customerid={{worklog.customerid}}
which instead should be something like:
localhost/Worklog/editWorklog?worklogid=1&customerid=2
I've checked twig documentation, but can't find anything on this. Is this simply not possible to do?
You may use the template_from_string extension.
The template_from_string function loads a template from a string.
In your case, it should be something like this:
{{ include(template_from_string(html.editWorklogButton)) }}
I have a language JSON file containing the text of each page on my website. So to fetch the title of the page, I'll do something like
{{ translation.page1.title }}
In order to fetch the title for page 1.
I already have a page variable that tells me the name of the page, I was wondering if it was possible to do something to avoid having a giant if statement for each page such as:
{{ translation.{{ page }}.title }}
I've looked through the twig doc and I have no idea.
Try to put your variable between brackets
{{ translation[page].title }}
That should be work
I want to send user data to my univeral header file, which is shown on every page so I can display a "Hi, {{ username }}" message.
How can I do this without getting the data in all controllers of every page?
You can access current user in your twig template via the global variable app, using app.user :
{# If user is connected #}
{% if is_granted('ROLE_USER') %}
Hi, {{ app.user.username }}
{# If user is NOT connected #}
{% else %}
...
{% endif %}
To get the currently logged in user, you can simply write: {{ app.user }}. Then you can access any property from there.
You can also render data from a totally different controller and action (so just from one place) in your twig template by using:
{{ render(controller('AppBundle:Base:myAction')) }}
You can read it over here: https://reformatcode.com/code/c/calling-r39s-optim-function-from-within-c-using-rcpp
I have an given template file with following content:
Welcome, {{ user }}
In my PHP File I load this content by:
$welcome = file_get_contents(APPPATH . 'classes/Plugins/Core/testplugin/view/frontend/index.html');
So that's in template:
"Welcome, {{ user }}"
Now, if I give this content into my template:
$twig->welcome = $welcome;
$twig->user = "Tom";
Template output (html):
{{ welcome }}
Template output (browser):
Welcome, {{ user }}
My problem: Template engine won't replace my user given in twig string!
Any ideas? I tried using following filters:
{{ welcome|nl2br|raw }}
So you have a string in your template which in itself is a template. Then you need to tell Twig to treat your template variable as a template, otherwise it'll just treat it as string. For this there's the template_from_string function.
I want to list all files in a directory Using PyroCMS.
Using the Files module each client has their own folder, the folder is their User ID
\files\clients\{ID}\
I need something like this.
{{ foreach file in { files:/clients/{{ user:id }} } }}
{{files:current_file_name}}
{{ endif }}
You should be able to do something like this...
{{ files:listing folder="/files/clients/{{ client_id }}" }}
Download {{ name }}
{{ /files:listing }}
You will need to set the client_id variable though. You could set it in your contoller and use:
$this->template
->set('client_id', $client_id)
->build('page-template-goes-here');
Or even use the current user with the tag:
{{ user:id }}
I haven't tested this, but you should be able to get something going.
From here:
http://docs.pyrocms.com/2.1/manual/index.php/modules-and-tags/tag-reference/files
http://docs.pyrocms.com/2.1/manual/index.php/modules-and-tags/tag-reference/user