Render twig HTML string with twig-variable - php

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

Related

Using Twig variable inside Twig variable

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

PHP what is the difference between { } and {{ }}

I'm using PHP Laravel framework and I came to some code examples where {{ }} is use inside a html code, like this:
<link rel="stylesheet" href=" {{ URL::to('css/app.css') }} ">
My conclusion is that the {{ }} are used to write no-HTML code inside the HTML, is that correct?
And for what is the { } used?
Thanks for your answer.
There is no { } in Blade, {{ }} displays escaped data and {!! !!} displays unescaped data.
By default, Blade {{ }} statements are automatically sent through
PHP's htmlentities function to prevent XSS attacks. If you do not want
your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}.
https://laravel.com/docs/5.3/blade#displaying-data
That is not php but rather syntax for the blade template system that laravel comes bundeled with.
In short, {{$aPhpVariable}} is basically compiled to <?= htmlentities($aPhpVariable) ?> (or even <?php echo htmlentities($aPhpVariable); ?>), but from what I know, there is no single bracket ({}) syntax.
You can also use normal php code inside blade templates or just treat it as a normal html page, but it does have a bunch of things that makes building the views a lot easier.
Go check out the docs for more info about blade!
To escape data use
{{ $data }}
If you don't want the data to be escaped use :
{!! $data !!}
{} is part of the syntax of PHP code. It's used in functions, blocks of code and objects.
{{ }} it part of Laravel's Blade template syntax, echoing something in a Laravel project.

How To Put HTML Code In Laravel Function?

I stumbled upon this problem. I am using twitter bootstrap. I generate form elements like this:
{{ Form::button('Save', array('class'=>'btn btn-success')) }}
This is alright. But when I want to put an icon before the 'Save' like this,
{{ Form::button('<i class="icon-ok"></i> Save', array('class'=>;'btn btn-success')) }}
The <i> tag is not interpreted as it should be.Is there any workaround on this? How do I do this?
Any Body Give Some Idea
Thanks in advance.
You can use the HTML::decode method to wrap your button.
Example:
{{ HTML::decode(Form::button('<i class="icon-ok"></i> Save', array('class'=>;'btn btn-success'))) }}
HTML::decode converts entities to HTML characters according to laravel's api, found here:
laravel api
What version of Laravel are you using?
In Laravel 5, Blade has changed so that {{ }} is escaped by default (therefore not rendered as HTML by the browser) and you should use {!! !!} instead.

Parse a variable content in TWIG

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.

Render twig-template with dynamically placed HTML in Symfony2?

I have an action and a Twig-template.
Via render() it is possible to place a text for a Twig-tag {{ tagname }}.
But this text is escaped. Now I would like to place (not-escaped) HTML-code.
How is that done in Symfony2?
Escaping is handled by Twig.
{{ var }} leads to escaped output.
{{ var|raw }} leads to raw/not-escaped output.
raw is a filter.

Categories