如何在 CakePHP 中创建 CSS 内部样式表

发布于 2024-12-02 17:12:54 字数 383 浏览 0 评论 0原文

我使用 CakePHP 一段时间了,最​​近需要从应用程序发送电子邮件。不幸的是,我无法找到一种方法来告诉 CakePHP 将 css 作为内部样式表而不是链接直接包含在文档中。我知道人们认为这是一个坏主意,但我的应用程序只向我们公司发送电子邮件,所以我不太担心某人的电子邮件客户端搞砸了。如果我只包含链接,它就不起作用,因为引用是错误的,尽管如果我可以使链接成为绝对链接(http://myserver/css/myfile.css 而不是 /css/myfile.css)是第二好的选择,因为他们可以访问我的服务器。

如果在 Cake 中没有办法做到这一点,是否有一种快速方法可以使用 PHP 读取文件的内容并将其转储到视图中?我想我可以从控制器上做到这一点,不过听起来像是一个糟糕的黑客。

任何想法将不胜感激,谢谢

I have been using CakePHP for a while and recently needed to send emails from an app. Unfortunately I can not figure out a way to tell CakePHP to include the css directly in the document as an internal style sheet instead of a link. I know people think that is a bad idea, but my app is only sending emails to our company so I'm not worried too much about someone's email client messing it up. If I just include the link it doesn't work since the reference is wrong, although if I could make the link an absolute link (http://myserver/css/myfile.css instead of /css/myfile.css) that would be a 2nd best alternative since they would have access to my server.

If there isn't a way to do it in Cake, is there a quick way to just use PHP to read the contents of the file and dump it in the view? I guess I could do that from the controller, sounds like a bad hack though.

Any ideas would be appreciated, thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恋你朝朝暮暮 2024-12-09 17:12:54

您可以使用readfile()直接在视图中打印文件内容。

或者您可以 $this->Html->url('css/yourcss.css', true) 获取文件的完整路径并将其传递给 css 方法。

You could use readfile() to print the file content directly in your view.

Or you could $this->Html->url('css/yourcss.css', true) to get the full path to the file and pass it too the css method.

_失温 2024-12-09 17:12:54

我建议你使用 php 变量作为样式类,并直接使用它作为 css 类。例如。

$class1 = "border : 1px solid #eeeeee; font-family : font1, font2, font3; color : #785634;"

并在您的电子邮件模板中使用它,因为

<div id='my-div' style=<?php echo $class1; ?>>Your div content </div>

即使我不知道有什么方法可以在电子邮件中包含样式表,并且如果您创建一些类,这些类将无法在电子邮件模板中使用。

这就是我在项目中使用 css 的方式。

I would like to suggest you to use php variable as style class and use it directly as css class. For example.

$class1 = "border : 1px solid #eeeeee; font-family : font1, font2, font3; color : #785634;"

And use it in your email template as

<div id='my-div' style=<?php echo $class1; ?>>Your div content </div>

Even I do not know any way to include style sheet in the email, and if you create some classes those will not work in email templates.

So this is how I'm using css in my projects.

猫腻 2024-12-09 17:12:54

你可以像普通html一样将css放在电子邮件布局中(使用html电子邮件布局)

;

you can put the css in email layout like normal html (use html email layout)

<style type="text/css"></style>
冬天的雪花 2024-12-09 17:12:54

查看htmlHelper的代码发现无法完成。

但是,您可以更改助手或使其超载;要以最简单的方式执行此操作,只需在第 371 行到第 378 行之间添加一个新选项。

Reviewing the code of the htmlHelper shows that it can't be done.

However, you can change or overload the helper; to do so in the simplest way just add a new option between line 371 to 378.

╰ゝ天使的微笑 2024-12-09 17:12:54

我使用了包含在脚本标签中的 PHP 包含函数,它运行得非常好!在默认电子邮件视图中:

<script>
    include('css/your_css_doc.css');`
</script>

I used PHP's include function enclosed in a script tag and it worked perfectly! In the default email view:

<script>
    include('css/your_css_doc.css');`
</script>
我恋#小黄人 2024-12-09 17:12:54

您可以内联编写 css:

 <h2 style="color:#818381">Here are the latest stories everyone's talking about...</h2>

You can write css inline:

 <h2 style="color:#818381">Here are the latest stories everyone's talking about...</h2>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文