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']);
Related
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 mcqs website, the problem is that when i try to load a quiz page with multiple questions and their answers, it loads fine.
Working code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{{$answer->content}}
</span>
#endforeach
Not Working Code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{!! $answer->content !!}
</span>
#endforeach
{!! $answer->content !!} is the same as echo $answer->content
however for the same code when i try to render the answers with HTML formatting using <?php echo , the page stops rendering midway. forexample if say the page was to load 30 questions , it will only load 10 and stops rendering . Even when i tried using {!!$answer->content!!} , the problem remains the same.
Note: i am returning set of questions using the below code:
$questions = Question::all()->where('exam_id',$exam_id)->random($questionsCount)->shuffle();
return view('quiz',compact('exam','questions','questionsCount','t'));
I have one to many relationship setup so i can extract the answers of the question using $question->answers
it works fine when i don't echo any answer content.
Anyhelp will be greatly appreciated. Thanks!
Looks like an issue with unescaped text. When you use the blade template's escape method ({{ }}) it is successful. When you use the unescaped method, ({!! !!}) it fails. This is the same when using the unescaped php echo.
There is likely some type of character in one of your questions that is causing a break of the loop or php echo.
To fix, use the blade escape {{ }} or php escape (htmlentities(), htmlspecialchars() etc) in the echo.
htmlentities-vs-htmlspecialchars is worth checking out as well.
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 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.
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')) }}