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 !!}
Related
Content in my database: <h2> This is heading text</h2>
I want to retrieve this without html tags but with bold format as <h2> tag does .
In this case: strip_tags() only removes the tags.
By default once you do {{ $foo->bar }} laravel retrieves everything as plain text. You may be looking at retrieving it this way instead. {!! $foo->bar !!}
You can use this using blade template :
{!! $data->content !!}
Using above code remove all HTML tags .
If you want to retrieve HTML code to page you can use the below code :
{!! html_entity_decode($data->content) !!}
If you want to get Text Plan only without HTML code use the below code :
{{htmlspecialchars(trim(strip_tags($data->content)))}}
For more details : Displaying Data - Laravel
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
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 am trying to create a button with the Form builder
{!! Form::button('<span style="color: {!! $colour[$i] !!}" class..') !!}
The rest of the contents of the button are irrelevant, what I was trying to do was make the colour of the font $colour[$id] which worked fine when I tested it in raw HTML, but it keeps exiting the quotes when I add blade inside another blade function.
I guess I can't do that, how would I be able to use php in blade code without exiting the function? Thank you in advance.
You should pass an array with your style to this method, like:
Form::button(['style' => 'yourstyle']);
I'm using a HTML editor for fields, and having problem on printing the content on edit form using Model Binding and resource controller.
Since the editor accepts HTML tags, they not escaped (htmlentities way), and inserted raw.
While this might not be a problem, I need to find a way to print that text back into HTML editor with all HTML parsed, due to needed editing of the text.
This is the field that has a HTML editor attached through JS (for insert and edit):
{{ Form::textarea('text', null, array('class' => 'form-control')) }}
Out of model binding way of creating CRUD, {{ html_entity_decode($text) }} will do the job when outputting the text back to the editor.
Question:
Is there a way to add htmlentities() to model-bind-form? I need to print back the text containing html inside HTML editor that will parse it correctly.
P.S. using Laravel's helper e() doesn't seem to solve the problem.
Thanks in advance
Have you tried passing the decoded text into the form:textarea like this:
{{ Form::textarea('text', html_entity_decode($model->text), array('class' => 'form-control')) }}