Consider the following PHP code:
<?php
echo "prefix";
echo "
{{ foo }}
";
echo "suffix";
It renders as:
prefix
{{ foo }}
suffix
As can be seen here:
http://sandbox.onlinephpfunctions.com/code/4c0b3a3e921b0a761b1cf05f12f709d025d1b2de
Note the line feed between prefix and {{ foo }}.
Now consider the following code:
<?php
echo "
{{ foo }}
";
echo "suffix";
It renders as:
{{ foo }}
suffix
As can be seen here:
http://sandbox.onlinephpfunctions.com/code/18b07b9d05b79703617364b4d301d6799654a086
Why was the first line feed not rendered in the second case while it was in the first?
This seems to be because the web browser (or website running the PHP for you) is removing the extra whitespace from the rendered HTML output. (See: https://stackoverflow.com/a/17784740)
If you run your code on the PHP command line, then you will see the leading newline that you desire.
If this code is going to be used in a HTML page, then use <br/> tags to make new lines.
Related
I'd like to output text ($post->content) to the user including line breaks and urls.
If I would use {{ $post->content }}, line breaks won't be displayed and a
converted URL looks like this <a href="www.google.com'>www.google.com</a> in the output.
If I would use {!! $post->content !!}, line breaks and urls would be displayed correctly, but this would mean an actual security risk because a user could enter HTML or other code.
What would be the best way to handle my problem?
I need to render raw HTML on page using twig. Issue that I'm having is that when I have two concurrent HTML element separated by white-space, that white-space gets removed.
How can I preserve that space?
I'm rendering HTML string as so:
{{ set _html = entity.html }}
{{ _html|raw }}
For example:
<p>start <span class="some-class">one</span> <span class="some-class">two</span> stop</p>
Is rendered as:
<p>start <span class="some-class">one</span><span class="some-class">two</span> stop</p>
I'm sure that twig raw function is sanitizing my data and therefore my issue.
How I see it:
As cale_b recommended, I will be using following CSS hack to add a space before element that lacks my spacing:
.monograph {
* + span:before {
content: ' ';
}
}
As answered in here and also in my case, the issue was that I had a {% spaceless %} tag that wrapped my content. So |raw was working correctly but it was the spaceless tag that was stripping the spaces.
I am having a problem by rendering some html stuff from a database table. I have a function that is calling and returning some html content from databse table, when i use {{ }} double curly braces it shows the content on page but as a plain text not rendered as html. After i try to use {!! !!} it does not show anything on page. i don't understand why and what's the solution in this case. My blade page contains the .blade extension as well.
Please advice.
$string = "<h1>Its H1 Tag</h1>";
{{ $string }}
Displaying 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 !!}.
if above not work then use below but not a good practice-
in your case you can use htmlentities($string), html_entity_decode($string) on your data and then use {{ $string }} to render html.
For Laravel Version 5.6.* or higher use single Curley Braces
$string = "<h1>Its H1 Tag</h1>;
{!! $string !!}
It can be displayed by the following code too if above code doesn't work.
#php
echo $string;
#endphp
I want to break the new lines of texarea input and needs to show that as next line in frond end.
I tried all the below methods..
preg_replace("/\r\n|\r|\n/",'<br/>', Input::get('post_content'));
str_replace('<br />', "\n", Input::get('post_content'));
After inserting data to table, the preg_replace function inserting tag in all line breaks.
But when I echo the table column its showing the tag as like a string.
ScrrenShot..
How to fix this issue.. ?
Judging by the Input::get('post_content') part of the code, this is Laravel framework. If you're also using Blade template, you can print your variable unescaped like this: {!! $content !!}
In my Laravel app I allow users to store some text from a text area. When outputting the text I would like to escape the text retrieved from the DB, but also convert any line breaks from the text into <p> tags. I have a function nl2p() that works well for this, but it gets escaped when I place it inside the triple brackets defeating the purpose: {{{ nl2p($bio) }}}
I tried doing something like this:
<?php $formatted_bio = {{{ $user->bio }}}; ?>
<h2>{{ nl2p($formatted_bio) }}</h2>
but data can't be echoed into a variable like that. Any creative solutions out there I may have overlooked?
Try using the e() helper function Laravel provides. It is basically what Blade calls under the hood when you do the triple braces.
So you'd have:
<h2>{{ nl2p(e($user->bio)) }}</h2>