Laravel blade #include into a Javascript variable? - laravel

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.

Related

How to remove certain characters in a string in blade template

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) }}

Pass html into the second argument of #section

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

Change interpolation brackets - Angular2

I want to use Laravel Blade and AngularJS.
Is some way to change interpolate sintax, change {{text}} for [{text}] or somthing like that?
I already change all components.ts files adding the line:
interpolation: ["{[", "]}"],
but, where I write blade, app breaks...
Thanks a lot everybody ;)
You can define your Blade content tags in your routes.php file:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
EDIT: You can add # in front of the brackets, so Blade won't render it.
#{ var }
or you can pass the directive for blade not to render part of the HTML using #verbatim keyword.
#verbatim
<div>
{{ var }}
</div>
#endverbatim

Laravel 5.2 : Extending Blade by passing an object instead of a string

I want to add some blade directives.
I have in my service provider
Blade::directive('image', function ($media) {
return "<?php echo {$media->getImageUrl()}; ?>";
});
The blade file contain
#image($media)
the $media variable is an object which use the Media model and which contains a public function getImageUrl()which return a string with the url of the image.
When I execute this code, I have this error message
Fatal error: Call to a member function getImageUrl() on string
The object passed in the Blade directive is considered as a string instead of Media object
Is there any way to use $media as an Mediaobject instead of a string ?
The curly braces should be around $media:
Blade::directive('image', function ($media) {
return "<?php echo {$media}->getImageUrl(); ?>";
});
This compiles to:
<?php echo ($media)->getImageUrl(); ?>
As far as I know Blade directives only accept one expression as a string, including the function call braces. So the expression received by the directive is:
"($media)"
After changing directives you should clear the compiled views because Blade is caching the directives, so you would not see your changes taking affect.

Using a Blade directive in a Blade directive

I'm using Laravel 5.1. I am trying to use a Blade directive (#extend) with my custom Blade directive.
Blade::directive('base', function() use ($theme) {
return "#extends($theme)"
});
However, the above code only literally displays the contents (#extends($theme))
Contrary to a comment I made earlier, I think this is possible (but untested) using the blade compiler.
Blade::directive('base', function() use ($theme) {
return Blade::compileString("#extends({$theme})");
});

Resources