Skip to content

Laravel Bootstrap Alert macro

Laravel allowing us to extend the HTML facade with additional functionality. we use the HTML::macro() method to extend the HTML facade with our own methods.So here is the Twitter Bootstrap alert macro.

Place the below macro code into your app/start/global.php file to register it.

HTML::macro('alert', function($type, $message, $head = null,$noHead = false) {
switch ($type) {
	case 'success': //green color
		$head = $head ? $head : 'Success';
		break;
	case 'danger': //red color
		$head = $head ? $head : 'Error';
		break;
	case 'warning': //yellow color
		$head = $head ? $head : 'Warning';
		break;
	case 'info': //blue color
		$head = $head ? $head : 'Info';
		break;
}
return $noHead ? '
' . $message . '
':'
'. $head .': ' . $message . '
'; });

How to use

Then place place this code in your blade template.

With Default head

{{ HTML::alert('success', 'your message') }} 
// output:  
{{ HTML::alert('danger', 'your message') }} 
// output : 
{{ HTML::alert('warning', 'your message') }} 
// output : 
{{ HTML::alert('info', 'your message') }} 
//output : 

With custom head

{{ HTML::alert('success', 'your message','Success message') }} 
// output:  
{{ HTML::alert('danger', 'your message','Danger message') }} 
// output : 
{{ HTML::alert('warning', 'your message','Warning message') }} 
// output : 
{{ HTML::alert('info', 'your message','Info message') }} 
//output : 

Without head

{{ HTML::alert('success', 'your message',null,true) }} 
// output:  
{{ HTML::alert('danger', 'your message',null,true) }} 
// output : 
{{ HTML::alert('warning', 'your message',null,true) }} 
// output : 
{{ HTML::alert('info', 'your message',null,true) }} 
//output : 
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments