how can I display html with variable from database in blade - html

I have HTML code with variable in database I want to display the code with html design and variable data in blade
Database Code
<ul class="dropdown-menu dropdown-menu-right">
<li>
#foreach ($block->options as $option)
<a>{{$option->name}}</a>
#endforeach
</li>
</ul>
Blade View
#foreach ($blocks as $block)
{!! $block !!}
#endforeach
I have already display the html code but the variable still as strings
enter image description here

Before saving your data in database, use the php built-in function htmlentities() on the html code string like this:
$html_code = '<p class="any-class">Lorem Ipsum / Random Text</p>';
$encode = htmlentities($html_code);
/* Insert the $encode in the database cell where you want to store html */
And then when displaying the cell data with html tags using blade, you do this {{!! html_entity_decode($block) !!}}
This will work fine!!! Cheers!!

Related

How to concatenate strings in an html attribute using ejs?

I am trying to make a link to a random file using ejs template engine. I have a javascript string variable named randomProject which is in a file called "case". However, I don't know how to concatenate these 2 strings together.
I have tried to use "plus" sign as in <a href=<% "/case/" + randomProject%>> but it did not work.
index.ejs (before I wanted to make a random link)
<a href='/case/portfolio-website'>
<h2>Portfolio Website</h2>
</a>
index.ejs (after I wanted to make a random link, which does not work now)
<a href=<% "/case/" + randomProject%>>
<h2><%= randomProject %></h2>
</a>
main.js
const projects = ['gochiso','junction','portfolio-website','tedx-flyer','tedx-website','thirty-logo-challenge']
const randomNum = Math.floor(Math.random() * projects.length);
const randomProject = projects[randomNum]
directory
case
gochiso.ejs
portfolio-website.ejs
thirty-logo-challenge.ejs
junction.ejs
tedx-website.ejs
A couple of ways you could concatenate:
Using template literal
<a href="<%= `/case/${randomProject}` %>">
Regular ejs to output escaped html
<a href="/case/<%= randomProject %>">

Print HTML from database as HTML [duplicate]

