Capitalized a blade's variable in Laravel - php

I used to use smarty a lot and now moved on to Laravel but I'm missing something that was really useful. The modification in the template of you're variable.
Let say I have a variable assign as {{$var}}. Is there a way in Laravel to set it to upper case ? Something like: {{$var|upper}}
I sadly haven't found any documentation on it.

Only first character :
You could use UCFirst, a PHP function
{{ucfirst(trans('text.blabla'))}}
For the doc : http://php.net/manual/en/function.ucfirst.php
Whole word
Str::upper($value)
Also this page might have handy things : http://cheats.jesse-obrien.ca/

PHP Native functions can be used here
{{ strtoupper($currency) }}
{{ ucfirst($interval) }}
Tested OK

You can also create a custom Blade directive within the AppServiceProvider.php
Example:
public function boot() {
Blade::directive('lang_u', function ($s) {
return "<?php echo ucfirst(trans($s)); ?>";
});
}
Don't forget to import Blade at the top of the AppServiceProvider
use Illuminate\Support\Facades\Blade;
Then use it like this within your blade template:
#lang_u('index.section_h2')
Source:
How to capitalize first letter in Laravel Blade
For further information:
https://laravel.com/docs/5.8/blade#extending-blade

works double quoted:
{{ucfirst(trans("$var"))}}

Related

is there a way to nest {{}} expressions in Laravel template?

I have a resource route generated by Artisan command as
Route::resource('posts','PostsController');
the URI of this route is posts/{post}/edit which require a dynamic value in the middle. But because I'm using the url() method to link all of my routs I am forced to nest the template expression as:
Edit
This is giving me an error: NotFoundHttpException because it couldn't get the {{$show->id}} part. How can I fix this?
In PostsController, create a new variable that you want to show up in the template. You should be able to use the url() function within a controller. (See here) Pass this value to the template like you normally would.
Just in case: documentation on how to pass values to a template
Use Edit with out the use of {{}} notation.
You need double quote and remove one bracket
Do you looking for something like this?
#foreach($posts as $post)
Edit
#endforeach

Extending blade with a service

I installed graham-campbell/markdown, and it works in the controller. I would like to extend it's functionality to blade so I could use #markdown($variable) but can't figure out how to accomplish that.
This is how my AppServiceProvider's boot method looks like with the added blade directive.
public function boot()
{
Schema::defaultStringLength(191);
Blade::directive('markdown', function ($expression) {
return "<?php echo Markdown::convertToHtml($expression); ?>";
});
}
And in my view
#markdown($comment->comment)
But I"m getting the following error:
Class 'Markdown' not found (View: C:\xampp\htdocs\portfolio\portfolio\resources\views\blog.blade.php)
I've added the use at the top of AppServiceProvider file:
use GrahamCampbell\Markdown\Facades\Markdown;
And still the same error. I've even tried the following directive instead of the one I have posted previously:
Blade::directive('markdown', function ($expression) {
return Markdown::convertToHtml($expression);
});
And although it's frowned upon, I've tried to inject the markdown class into the view
#inject('markdown', 'GrahamCampbell\Markdown\Facades\Markdown')
The error no longer shows, but it simply displays $comment->comment.
If I put #markdown(foo **this**) I get 'foo this' just like I would expect. How do I extract the contents of '$comment->comment' and submit it to be parsed by the markdown compiler?
Also, is it possible to do that without the Facades injection?
[EDIT]
I've solved my issue where it just prints $comment->comment. I've removed any changes to AppServiceProvider... I've removed that use statement and blade directive and just using the following in view
#inject('markdown', 'GrahamCampbell\Markdown\Facades\Markdown')
{!! $markdown::convertToHtml($comment->comment) !!}
But I'm still interesting in using the directive #markdown($variable) without the need for that injection.
The first line of code is correct except that you need to add {} instead of (), please refer to this answer.
so you need to type it like this:{$expression} instead of ($expression).
here as well a good tutorial on how to create a custom directive and you can check laracasts.

how to highlight string in a string in a laravel blade view

