Skip to content

Commit

Permalink
Merge pull request #71 from jornatf/develop
Browse files Browse the repository at this point in the history
new directives added
  • Loading branch information
teuunn authored May 2, 2023
2 parents 19129ea + bcff544 commit dcd454f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 9 deletions.
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ composer require appstract/laravel-blade-directives

### @istrue

Only show when ```$variable``` isset and true.
Only show when `$variable` isset and true.

```blade
@istrue($variable)
Expand All @@ -35,7 +35,7 @@ Or when you would like to quickly echo

### @isfalse

Same as ```@istrue``` but checks for isset and false.
Same as `@istrue` but checks for isset and false.

```blade
@isfalse($variable)
Expand All @@ -45,7 +45,7 @@ Same as ```@istrue``` but checks for isset and false.

### @isnull

Only show when ```$variable``` is null.
Only show when `$variable` is null.

```blade
@isnull($variable)
Expand All @@ -55,7 +55,7 @@ Only show when ```$variable``` is null.

### @isnotnull

Same as ```@isnull``` but one shows when ```$variable``` is not null.
Same as `@isnull` but one shows when `$variable` is not null.

```blade
@isnotnull($variable)
Expand All @@ -74,10 +74,12 @@ Same as ```@isnull``` but one shows when ```$variable``` is not null.
### @mix

Create a HTML element to your Laravel-Mix css or js.

```blade
@mix('/css/app.css')
@mix('/js/app.js')
```

Output:

```blade
Expand All @@ -87,7 +89,7 @@ Output:

### @style

Create a ```<style>``` element or ```<link>``` element with a css path.
Create a `<style>` element or `<link>` element with a css path.

```blade
@style
Expand All @@ -100,7 +102,7 @@ Create a ```<style>``` element or ```<link>``` element with a css path.

### @script

Create a ```<script>``` element with or without a js path.
Create a `<script>` element with or without a js path.

```blade
@script
Expand All @@ -119,7 +121,6 @@ Load the contents of a css or js file inline in your view.
@inline('/js/manifest.js')
```


### @pushonce

Same as `@push` but will include content one time only. Useful for repeatable blocks.
Expand Down Expand Up @@ -219,14 +220,45 @@ Output data-attributes from an array.

### @haserror

Quickly output for classical ```$errors->has('input_name')``` to determine if any error messages exist for a given field.
Quickly output for classical `$errors->has('input_name')` to determine if any error messages exist for a given field.

```blade
@haserror('input_name')
This input has an error
@endhaserror
```

### @count

Output number of entries.

```blade
@count([1,2,3])
```

### @nl2br

Replaces `\n` into `<br>`.

```blade
@nl2br('foo\n bar\n baz\n')
```

### @snake, @kebab, @camel

Output formatted string (uses Laravel Helpers).

```blade
@snake('fooBar')
// output: 'foo_bar'
@kebab('fooBar')
// output: 'foo-bar'
@camel('foo bar')
// output: 'fooBar'
```

## Testing

```bash
Expand Down
43 changes: 42 additions & 1 deletion src/directives.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

use Appstract\BladeDirectives\Parser;
use Illuminate\Support\Str;
use Appstract\BladeDirectives\Parser;
use Illuminate\Database\Eloquent\Collection;

return [

Expand Down Expand Up @@ -399,4 +400,44 @@
return '<?php endif; ?>';
},

/*
|---------------------------------------------------------------------
| @count
|---------------------------------------------------------------------
|
| Usage: @count([1,2,3])
|
*/

'count' => function ($expression) {
return '<?php echo ' . count(json_decode($expression)) . '; ?>';
},

/*
|---------------------------------------------------------------------
| @nl2br
|---------------------------------------------------------------------
*/

'nl2br' => function ($expression) {
return "<?php echo nl2br($expression); ?>";
},

/*
|---------------------------------------------------------------------
| @kebab, @snake, @camel
|---------------------------------------------------------------------
*/

'kebab' => function ($expression) {
return '<?php echo ' . Str::kebab($expression) . '; ?>';
},

'snake' => function ($expression) {
return '<?php echo ' . Str::snake($expression) . '; ?>';
},

'camel' => function ($expression) {
return '<?php echo ' . Str::camel($expression) . '; ?>';
},
];
14 changes: 14 additions & 0 deletions tests/CountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Appstract\BladeDirectives\Test;

class CountTest extends TestCase
{
public function test_count_is_compiled()
{
$blade = "@count([1,2,3])";
$expected = "<?php echo 3; ?>";

$this->assertSame($expected, $this->blade->compileString($blade));
}
}
30 changes: 30 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Appstract\BladeDirectives\Test;

class HelpersTest extends TestCase
{
public function test_snake_is_compiled()
{
$blade = "@snake('fooBar')";
$expected = "<?php echo 'foo_bar'; ?>";

$this->assertSame($expected, $this->blade->compileString($blade));
}

public function test_camel_is_compiled()
{
$blade = "@camel('foo bar')";
$expected = "<?php echo 'fooBar'; ?>";

$this->assertSame($expected, $this->blade->compileString($blade));
}

public function test_kebab_is_compiled()
{
$blade = "@kebab('foo bar')";
$expected = "<?php echo 'foo-bar'; ?>";

$this->assertSame($expected, $this->blade->compileString($blade));
}
}

0 comments on commit dcd454f

Please sign in to comment.