如何在 Laravel 中将 css 嵌入 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30316548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:34:56  来源:igfitidea点击:

How to embed css to HTML in laravel?

phpcsslaravellaravel-4html-email

提问by cytsunny

First of all, to make things clear, I know I can include my css file by

首先,为了清楚起见,我知道我可以通过以下方式包含我的 css 文件

{{ HTML::style('css/myStyle.css') }}

Which the html to be generated will be:

要生成的 html 将是:

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

The problem is that my view file is used for email and many email client don't load external css file. So I need to make it something like:

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

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

As the css settings will be used for multiple email template, I don't want to hard code it. In pure PHP, this can be done by (reference)

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

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

However, when I do that with laravel, it says:

但是,当我用 laravel 这样做时,它说:

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

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

So, is there a way I can embed the css in laravel? (I am using laravel 4.2)

那么,有没有办法将 css 嵌入到 Laravel 中?(我使用的是laravel 4.2)

回答by GeorgeQ

It appears that you are using a URL instead of the local file path.

看来您使用的是 URL 而不是本地文件路径。

For example

例如

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

will produce your error but the following should not

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

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

回答by umefarooq

if you are using multiple template i will suggest you to create multiple style views which contain just css and you can include them with blade @include

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

style1.blade.php
style2.blade.php

in styles just put css code

在样式中只需放置 css 代码

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

in master.blade.php layout

在 master.blade.php 布局中

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

if it is dynamic you can pass style parameter

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

@include($style)