Somewhere in my template I have this:
{{ $result->someText }}
Now in this text I want to highlight all words that are in the string
{{ $searchString }}
So I thought I create a new blade directive:
{{ #highlightSearch($result->someText, $searchString) }}
Blade::directive('highlightSearch', function($input, $searchString)...
error: missing argument 2
Found out that directives do not except 2 arguments. I tried every workaround that I could find but none worked. They always return the arguments as a plain string, not even passing the actual values.
I tried adding a helper function like explained here: https://stackoverflow.com/a/32430258/928666. Did not work:
error: unknown function "highlightSearch"
So how do I do this super easy task in laravel? I don't care about the highlighting function, that's almost a one-liner.
The reality is blade directives can't do what you need them to do. Whether or not they should is not a topic I can't help with. However you can instead do this in your service provider:
use Illuminate\Support\Str;
/* ... */
Str::macro('highlightSearch', function ($input, $searchString) {
return str_replace($searchString, "<mark>$searchString</mark>", $input);
//Or whatever else you do
});
Then in blade you can just do:
{!! \Illuminate\Support\Str::highlightSearch($result->someText, $searchString) !!}
I've just tested in Laravel 5.1 it and it works without any problem:
\Blade::directive('highlightSearch', function($input) {
return "<?php echo strtoupper($input); ?>";
});
in boot method of AppServiceProvider
and in view I can use:
#highlightSearch('test')
and it returns TEST as expected.
Make sure you are using Blade from global namespace, also run
php artisan clear-compiled
to make sure everything is refreshed. If it won't help, you can try running
composer dump-autoload
just in case.
EDIT
I've also tested it with additional argument and it really seems not be working as expected, so the most reasonable would be adding helper file (if you don't have any with simple PHP function) as for example:
function highlight_search($text, $searchString)
{
// return whatever you want
}
and later use it it in Blade as any other function like so:
{{ highlight_search('Sample', 'Sam') }}
optionally using
{!! highlight_search('Sample', 'Sam') !!}
if you want highlight_search to output HTML

Laravel develop Blade template Engine

i want to use custom code in blade template engine such as custom function for use into that
my function:
public function viewCurrentDate( $arg=0 ){
return 'date is'.date('y');
}
in Blade template use like with this code
#< viewCurrentDate >#
how to develop blade for custom actions?
Add in the filters.php file
Blade::extend(function ($view) {
return str_replace("#dateY", 'date is'.date('y'), $view);
});
in template :
<h1>#dateY</h1>
Full tutorial about blade extension here : http://blog.zerilliworks.net/blog/2013/04/03/blade-extensions-in-laravel/
This is possible (see markcial's answer) but I think there is a much better way to do it.
Why not just set up a HTML helper class.
class HTMLHelper {
public static function viewCurrentDate($arg = 0){
return 'date is'.date('y');
}
}
You can then add this to echo the date in your blade file:
{{ HTMLHelper::viewCurrentDate() }}
It is slightly more characters (compared to what you wanted), but will be so much more flexible for you as you can add as many helper methods to your class as you like and use them anywhere, not just in blade.
Edit: Whilst markcial's answer is what you are after (tells you how to create a new template string in blade) I'd don't think that is the simplest way to do things. The helper file is much more flexible and reusable. For example, you can use this helper file ANYWHERE in your app. With markcial's answer, you are only allowing blade to use what you have written. To me, it doesn't seem worth it when a more flexible, easier solutions is available.

Laravel 4 Blade #include variable

I was trying to do include with Laravel blade, but the problem is it can't pass the variable. Here's my example code:
file_include.blade.php
<?php
$myvar = "some text";
main.blade.php
#include('file_include')
{{$myvar}}
When I run the file, it return the error "Undefined variable: myvar". So, how can I pass the variable from the include file to the main file?
Thank you.
Why would you pass it from the include to the calling template? If you need it in the calling template, create it there, then pass it into the included template like this:
#include('view.name', array('some'=>'data'))
Above code snippet from http://laravel.com/docs/templates
Unfortunately Laravel Blade engine doesn't support what you expected!.But a little traditional way you can achieve this!
SOLUTION 1 - without Laravel Blade Engine
Step a:
from
file_include.blade.php
to
file_include.php
Step b:
main.blade.php
<?php
include('app/views/file_include.php')
?>
{{$myvar}}
SOLUTION 2 with Laravel Blade Engine
routes.php
$data = array(
'data1' => "one",
'data2' => "two",
);
View::share('data', $data);
Access $data array from Any View like this
{{ $data['data1'] }}
Output
one
Blade is a Template Engine for Laravel. So try passing the value from the controller or you may define it in the routes.php for testing purposes.
#include is used to include sub-views.
I think you must understand the variable scope in Laravel Blade template. Including a template using #include will inherit all variables from its parent view(the view where it was defined). But I guess you can't use your defined variables in your child view at the parent scope. If you want your variable be available to the parent try use View::share($variableName, $variableValue) it will be available to all views as expected.
In this scenarion $myvar would be available only on the local scope of the include call.
Why don't you send the variable directly from the controller?
I suggest you do a classic PHP require if you really want to change your variable (then it's automatically by reference)

Categories