I would like to know how to remove certain characters in a string inside blade templade, say:
the string is my string_345432.pdf and you just want to remove the number part _345432 and left with my string.pdf
I suppose you should do it in controller, not in template.
You can use regex for that something like this:
preg_replace('/[a-zA-Z.]/', '', $string);
If you're really need it to use in blade template, you can create some additional file with helper function, and use it anywhere you want:
function filterFileName($string) {
return preg_replace('/[a-zA-Z.]/', '', $string);
}
Create helpers.php file
Add your function filterFileName in it.
Add that file in composer.json and load it with composer dump-autoload
Now, you can use it in template:
{{ filterFileName($string) }}
Related
I've some html with {{ soimething }} placeholders in a table.
I would like to get the rendered view from this custom html.
I would like to avoid to manually do string replace.
Is it possible?
Note : I seen suggested questions but I ended finding a more concise way to reach my goal. So I post an answer to this question. Please keep this open.
You can use Blade Facade.
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Blade;
public function __invoke()
{
$name='Peter Pan';
return Blade::render("
<h1> Hello {$name} </h1>
",['name'=>$name]);
}
Found
We can use \Illuminate\View\Compilers\BladeCompiler::render($string, $data)
Where
$string is the text to parse, for example
Hi {{$username}}
$data is the same associate array we could normally pass down to view() helper, for example [ 'username' => $this->email ]
I was missing this from the official doc: https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates
So we can also use
use Illuminate\Support\Facades\Blade;
Blade::render($string, $data)
I have a master.blade.php which contains #yield('page_tagline')
I want to use it like so #section('page_tagline', __('pages.home.tagline'))
This will work if the translation does not contain any html, but it does.
So how can i use it like that without blade escaping it ?
One way to get around this would be to use the HtmlString class:
#section('page_tagline', new \Illuminate\Support\HtmlString( __('pages.home.tagline')))
You could then take this one step further and create a macro for the Str class or a global helper function.
Macro Example
In you AppServiceProvider (or any service provider you want) add the following to the boot method:
Str::macro('html', function ($string) {
return new HtmlString($string);
});
Don't forget to add the following use statements to the class:
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
Then you #section would look something like:
#section('content', Str::html( __('pages.home.tagline')))
You should use
#section('page_tagline')
{!! __('pages.home.tagline') !!}
#endsection
This way you can put HTML inside the tagline
I have file excel.php by the path /resources/lang/en/excel.php
Then in controller I tried to fetch word by key:
use Lang;
echo Lang::get('excel.idEvent');
Also I tried:
dd(echo __('excel.idEvent'));
Whats is right way to do that?
First, your excel.php file must be in the right format:
<?php
return [
'welcome' => 'Welcome to our application'
];
The right way to get it on your blade template in fact it is:
echo __('excel.welcome');
or
echo __('Welcome to our application');
The way to do it on your controller is:
use Lang;
Lang::get('excel.welcome');
If you are not using Facades: use \Illuminate\Support\Facades\Lang;
You can also use the trans() function, ex:
Route::get('/', function () {
echo trans('messages.welcome');
});
If you use JSON translation files, you might have to use __().
Here are all the ways to use:
#lang('...') // only in blade files
__('...')
Lang::get('...')
trans('...')
app('translator')->get('...')
Lang::trans('...')
They all defer to \Illuminate\Translation\Translator::get() eventually.
In a Laravel 5 blade template I have a <script> section.
In this section I need to set a variable to a string that is coming from another blade template.
I tried something like :
<script>
var a = "#include 'sometext.blade.php' ";
</script>
but this obviously doesn't work.
Also, the included blade can contain both single and double quotes, so these need to be escaped somehow, or the Javascript will not be valid.
Any ideas?
Ended up needing similar functionality when working with DataTables, and the additional actions HTML needs to be injected after the fact via jQuery.
First I created a helper function with this (from Pass a PHP string to a JavaScript variable (and escape newlines)):
function includeAsJsString($template)
{
$string = view($template);
return str_replace("\n", '\n', str_replace('"', '\"', addcslashes(str_replace("\r", '', (string)$string), "\0..\37'\\")));
}
Then in your template you could do something like:
$('div.datatable-toolbar').html("{!! includeAsJsString('sometext') !!}");
to include the sometext.blade.php blade template with escaping of quotes and removal of newlines.
I have variable in Controller:
$abc = '#include('partials.formerror', array(.....))';
And I send that variable to view:
\View::share([
"abc" => $abc,
]);
And I want in view:
......
......
#include('partials.formerror', array(....)
......
......
#include('partials.formerror', array(....) is dynamic content from Controller. But it's COMMAND of Blade, not plain text. How can I do that?
You need a string blade compiler like this one:
https://packagist.org/packages/wpb/string-blade-compiler
Laravel doens't allow blade rendering from strings out of the box.
Fyi: you could also use Blade::compileString which imho is not an elegant solution
your answer is here: https://laravel.com/docs/5.2/views#view-composers
You can create a view composer and attache dynamic content to your view.