I have a string returned to one of my views, like this:
$text = '<p><strong>Lorem</strong> ipsum dolor <img src="images/test.jpg"></p>'
I'm trying to display it with Blade:
{{$text}}
However, the output is a raw string instead of rendered HTML. How do I display HTML with Blade in Laravel?
PS. PHP echo() displays the HTML correctly.
You need to use
{!! $text !!}
The string will auto escape when using {{ $text }}.
For laravel 5
{!!html_entity_decode($text)!!}
Figured out through this link, see RachidLaasri answer
You can try this:
{!! $text !!}
You should have a look at: http://laravel.com/docs/5.0/upgrade#upgrade-5.0
Please use
{!! $test !!}
Only in case of HTML while if you want to render data, sting etc. use
{{ $test }}
This is because when your blade file is compiled
{{ $test }} is converted to <?php echo e($test) ?>
while
{!! $test !!} is converted to <?php echo $test ?>
There is another way. If object purpose is to render html you can implement \Illuminate\Contracts\Support\Htmlable contract that has toHtml() method.
Then you can render that object from blade like this: {{ $someObject }} (note, no need for {!! !!} syntax).
Also if you want to return html property and you know it will be html, use \Illuminate\Support\HtmlString class like this:
public function getProductDescription()
{
return new HtmlString($this->description);
}
and then use it like {{ $product->getProductDescription() }}.
Of course be responsible when directly rendering raw html on page.
When your data contains HTML tags then use
{!! $text !!}
When your data doesn't contain HTML tags then use
{{ $text }}
Try this. It worked for me.
{{ html_entity_decode($text) }}
In Laravel Blade template, {{ }} wil escape html. If you want to display html from controller in view, decode html from string.
You can do that using three ways first use if condition like below
{!! $text !!}
The is Second way
<td class="nowrap">
#if( $order->status == '0' )
<button class="btn btn-danger">Inactive</button>
#else
<button class="btn btn-success">Active</button>
#endif
</td>
The third and proper way for use ternary operator on blade
<td class="nowrap">
{!! $order->status=='0' ?
'<button class="btn btn-danger">Inactive</button> :
'<button class="btn btn-success">Active</button> !!}
</td>
I hope the third way is perfect for used ternary operator on blade.
you can do with many ways in laravel 5..
{!! $text !!}
{!! html_entity_decode($text) !!}
Use {!! $text !!}to display data without escaping it. Just be sure that you don’t do this with data that came from the user and has not been cleaned.
To add further explanation, code inside Blade {{ }} statements are automatically passed through the htmlspecialchars() function that php provides. This function takes in a string and will find all reserved characters that HTML uses. Reserved characters are & < > and ". It will then replace these reserved characters with their HTML entity variant. Which are the following:
|---------------------|------------------|
| Character | Entity |
|---------------------|------------------|
| & | & |
|---------------------|------------------|
| < | < |
|---------------------|------------------|
| > | > |
|---------------------|------------------|
| " | " |
|---------------------|------------------|
For example, assume we have the following php statement:
$hello = "<b>Hello</b>";
Passed into blade as {{ $hello }} would yield the literal string you passed:
<b>Hello</b>
Under the hood, it would actually echo as <b>Hello<b&gt
If we wanted to bypass this and actually render it as a bold tag, we escape the htmlspecialchars() function by adding the escape syntax blade provides:
{!! $hello !!}
Note that we only use one curly brace.
The output of the above would yield:
Hello
We could also utilise another handy function that php provides, which is the html_entity_decode() function. This will convert HTML entities to their respected HTML characters. Think of it as the reverse of htmlspecialchars()
For example say we have the following php statement:
$hello = "<b> Hello <b>";
We could now add this function to our escaped blade statement:
{!! html_entity_decode($hello) !!}
This will take the HTML entity < and parse it as HTML code <, not just a string.
The same will apply with the greater than entity >
which would yield
Hello
The whole point of escaping in the first place is to avoid XSS attacks. So be very careful when using escape syntax, especially if users in your application are providing the HTML themselves, they could inject their own code as they please.
This works fine for Laravel 5.6
<?php echo "$text"; ?>
In a different way
{!! $text !!}
It will not render HTML code and print as a string.
For more details open link:- Display HTML with Blade
By default, Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
According to the doc, you must do the following to render your html in your Blade files:
{!! $text !!}
Be very careful when echoing content that is supplied by users of your application. You should typically use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.
If you want to escape the data use
{{ $html }}
If don't want to escape the data use
{!! $html !!}
But till Laravel-4 you can use
{{ HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) }}
When comes to Laravel-5
{!! HTML::link('/auth/logout', 'Sign Out', array('class' => 'btn btn-default btn-flat')) !!}
You can also do this with the PHP function
{{ html_entity_decode($data) }}
go through the PHP document for the parameters of this function
html_entity_decode - php.net
Try this, It's worked:
#php
echo $text;
#endphp
For who using tinymce and markup within textarea:
{{ htmlspecialchars($text) }}
On controller.
$your_variable = '';
$your_variable .= '<p>Hello world</p>';
return view('viewname')->with('your_variable', $your_variable)
If you do not want your data to be escaped, you may use the following syntax:
{!! $your_variable !!}
Output
Hello world
{!! !!} is not safe.
Read here: https://laravel.com/docs/5.6/blade#displaying-data
You can try:
#php
echo $variable;
#endphp
If you use the Bootstrap Collapse class sometimes {!! $text !!}
is not worked for me but {{ html_entity_decode($text) }} is worked for me.
I have been there and it was my fault. And very stupid one.
if you forget .blade extension in the file name, that file doesn't understand blade but runs php code. You should use
/resources/views/filename.blade.php
instead of
/resources/views/filename.php
hope this helps some one

Displaying Json string in Handlebars using node.js

Using node.js and handlebars I am trying to display a query using JSON.Stringify() function
javascript:
res.render('search', {"data" : JSON.stringify(data)});
the handblebars code search.handlebars:
<div>
{{data}}
</div>
which displays:
[{"media_name":"Edge of Darkness (2010)"},{"media_name":"Tim (1979)"},{"media_name":"We Were Soldiers (2002)"},{"media_name":"\"The Tonight Show with Jay Leno\" (1992) {(#20.49)} (archive footage)"}, ...]
I am trying to loop through this code using
{{#each}}
<li>
{{data.media_name}}
</li>
{{/each}}
so that i can pull out individual indexes: data[4] and have "Edge of Darkness (2010)" formatting without the {}. But doesn't seem to work.

How to store a number of a text to a variable (cucumber/capybara)?

I'm creating a ticket with Cucumber and Capybara, but when it's created I receive an alert with a confirmation message on the HTML page:
Ticket 6168218 created
How could I store just the number of this text on a variable?
This is the HTML code:
`<div id="messages" class="clearfix">
<div class="success global alert-default form-section">
<ul>
<li><i class="fa fa-check"></i>Ticket 6168218 created.</li>
</ul>
<strong>x</strong>
</div>
</div>`
You can get the text of the element with
find('.success li').text #change the .success selector if you need more specificity
then you can extract the number using a regex. All together that would be
ticket_no = /Ticket (\d+) created/.match(find('.success li').text)[1]

Using html-tags within HTMTL::link_to_route()

In Laravel, how can I use html-tags when linking to a route via HTML::link_to_route()?
Example of what I have:
<li>
{{ HTML::link_to_route( "books_new", "New Book" ) }}
</li>
What I would like to do:
<li>
{{ HTML::link_to_route(
"books_new",
"<span class='icon-book'></span>New Book"
) }}
</li>
I know this is not the answer you want to hear - but you cannot pass html via link_to_route.
The problem is the output from the HTML class is escaped automatically. So if you try to pass this:
{{ HTML::link_to_route('author','<img src="'.URL::base().'assets/images/image.jpg" alt="icon" />')) }}
it comes out like this:
<img src="http://laravel3.dev/assets/images/image.jpg" alt="icon" />
which will just be text on the screen - no image. Instead you need to use URI::to_route('author') and generate the link yourself. So make a helper a like this (not tested):
function link_to_route_image($route, $image)
{
$m = '<a href="'.URL::to_route($route).'">'
. '<img>'.$image.'</img>'
. '</a>';
return $m;
}
How about something like this?
<li>
<span class='icon-book'></span>New Book
</li>
If you're using "Font Awesome", just adding the class to anchor tag as someone mentioned would be fine for most cases because "Icon classes are echoed via CSS :before". You might need a bit of adjustment in CSS; but it might be better in terms of semantic mark-up.
<a href="{{ URL::route('empdelete', array('id' => $employee->id)) }}">
<img src="{{ asset('images/tick-red.jpg') }}" alt="DRC" id="DRCS-logo" /></a>
You can not have HTML markup with HTML::.... (class) , in the documentation they say that anything that is passed as a parameter to the class is escaped with an HTML entity function to make front-end safer!
You can include font awesome or icon into Laravel Blade Template using this code, i already use and work perfect.
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit
If you're using "Font Awesome", just adding the class to anchor tag as someone mentioned would be fine for most cases because "Icon classes are echoed via CSS :before".
So this is working for me:
<li>
{{ HTML::link_to_route( "books_new", "New Book", null, ['class' => 'fa fa-edit'] ) }}
</li>
So far as I know, Laravel doesn't allow you to do that. To me, it seems out of standards.
Rather, apply a class called icon-book to your anchor tag, and then use the class to put the icon inside your anchor as a 'background-image`.
HTML::link_to_route('books_new', 'New Book', array('class' => 'icon-book'))
Alternatively:
Insert the span tag inside the li tag
Assign the icon-book class to the li tag