How to directly call the laravel sanitize function? - laravel

In a blade file one can do this:
{{ $someVariable }}
This sanitizes $someVariable as opposed to calling it like this:
{!! $someVariable !!}
What PHP function is called for the first case? Is there a way to do this outside of a blade file?

The function that ends up being called is e, for 'escape'.
"Encode HTML special characters in a string."
{{ ... }} is replaced with <?php echo e(...); ?>.
It is defined in vendor/laravel/framework/src/Illuminate/Support/helpers.php. It calls htmlspecialchars but also handles special objects that are Htmlable or DeferringDisplayableValue.
"The e function runs PHP's htmlspecialchars function with the double_encode option set to true by default" - Laravel 9.x Docs - Helpers - String Helpers - e
On a side note, this is not sanitizing, it is just escaping.

According to the Laravel documentation you can do it with htmlspecialchars()
Example:
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
https://www.php.net/manual/en/function.htmlspecialchars.php

Related

How to render HTML when inside {{}} on Brade with ternary operator in Laravel 5.7?

I have the follow code in Blade using a ternary operator:
<td>{{isset($arrTemp[$ccc->id]) ? "<a hfet='".url('/cc/'.$cc->id)."'>".count($arrTemp[$cc->id])."</a>": 'N/A'}}</td>
If it find somenthing for the array key $cc->id, should thisplay the value with the link atteched to it.
But the page is rendering <a hfet='http://my.test/cc/56526235'>4</a> the string itself.
What am I missing?
When you use {{ }} the output is automatically escaped to prevent XSS attacks. You can use {!! !!} instead, which will not escape the string.
Source: https://laravel.com/docs/5.4/blade#displaying-data

Laravel blade template once echoing variable and once not error

I recently found strange behavior of blade in Laravel 5.8.
Let's say I have in file
#extends('layout')
#section('title','Dodaj playlistę')
#section('content')
{{$hosts = \App\Host::all()}}
<h2>Dodaj audycję do bazy</h2>
in the middle of the file I have
{{$hosts = \App\Host::all()}}
#foreach ($hosts as $man)
<option value='{{$man->id}}'>{{$man->name}} </option>
#endforeach
The problem is I always get the first $hosts variable echoed, while second not. What the hell? Such variable shouldn't be echoed at all because the command is only variable value attribution.
I've checked all my routes, there is no dd() or var_dump() command anyhere.
Looks like possibly a typo pushing for two sections within one. It should normally throw an error, but might be getting confused with the different language.
Change #section('title','Dodaj playlistę') to #section('title') and give it a try.
Also, set the var directly in the foreach for better clarity:
#foreach ($hosts as $man)
Becomes
#foreach(\App\Host::all() as $man)
{{$hosts = \App\Host::all()}} that will echo "\App\Host::all()"
you can
#php
$hosts = \App\Host::all()
#endphp
but it is discouraged to do in a view
see here How to Set Variables in a Laravel Blade Template

how to pass dynamic value in #include as blade name in laravel5.4

This is my code in laravel blade
#foreach($modules as $module) // {{ mod.$module['module_name'] }}
giving me right value
#section('content')
#include("mod.$module['module_name']") // mod is my folder name and
$module['module_name'] value is
coming from database
#endsection
#endforeach
{{ mod.$module['module_name'] }} is giving me correct value but when i pass it to #include it gives me error. Please help me !!!
I think it has to do with the way the blade template translates the #include directive into compiled PHP. Including the snippet from the compiled PHP file would help, but try this:
#include( 'mod.' . $module['module_name'] )
Looking at some of my past code, you can definitely use a variable in an include. In my case I simply built the desired filename in the controller and passed it along to the view, so my include statement was simply:
#include( $filename )

how to define a variable in laravel blade

How can define a variable in the view side(blade) in laravel?
I found that I can do it in this way:
<?php $var = 'something' ?>
But is there any way to do this like {{ $var = 'something' }} or #var1 = 'something' ?(ofcourse without printing it)
I agree with #Kiril Ivanov answer, but if you still want to do that you can use
#php ($variable = 'test')
Thanks
no, there is no way to define a variable with blade syntax except using the php syntax you have pointed. actually it is not a good practice to define variables in your views and do complex stuff except loops and conditional statements
yes there is a way to do this
first assign your variable like this
{{ $yourvariable='' }}
and after than u can manipulate the variable
#if ($abc['type']=='youresult')
{{ $yourvariable='success'}}
#endif
Hpe this works

anchor() function in code igniter

I want to use the anchor function to route a view to a controller, however I wanna pass in some variables such as $groupid,$jurisdictionid and append them to the uri segment argument.
For instance,
$groupid=(input by user)
$jurisdictionid=(input by user)
anchor ('pagetohit/$groupid/$jurisdictionid')
how do I format this correctly since I want those varaibles to take on the values they are assigned before the anchor function takes them and routes the url.
anchor() is internally using site_url(), so you can:
anchor(array('pagetohit', $groupid, $jurisdictionid));
Try to use double quotes instead of single quotes.
anchor("pagetohit/$groupid/$jurisdictionid")
Variables inside double quotes will be replaced with their values automatically. PHP Docs
We Can Pass More Than One Value Using This.
For Ex, it will look like this ://...../welcome/deletefiles/3/1
<?php echo anchor("welcome/deletefiles/".$row1->FileNo.'/'.$row1->FileID,
'<span class="glyphicon glyphicon-trash" style="color:red;"></span>',
array('onclick' => "return confirm('Do you want delete this record')"))?>
It is for passing a single value
<?php echo anchor("Home_control/add/{$value->id}",'Add Blog',['class'=>'btn btn-info']); ?>
It is for passing a multiple value
<?php echo anchor("Home_control/add/{$value->id}/{$value->ids}",'Add Blog',['class'=>'btn btn-info']); ?>
or use this
<?php echo anchor("Home_control/add/$value",'Add Blog',['class'=>'btn btn-info']); ?>
1st parameter => Controller name,function name,parameter.
2nd parameter=> Name
3rd parameter=> html attributes
It's simple write double quotes (" ") instead of single quotes ('').
When any variable resides double quote then it takes its value not send direct variable.
Example:
anchor("controller/function/$id");

Resources