Parse a variable content in TWIG - php

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.

Related

Render twig HTML string with twig-variable

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)) }}

How can I get node URL in Twig?

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

Laravel Blade doesn't include partial

I'm trying to add to my layout a header file like so:
Views/layouts/main_layout.blade.php
#include('layouts.header.stylesheets')
the content for that file is:
#section('stylesheets')
{{ stylesheet_link_tag('style.css') }}
#stop
but that doesn't include anything, could you please tell me what am I doing wrong?
If you are including a file, you just do this:
Views/layouts/main_layout.blade.php
#include('layouts.header.stylesheets')
Then in your other file:
{{ stylesheet_link_tag('style.css') }}
You only use #section if you are #yielding to that file

symfony twig html tags </div>

I'm fairly new with twig, so I'm having a little trouble. How add tag to end of variable name?
{{ item.description|raw'</div>' }}
is correct?
description is with html tags
I think the best way is to add your tag out from twig:
{{ item.description|raw }}</div>
However, you could use concatenation:
{{ (item.description ~ '</div>')|raw }}

List files in Dir using Pyro

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

Categories