Last active
February 1, 2019 17:03
-
-
Save nadyshalaby/f74d01b34a73386c7f74c315c61e16fc to your computer and use it in GitHub Desktop.
Coding Laravel Blade Directive to minify embedded JS & CSS code e.g. @minify(js) , @endminify using minification package ``` matthiasmullie/minify ```
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\Facades\Blade; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Blade::directive('minify', function ($engine) { | |
return sprintf(' | |
<?php | |
$minifier = app(\'minifier.%s\'); | |
$content = Blade::compileString(<<<\'BLADE\' | |
', $engine); | |
}); | |
Blade::directive('endminify', function () { | |
return ' | |
BLADE); | |
$minifier->add($content); | |
echo eval(\' ?>\' . $minifier->minify() . \'<?php \'); | |
?> | |
'; | |
}); | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script>$(function(){var actionsTemplate=$('#actions').html();var detailsTemplate=$('#details').html();var table=$('#datatable-icon').DataTable({processing:!0,serverSide:!0,dom:'Bfrtip',responsive:!0,buttons:[{extend:'copy',text:'<i class="far fa-copy"></i>',exportOptions:{columns:':visible'}},{charset:'UTF-16LE',text:'<i class="fas fa-file-csv"></i>',fieldSeparator:'\t',bom:!0,exportOptions:{columns:':visible'}},{extend:'excel',text:'<i class="far fa-file-excel"></i>',exportOptions:{columns:':visible'}},{extend:'pdf',text:'<i class="fas fa-pdf"></i>',enabled:!1},{extend:'print',text:'<i class="fas fa-print"></i>',exportOptions:{columns:':visible'}},{extend:'pageLength',text:'<i class="fab fa-pagelines"></i>',},],language:{"decimal":"","emptyTable":"لا يوجد بيانات حتي الان.","info":"عرض _START_ الي _END_ من _TOTAL_ صفوف","infoEmpty":"عرض 0 الي 0 من 0 صفوف","infoFiltered":"(تصفية من _MAX_ الكل صفوف)","infoPostFix":"","thousands":",","lengthMenu":"عرض _MENU_ صفوف","loadingRecords":"تحميل...","processing":"معالجة...","search":"البحث:","zeroRecords":"لا يوجد بيانات تطابق البحث.","buttons":{pageLength:{_:"%d",'-1':"All"},},"paginate":{"first":"الاول","last":"الاخير","next":"التالي","previous":"السابق"},"aria":{"sortAscending":": اضغط للترتيب تصاعديا","sortDescending":": اضغط للترتيب تنازليا"}}, order:[[4,'desc']],ajax:{url:'http://127.0.0.1:8000/categories'},columns:[{"className":'details-control',"orderable":!1,"searchable":!0,"name":'id',"defaultContent":''},{data:'name'},{name:"created_at",data:'created_at'},{name:"updated_at",data:'updated_at'},{data:'id',render:function(data,type,row,meta){return compile(actionsTemplate,data,'id')},searchable:!1,orderable:!1},],initComplete:function(){$('div.dataTables_wrapper .dt-buttons').after($('#filters-template').html())}});$(document).on('change','[id*=filter-]',function(){table.draw()});$(document).on('click','#print-all',function(){$('#datatable-icon tbody td.details-control').closest('tr').each(function(){var tr=$(this);var row=table.row(tr);row.child(compile(detailsTemplate,row.data())).show();tr.addClass('shown')});$("#datatable-icon").printThis()});$('#datatable-icon tbody').on('click','td.details-control',function(){var tr=$(this).closest('tr');var row=table.row(tr);if(row.child.isShown()){row.child.hide();tr.removeClass('shown')}else{row.child(compile(detailsTemplate,row.data())).show();tr.addClass('shown')}})})</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
@minify(js) | |
$(function () { | |
var actionsTemplate = $('#actions').html(); | |
var detailsTemplate = $('#details').html(); | |
var table = $('#datatable-icon').DataTable({ | |
/** | |
* I could also compile both blade & plain php code | |
*/ | |
@include('site.layouts.scripts.datatables-props') | |
order: [[4, 'desc']], | |
ajax: { | |
url: '{!! route('site.categories.index') !!}' | |
}, | |
columns: [ | |
{ | |
"className": 'details-control', | |
"orderable": false, | |
"searchable": true, | |
"name": 'id', | |
"defaultContent": '' | |
}, | |
{data: 'name'}, | |
{name: "created_at", data: 'created_at'}, | |
{name: "updated_at", data: 'updated_at'}, | |
{ | |
data: 'id', render: function (data, type, row, meta) { | |
return compile(actionsTemplate, data, 'id'); | |
}, searchable: false, orderable: false | |
}, | |
], | |
initComplete: function () { | |
$('div.dataTables_wrapper .dt-buttons').after($('#filters-template').html()); | |
} | |
}); | |
$(document).on('change', '[id*=filter-]', function () { | |
table.draw(); | |
}); | |
$(document).on('click', '#print-all', function () { | |
$('#datatable-icon tbody td.details-control').closest('tr').each(function () { | |
var tr = $(this); | |
var row = table.row(tr); | |
row.child(compile(detailsTemplate, row.data())).show(); | |
tr.addClass('shown'); | |
}); | |
$("#datatable-icon").printThis(); | |
}); | |
// Add event listener for opening and closing details | |
$('#datatable-icon tbody').on('click', 'td.details-control', function () { | |
var tr = $(this).closest('tr'); | |
var row = table.row(tr); | |
if (row.child.isShown()) { | |
// This row is already open - close it | |
row.child.hide(); | |
tr.removeClass('shown'); | |
} | |
else { | |
// Open this row | |
row.child(compile(detailsTemplate, row.data())).show(); | |
tr.addClass('shown'); | |
} | |
}); | |
}); | |
@endminify | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use MatthiasMullie\Minify; | |
class MinifierProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap the application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
} | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->bind('minifier.js', function (){ | |
return new Minify\JS; | |
}); | |
$this->app->bind('minifier.css', function (){ | |
return new Minify\CSS; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment