Skip to content

Commit 1d7bfe8

Browse files
committed
Initial commit
0 parents  commit 1d7bfe8

File tree

9 files changed

+546
-0
lines changed

9 files changed

+546
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.cache-loader
2+
node_modules
3+
npm-debug.log
4+
yarn-error.log
5+
vendor
6+
.idea

README.md

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
# Roots\Sage Developer Extensions
2+
3+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
4+
[![Build Status](https://img.shields.io/travis/appstract/laravel-blade-directives/master.svg?style=flat-square)](https://travis-ci.org/webstractions/sage955)
5+
6+
A collection of extensions for your Roots\Sage themes.
7+
8+
- **Blade Directives:** @directives for loop, query, sidebar, FontAwesome, and more.
9+
- **Whoops Pretty Debugger:** PHP error handler for cool kids .
10+
- **GetTheImage:** (very soon) Adapted version of Justin Tadlock's plugin for Sage use.
11+
- **Schema Attributes:** (planned) Add schema.org attributes with no fuss.
12+
- **Template Utilities:** (planned) Bootstrap Walker Menu, filter/action configurations, etc.
13+
- **Sage CLI:** (planned) A Sage counterpart to Laravel Artisan.
14+
15+
## Requirements
16+
This package is specifically built for `Roots\Sage 9.0.0-beta.4` and above. It goes without saying that your development server needs to have `Php 7.0` or greater as well.
17+
18+
## Installation
19+
20+
You can install the package via composer:
21+
22+
```bash
23+
composer require webstractions/sage-devtools
24+
```
25+
26+
## Whoops Setup
27+
Add the following to the top of your `app\setup.php` file. The higher up the better, preferrably following all of the `use` statements:
28+
29+
```php
30+
namespace App;
31+
32+
use Roots\Sage\Container;
33+
use Roots\Sage\Assets\JsonManifest;
34+
use Roots\Sage\Template\Blade;
35+
use Roots\Sage\Template\BladeProvider;
36+
use This\Other\Class\Too;
37+
38+
// Copy this part into app\setup.php
39+
function registerWhoops() {
40+
$whoops = new \Whoops\Run;
41+
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
42+
$whoops->register();
43+
}
44+
registerWhoops();
45+
```
46+
47+
## Blade Directives Setup
48+
49+
Add to your `after_theme_setup` action in `app/setup.php` file. It is important that you make this addition **after** Sage's singleton for the `BladeProvider`:
50+
51+
```php
52+
add_action('after_setup_theme', function (){
53+
54+
/**
55+
* Add Blade to Sage container
56+
*/
57+
sage()->singleton('sage.blade', function (Container $app) {
58+
$cachePath = config('view.compiled');
59+
if (!file_exists($cachePath)) {
60+
wp_mkdir_p($cachePath);
61+
}
62+
(new BladeProvider($app))->register();
63+
return new Blade($app['view']);
64+
});
65+
66+
// Copy this part into app\setup.php after_theme_setup action.
67+
// Make sure it follows the Sage singleton for the Blade Provider.
68+
(new \SageDevTools\Blade\DirectivesProvider(sage()))->register();
69+
70+
});
71+
```
72+
73+
## Blade Directives Usage
74+
75+
### @loop
76+
77+
```blade
78+
@loop
79+
80+
{{-- Code inside of the loop --}}
81+
82+
@endloop
83+
```
84+
Output:
85+
86+
```php
87+
if (have_posts()) :
88+
while(have_posts()) :
89+
the_post();
90+
91+
// Code inside of the loop
92+
93+
endwhile;
94+
endif;
95+
```
96+
97+
### @query( $query )
98+
99+
```blade
100+
@query('expression')
101+
102+
{{-- Code inside of your new query loop --}}
103+
104+
@endquery
105+
```
106+
107+
Output:
108+
```php
109+
$__query = new WP_Query(' . $expression . ');
110+
if ($__query->have_posts()) :
111+
while($__query->have_posts()) :
112+
$__query->the_post(); ?>";
113+
114+
// Code inside of your new query loop
115+
116+
endwhile;
117+
endif;
118+
```
119+
120+
### @doaction( $action )
121+
122+
```blade
123+
@doaction('before_header')
124+
```
125+
126+
### @sidebar( $location )
127+
128+
```blade
129+
@sidebar('primary')
130+
```
131+
132+
### @wphead and @wpfooter
133+
134+
```blade
135+
@wphead
136+
@wpfooter
137+
```
138+
139+
### @fa
140+
141+
Quickly output a Font Awesome icon.
142+
143+
```blade
144+
@fa('address-book')
145+
```
146+
147+
148+
### @istrue
149+
150+
Only show when ```$variable``` isset and true.
151+
152+
```blade
153+
@istrue($variable)
154+
This will be echoed
155+
@endistrue
156+
```
157+
158+
Or when you would like to quickly echo
159+
160+
```blade
161+
@istrue($variable, 'This will be echoed')
162+
```
163+
164+
### @isfalse
165+
166+
Same as ```@istrue``` but checks for isset and false.
167+
168+
```blade
169+
@isfalse($variable)
170+
This will be echoed
171+
@endisfalse
172+
```
173+
174+
### @dump and @dd
175+
176+
```blade
177+
@dump($var)
178+
179+
@dd($var)
180+
```
181+
182+
### @mix
183+
184+
Create a HTML element to your Laravel-Mix css or js.
185+
```blade
186+
@mix('/css/app.css')
187+
@mix('/css/app.js')
188+
```
189+
Output:
190+
191+
```blade
192+
<link rel="stylesheet" href="{{ mix('/css/app.css') }}">
193+
<script src="{{ mix('/css/app.js') }}"></script>
194+
```
195+
196+
### @style
197+
198+
Create a ```<style>``` element or ```<link>``` element with a css path.
199+
200+
```blade
201+
@style
202+
body { background: black }
203+
@endstyle
204+
205+
206+
@style('/css/app.css')
207+
```
208+
209+
### @script
210+
211+
Create a ```<script>``` element with or without a js path.
212+
213+
```blade
214+
@script
215+
alert('hello world')
216+
@endscript
217+
218+
219+
@script('/js/app.js')
220+
```
221+
222+
### @inline
223+
224+
Load the contents of a css or js file inline in your view.
225+
226+
```blade
227+
@inline('/js/manifest.js')
228+
```
229+
230+
## License
231+
232+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "webstractions/sage-devtools",
3+
"description": "Extends Sage ~9-beta with handy development tools. ",
4+
"type": "package",
5+
"keywords": [
6+
"webstractions",
7+
"roots",
8+
"sage"
9+
],
10+
"homepage": "https://github.com/webstractions/sage-devtools",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Ron Pugmire",
15+
"homepage": "https://github.com/webstractions",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php": ">=7"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^5.7",
24+
"filp/whoops": "^2.1"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"SageDevTools\\": "src"
29+
}
30+
}
31+
}

