如何在 CakePHP 中创建 CSS 内部样式表
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
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.我建议你使用 php 变量作为样式类,并直接使用它作为 css 类。例如。
并在您的电子邮件模板中使用它,因为
即使我不知道有什么方法可以在电子邮件中包含样式表,并且如果您创建一些类,这些类将无法在电子邮件模板中使用。
这就是我在项目中使用 css 的方式。
I would like to suggest you to use php variable as style class and use it directly as css class. For example.
And use it in your email template as
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.
你可以像普通html一样将css放在电子邮件布局中(使用html电子邮件布局)
you can put the css in email layout like normal html (use html email layout)
查看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.
我使用了包含在脚本标签中的 PHP 包含函数,它运行得非常好!在默认电子邮件视图中:
I used PHP's include function enclosed in a script tag and it worked perfectly! In the default email view:
您可以内联编写 css:
You can write css inline: