Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

laravel blade 内で scss を使うディレクティブ @scss

Posted at

ずぼらなのでblade内でscssが使いたくなります。

使い方

blade内で @scss@endscss で挟んだ領域で、scssをcssに変換してくれます。

hoge.blade.php

<style>
@scss
p{
  span{
     i{
        color:red;     
     }
  }
}
@endscss
</style>

設定方法

leafo/scssphpをインストール

ScssServiseProvider

app/Providers/ScssServiseProvider
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;

class ScssServiceProvider extends ServiceProvider
{
    public function boot()
    {
        require app_path('../vendor/leafo/scssphp/scss.inc.php');

        Blade::directive('scss', function ($expression) {
            return '<?php $__env->startComponent(\'components.scss\'); ?>';
        });

        Blade::directive('endscss', function ($expression) {
            return '<?php echo $__env->renderComponent(); ?>';
        });
    }

    public function register()
    {
        //
    }
}

config/app.php にProvider追加

config/app.php
    'providers' => [
    ...
    App\Providers\ScssServiceProvider::class,
    ...

scss.blade.php

resources/views/components.scss.blade.php
{!! (new scssc())->compile((string)$slot) !!}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?