如何在laravel中嵌入css到HTML?

How to embed css to HTML in laravel?

首先,为了清楚起见,我知道我可以通过

包含我的 css 文件
{{ HTML::style('css/myStyle.css') }}

要生成的 html 将是:

<link rel="stylesheet" type="text/css" href="http://myhost/public/css/myStyle.css">

问题是我的视图文件用于电子邮件,许多电子邮件客户端不加载外部 css 文件。所以我需要把它做成这样:

<style>
    .content-of-my-css {
        //content of my css
    }
    // more css
</style>

由于 css 设置将用于多个电子邮件模板,我不想对其进行硬编码。在纯 PHP 中,这可以通过 (reference)

来完成
<style><?php include "path/to/my/css";?></style>

但是,当我使用 laravel 执行此操作时,它会显示:

ErrorException (E_ERROR) include(): http:// wrapper is disabled in the server configuration by allow_url_include=0

那么,有什么方法可以将 css 嵌入到 laravel 中吗? (我正在使用 laravel 4.2)

您使用的似乎是 URL 而不是本地文件路径。

例如

include('http://example.com/css/styles.css');

会产生你的错误,但下面的应该不会

include(public_path().'css/styles.css');

如果您使用多个模板,我建议您创建仅包含 css 的多个样式视图,您可以使用 blade @include

包含它们
style1.blade.php
style2.blade.php

在样式中只需输入 css 代码

<style>
    .content-of-my-css {
        //content of my css
    }
    // more css
</style>

在master.blade.php布局中

<html>
  <head>@include('style1')</head>
</html>

如果它是动态的你可以传递样式参数

@include($style)