config/.gitkeep

Whitespace-only changes.

config/blade-directives.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Directives
8+
|--------------------------------------------------------------------------
9+
|
10+
| This is a list of directives you'd like to register.
11+
| Use the directive name as key, and a closure.
12+
|
13+
*/
14+
15+
'directives' => [
16+
17+
// 'name' => function($expresion) {}
18+
19+
],
20+
21+
];

src/Blade/DirectivesProvider.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace SageDevTools\Blade;
4+
5+
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\ServiceProvider;
7+
use Roots\Sage\Container;
8+
9+
class DirectivesProvider extends ServiceProvider
10+
{
11+
/**
12+
* Bootstrap the application services.
13+
*/
14+
public function boot()
15+
{
16+
if ($this->app->runningInConsole()) {
17+
$this->publishes([
18+
__DIR__.'/../config/blade-directives.php' => config_path('blade-directives.php'),
19+
], 'config');
20+
}
21+
22+
$this->registerDirectives();
23+
}
24+
25+
/**
26+
* Register the application services.
27+
*/
28+
public function register()
29+
{
30+
$this->registerDirectives($this->app);
31+
$this->mergeConfigFrom(__DIR__.'/../../config/blade-directives.php', 'blade-directives');
32+
}
33+
34+
/**
35+
* Register all directives.
36+
*
37+
* @return void
38+
*/
39+
public function registerDirectives($app)
40+
{
41+
$directives = require __DIR__.'/directives.php';
42+
43+
$sageBlade = $app->get('sage.blade');
44+
45+
// $directives = array_merge(
46+
// $directives,
47+
// Config::get('blade-directives.directives')
48+
// );
49+
50+
DirectivesRepository::register($directives, $sageBlade);
51+
}
52+
}

src/Blade/DirectivesRepository.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace SageDevTools\Blade;
4+
5+
// use Illuminate\Support\Facades\Blade;
6+
use Roots\Sage\Container;
7+
8+
class DirectivesRepository
9+
{
10+
/**
11+
* Register the directives.
12+
*
13+
* @param array $directives
14+
* @return void
15+
*/
16+
public static function register(array $directives, $sageBlade )
17+
{
18+
19+
collect($directives)->each(function ($item, $key) use($sageBlade) {
20+
$sageBlade->compiler()->directive($key, $item);
21+
});
22+
}
23+
24+
/**
25+
* Parse expression.
26+
*
27+
* @param string $expression
28+
* @return \Illuminate\Support\Collection
29+
*/
30+
public static function parseMultipleArgs($expression)
31+
{
32+
return collect(explode(',', $expression))->map(function ($item) {
33+
return trim($item);
34+
});
35+
}
36+
37+
/**
38+
* Strip single quotes.
39+
*
40+
* @param string $expression
41+
* @return string
42+
*/
43+
public static function stripQuotes($expression)
44+
{
45+
return str_replace("'", '', $expression);
46+
}
47+
}

0 commit comments

Comments
 (0)