Skip to content

Commit

Permalink
Added: made dateOrDiff value configurable + config publishable
Browse files Browse the repository at this point in the history
  • Loading branch information
djl997 committed Jun 9, 2023
1 parent fa52ebc commit 5257f66
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
[![Total Downloads](https://img.shields.io/packagist/dt/djl997/blade-shortcuts.svg?style=flat-square)](https://packagist.org/packages/djl997/blade-shortcuts)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

Blade Shortcuts is a library of handy Blade Directives as listed below. The goal is to have less repetitive (base) logic in views, shorter code and/or better readability.
Blade Shortcuts is a library of clever Blade Directives as listed below. The goal is to have less repetitive (base) logic in your Blade Views, overall shorter code and better readability.

## Requirements
Blade Shortcuts requires PHP 8+ and Laravel 6+.

## Installation
You can install the package via composer:
```bash
```sh
composer require djl997/blade-shortcuts
```

## Usage
After installation, all directives should be usable immediately. If you experience problems on first use or after updating the package, try running `php artisan view:clear`.
After installation, all directives should be usable immediately. If something goes wrong at first use or after an update, `php artisan view:clear` should clear the issue.

## All directives
## Contents
- [App Name](#app-name)
- [Boolean](#boolean)
- [Config](#config)

- [Dates](#dates)
- date
- datetime
Expand Down Expand Up @@ -73,8 +72,9 @@ Other options:
@date(now()->subWeek()) <!-- November 8, 2022 -->
@date(now()->subWeek(), 'dateOrDiff') <!-- November 8, 2022 -->
```
> If the time difference is more than 23 hours, ‘dateOrDiff’ will automatically show the date in a localized format instead of ‘x time ago’ or ‘in x time’. You can adjust this threshold in the config file: `php artisan vendor:publish --tag=blade-shortcuts-config`.
Or try shortcuts for datetime, time, year, month or day (also in the correct localized format):
Try shortcuts for datetime, time, year, month or day (also in the correct localized format):
```blade
@datetime(now()) <!-- November 8, 2022 3:04 PM -->
@time(now()) <!-- 3:04 PM -->
Expand Down Expand Up @@ -146,6 +146,11 @@ laravel framework
```
Find all available methods in [Laravel Docs](https://laravel.com/docs/10.x/helpers#fluent-strings-method-list).

## Publish config
```sh
php artisan vendor:publish --tag=blade-shortcuts-config
```

## Changelog
Please see [GitHubs releases section](https://github.com/djl997/blade-shortcuts/releases) for more information on what has changed recently.

Expand Down
22 changes: 22 additions & 0 deletions config/blade-shortcuts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

return [


/*
|--------------------------------------------------------------------------
| Date Or Diff Options
|--------------------------------------------------------------------------
|
| This value defines the hours after which you want to change from
| 'x time ago' or 'in x time' to a localized date format in the
| @date(now(), 'dateOrDiff') Blade Directive.
|
| Performed comparison:
| $date->diffInHours() > YOUR-VALUE
|
*/

'dateOrDiff' => 23

];
2 changes: 1 addition & 1 deletion src/BladeShortcutsBladeDirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function date(string $expression): string
if(count($arr) === 2) {
switch ($arr[1]) {
case "'dateOrDiff'":
return "<?php if(!empty($arr[0])) { if(\Carbon\Carbon::parse($arr[0])->diffInHours() > 23) { echo Carbon\Carbon::parse($arr[0])->translatedFormat(__('blade_directives::format.date')); } else { echo Carbon\Carbon::parse($arr[0])->diffForHumans(['options' => Carbon\Carbon::ONE_DAY_WORDS]); } } else { echo ''; } ?>";
return "<?php if(!empty($arr[0])) { if(\Carbon\Carbon::parse($arr[0])->diffInHours() > config('blade-shortcuts.dateOrDiff')) { echo Carbon\Carbon::parse($arr[0])->translatedFormat(__('blade_directives::format.date')); } else { echo Carbon\Carbon::parse($arr[0])->diffForHumans(['options' => Carbon\Carbon::ONE_DAY_WORDS]); } } else { echo ''; } ?>";
break;

default:
Expand Down
8 changes: 7 additions & 1 deletion src/BladeShortcutsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class BladeShortcutsServiceProvider extends ServiceProvider
public function boot()
{
$this->loadTranslationsFrom(__DIR__.'/../lang', 'blade_directives');

$this->publishes([
__DIR__.'/../config/blade-shortcuts.php' => config_path('blade-shortcuts.php'),
], 'blade-shortcuts-config');

/**
* Config
Expand Down Expand Up @@ -117,6 +121,8 @@ public function boot()
*/
public function register()
{
// ..
$this->mergeConfigFrom(
__DIR__.'/../config/blade-shortcuts.php', 'blade-shortcuts'
);
}
}

0 comments on commit 5257f66

Please sign in to comment.