dovzrfr0506 2019-06-28 09:00
浏览 161
已采纳

使用外部CSS通过Laravel View Composer为页面的一部分设置样式

I'm trying to use external css to style a section of my page - specifically, the form section of my registration page. I'm using the default app.css that ships with Laravel. However, I don't want to include app.css in my html header as it gives me undesired effect on other parts of the page. So I want it to style only my html forms within the page.

Currently, I've used either the asset() or HTML::style() methods like this within my form section:

@section('form')
<style> @import "{{ asset('css/app.css') }}"; </style>
<form>...</form>
@endsection

OR

@section('form')
{{ HTML::style('css/app.css') }}
<form>...</form>
@endsection

Both method loads the style correctly, but affects the entire page instead of only the form elements.

I tried using the ViewComposer class to solve this problem by setting a variable in ViewComposer to my desired style - returning it only when I request the required view:

class ViewComposer
{
  public function compose(View $view)
  {
    $data = [];
    switch($view->getName())
    {
      ...
      case 'sections.register':
        $this->data = ['style'=>"<style> @import \"". asset('css/app.css') . "\"; </style>"];
        break;
    }

    return $view->with($this->data);
  }
}

However, when I render the sections.register sub-view, I get the style variable like this:

@section('form')
{{ $style ?? '' }}
<form>...</form>
@endsection

the output on the browser is not parsed as css but displayed as-is:

<style> @import "{{ asset('css/app.css') }}"; </style>

So, is there a way I can parse external css for only a given view section within the html page and can it be achieved using the ViewComposer class?

UPDATE: I was trying a few things and used this:

@section('form')
{!! $style ?? '' !!}
<form>...</form>
@endsection

The css is parsed but still applied to the entire page. I still need it applied to only the form section.

  • 写回答

2条回答 默认 最新

  • douzhan8303 2019-06-30 04:45
    关注

    First of all, importing or loading css per-view will be bad for the performance of the application. So, using View Composer to load in css is not advisable. I took a cue from Denis Ćerić's answer, though it wasn't clear at first glance.

    Also, the accepted answer on this post made things a little clearer.

    The right way to achieve this is to use a css preprocessor. Popular ones are less and sass. I used sass because it is currently adopted by Laravel.

    I installed sass on my windows machine following the instructions here.

    Create a new scss file: app-custom.scss in the same folder as app.css.

    Modify app-custom.scss using nested imports:

    .app-form
    {
        @import 'app';
    }
    

    Generate app-custom.css using the sass command on Windows command line:

    sass app-custom.scss app-custom.css
    

    Change the class of your form to app-form:

    @section('form')
    <form class='app-form'>...</form>
    @endsection
    

    Include app-custom.css in your header using link tag:

    <head>
        <link href="{{ asset('css/app-custom.css') }}" rel="stylesheet">
    </head>
    

    and you are done.

    HINT: if you want to use the style in app.css for multiple separate sections of your page, you can still achieve this from a single scss file. Just include the classes of each section in your scss file like this:

    .section-1, .section-2, .section-3
    {
        @import 'app';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历