I am new to symfony.SO please find me a solution.On my controller
$table="<table><tr><th>Test</th></tr><tr><td>Show Test</td></tr></table>"
And I have Passed it to the twig from the controller like
return $this->render('lottery/transactions/trnissuenote/show.html.twig', array(
'table'=>$table
));
And on my twig
<div >
{{table}}
</div>
But i didnt get the table structure .Just echoing it.How will i get the table structure.Help me find a solution.
You need to display it using raw filter. Since by default {{}} escapes html content of a variable.
<div>
{{ table|raw }}
</div>
Read more: https://twig.symfony.com/doc/2.x/filters/raw.html
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 unusual situation which I have to print dynamic variables inside another variable in blade but not sure if it's possible or how to?
Example
// this returns content of my template which has dynamic data in it
{!! $data['template']->content !!}
// inside that template content I have to get user name like
{{$data['customer']->name}}
the problem is printing customer name
here is sample result
Note: $data['customer'] is provided in view page but the problem is how to get it inside my template data.
Code
My code is basically is just sample code i shared above (it's all happening in blade)
{!! $data['template']->content!!}
// inside `content` there is like:
<h4>{{$data['customer']->customer_lastname}}</h4>
Question
How can I get {{$data['customer']}} printed inside {!! $data['template']->content!!}?
Per my comments, you could treat and compile the template string with pre-defined patterns. For example, for customer names you could do this when constructing content text:
<h4>:customer_name</h4>
And then before sending the data to blade:
$templateContent = $data['template']->content;
$templateContent = preg_replace("/:customer_name/i", $data['customer']->name, $templateContent);
return view('yourview', compact('templateContent'));
And, in blade:
{!! $templateContent !!}
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 have a question: how can 1 view/page use 2 controllers to display different data?
For example:
1) Page displays DVD's information using DvdController.
2) On that same page I want to use a Template/Partial view, that displays list of actors, and that list should come from the second ActorsController.
I can add a template view inside another page but the second controller doesn't work:
MainPage on URL: website/dvd
{% block body %}
<div>
<h1>{{ dvd.title }} </h1>
</div>
<div>
{{ include('DVDBundle:Actors:actors.html.twig') }}
</div>
{% endblock %}
Above I use the partial view actors.html.twig to show the actors, strangely the html code/div's and so on are actually displayed and working on the page, however the controllers(ActorsController) method that meant to return this partial view is not executed for some reason.?
But if I go to the view directly via link: website/actors then its working.
The Symfony documentation on embedded controllers explains this nicely.
In short, you will need to do something like this in your template file:
<div>
{{ render(controller(
'DVDBundle:Actors:actors',
{ ... parameters to the controller action here ... }
)) }}
</div>
The important thing is calling the render function, which allows a controller to be rendered, as opposed to simply includeing a template. (This assumes that your have an ActorsController with an actorsAction method; change the names as appropriate to your controller.)
I have been stumped for a couple days and I am seeking some direction.
I am attempting to call an image path stored in database to twig file in order to display said image. The twig example below, I am expecting the same image to be displayed twice. When inspecting the rendered html, the variable passes the path from the database, but the first image is not displayed.
From controller:
'logo' => $vendor->getLogovendors()
From database column logoVendors:
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
From twig:
<div class="container">
{{logo | raw}}
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
</div>
I am new to Symfony and its asset management. Any help or prodding in the right direction would be appreciated.
You should normally store only the path to the image in your database!
If logo was the variable you pass to the template holding the image path bundles/loginlogin/img/fs_logo_large.png you could simply include it using twig's asset function like this:
<img src="{{ asset(logo) }}"/>
what you're trying to do ( evaluating a twig function inside a string ) can be solved aswell...but i don't recommend it.
If you want to store the complete code including {{ asset() }} in your database you need to make twig evaluate the code inside the string.
This means twig shall execute the code inside the string instead of just printing it.
This can be achieved using the evaluate filter from this answer.
The final result would then be:
{{ logo |evaluate |raw }}