是否有将 Google 字体添加到 Laravel 项目的最佳实践?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28847108/
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:03:32  来源:igfitidea点击:

Is there a best practice for adding a Google Font to a Laravel Project?

csslaravelfontsgoogle-font-api

提问by cchapman

I apologize that I'm not even sure where I would get started in terms of doing pre-research myself. I hope this isn't an opinion-based question, but a specific Laravel usage question. I'm curious if there is a specific way to go about including a Google font, or multiple Google fonts in a Laravel project. I could easily drop it in as if it were any other web page, but my instincts tell me that there's probably a more robust way of using the fonts than just placing the

我很抱歉,我什至不确定我自己会从哪里开始进行预研究。我希望这不是一个基于意见的问题,而是一个特定的 Laravel 使用问题。我很好奇是否有一种特定的方法可以在 Laravel 项目中包含 Google 字体或多种 Google 字体。我可以很容易地将它放入任何其他网页,但我的直觉告诉我,使用字体可能比仅仅放置

<link href='http://fonts.googleapis.com/css?family=Oxygen:400,300' rel='stylesheet' type='text/css'>

include line in a main Blade layout template. Sure, I might even be able to shorten it with Blade with {{ HTML::style(...) }}or such, but that still doesn't seem to fully utilize the Laravel framework.

在主 Blade 布局模板中包含行。当然,我什至可以用 Blade with 之{{ HTML::style(...) }}类的东西来缩短它,但这似乎仍然没有充分利用 Laravel 框架。

For example, let's say I want to change the specific font I'm using: If I'm using multiple templates, I would have to go in and change each of the include lines where that font is introduced. But ideally, I would want to have just one google-fontvariable where I could adjust it in just one place.

例如,假设我想更改我正在使用的特定字体:如果我使用多个模板,我将不得不进入并更改引入该字体的每个包含行。但理想情况下,我只想有一个google-font变量,我可以在一个地方调整它。

And if it wouldn't be too much to ask, would you also be able to explain any of the theory on where and how these font variable should/could be stored?

如果不会问太多,您是否还能够解释关于这些字体变量应该/可以存储在何处以及如何存储的任何理论?

采纳答案by Andy Fleming

I'm not sure if I completely follow your question, but there are a number of approaches, but this sounds like more of a template inclusion/extension issues more than anything related to the fonts themselves.

我不确定我是否完全遵循您的问题,但是有多种方法,但这听起来更像是模板包含/扩展问题,而不是与字体本身相关的任何问题。

You could include a snippet so you don't have change the font in multiple places:

您可以包含一个片段,这样您就不必在多个地方更改字体:

In the places where you want to include the font in the head of a template:

在要在模板头部包含字体的地方:

@include('snippets.fonts')

In the snippets/fonts.blade.php:

snippets/fonts.blade.php

<link href='http://fonts.googleapis.com/css?family=Oxygen:400,300' rel='stylesheet' type='text/css'>

You'll still need to handle the CSS though somewhere though. So you'll have to have something like:

不过,您仍然需要在某处处理 CSS。所以你必须有类似的东西:

h1 { font-family: Oxygen, Arial, sans-serif; }

All in all though, there are only 2 places you would have to update the font.

总而言之,只有 2 个地方需要更新字体。

回答by Mitab Shrestha

There is the simple way to do it.. {{ HTML::style('http://fonts.googleapis.com/css?family=Playfair+Display') }}

有一个简单的方法来做到这一点.. {{ HTML::style(' http://fonts.googleapis.com/css?family=Playfair+Display') }}