Skip to content

Commit bfc059c

Browse files
authored
enhance(directives): Add a @sidebar, @hassidebar, and @endhassidebar directive (Fixes #71) (#92)
2 parents 56019a4 + 5df3ba5 commit bfc059c

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

docs/usage/wordpress.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,17 @@ This is a directive version of [`wp_body_open`](https://developer.wordpress.org/
519519
@postclass('bg-white')
520520
@postclass('bg-white', $post->ID)
521521
```
522+
523+
## @sidebar
524+
525+
`@sidebar` is a simply directive that calls [`dynamic_sidebar`](https://developer.wordpress.org/reference/functions/dynamic_sidebar/).
526+
527+
It comes with two assisting directives `@hassidebar` and `@endhassidebar` that checks for the existence of the sidebar using [`is_active_sidebar`](https://developer.wordpress.org/reference/functions/is_active_sidebar/).
528+
529+
```php
530+
@sidebar('sidebar-primary')
531+
532+
@hassidebar('sidebar-primary')
533+
@sidebar('sidebar-primary')
534+
@endhassidebar
535+
```

src/Directives/WordPress.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,24 @@
582582
return "<?php post_class({$expression}); ?>";
583583
},
584584

585+
/*
586+
|---------------------------------------------------------------------
587+
| @sidebar / @hassidebar / @endhassidebar
588+
|---------------------------------------------------------------------
589+
*/
590+
591+
'sidebar' => function ($expression) {
592+
return "<?php dynamic_sidebar($expression); ?>";
593+
},
594+
595+
'hassidebar' => function ($expression) {
596+
return "<?php if (is_active_sidebar($expression)) : ?>";
597+
},
598+
599+
'endhassidebar' => function () {
600+
return '<?php endif; ?>';
601+
},
602+
585603
/*
586604
|---------------------------------------------------------------------
587605
| @__

tests/Unit/WordPressTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,36 @@
764764
});
765765
});
766766

767+
describe('@sidebar', function () {
768+
it('compiles correctly', function () {
769+
$directive = "@sidebar('sidebar-1')";
770+
771+
$compiled = $this->compile($directive);
772+
773+
expect($compiled)->toBe("<?php dynamic_sidebar('sidebar-1'); ?>");
774+
});
775+
});
776+
777+
describe('@hassidebar', function () {
778+
it('compiles correctly', function () {
779+
$directive = "@hassidebar('sidebar-1')";
780+
781+
$compiled = $this->compile($directive);
782+
783+
expect($compiled)->toBe("<?php if (is_active_sidebar('sidebar-1')) : ?>");
784+
});
785+
});
786+
787+
describe('@endhassidebar', function () {
788+
it('compiles correctly', function () {
789+
$directive = '@endhassidebar';
790+
791+
$compiled = $this->compile($directive);
792+
793+
expect($compiled)->toBe('<?php endif; ?>');
794+
});
795+
});
796+
767797
describe('@__', function () {
768798
it('compiles correctly', function () {
769799
$directive = "@__('Hello World', 'sage')";

0 commit comments

Comments
 (0)