LogoLogo
9.0.x
9.0.x
  • Overview
    • About Varbase
    • Roadmap
    • Release Cycle and Supported Versions
    • How to Get Support
  • Developers
    • Getting Started
    • Installing Varbase
      • Requirements
      • Installing Varbase with DDEV
      • Installing Varbase with Lando
    • Understanding Varbase
      • Basic Concepts
      • Core Components
        • Varbase Core
          • Varbase Admin
          • Varbase Page
          • Varbase Security
          • Varbase Webform
          • Varbase Default Content
          • Varbase Tour
        • Varbase Media
        • Varbase Editor
        • Varbase Email
        • Varbase SEO
        • Varbase Workflow
        • Varbase Layout Builder
          • Varbase Landing page (Layout Builder)
          • Varbase Heading Block
          • Varbase Rich Text Block
          • Varbase HTML Code Block
          • Varbase Media (Image/Video) Block
          • Varbase Gallery Block
        • Varbase Total Control Dashboard
        • Vartheme Claro
        • Vartheme BS4
      • Optional Components
        • Varbase API
        • Varbase Internationalization
        • Varbase Media Hero Slider
        • Varbase Carousels
        • Varbase Blog
        • Varbase Search
        • Varbase Social Single Sign-On
        • Varbase Media Twitter
        • Varbase Media Instagram
        • Varbase Content Planner
        • Varbase Bootstrap Paragraphs
          • Varbase Bootstrap Paragraphs Text and Image
        • Varbase Landing Page (Paragraphs)
      • Additional Components
        • Varbase Media Header
        • Varbase FAQs
        • Varbase Reports
        • Varbase Commerce
        • Varbase YouTube Import
      • Development Components
        • Varbase Development
        • Varbase Style Guide
        • Reroute Email
        • Varbase Updates Helper
    • Configuring a Varbase Site
      • Navigation
        • Defining Active Menu Trail Using Menu Position
      • Using View Modes Inventory
      • Content Moderation and Workflows
      • URL Aliases in Varbase/Drupal
        • Configuring URL Aliases
        • Managing URL Aliases
      • Managing URL Patterns for Multilingual Websites
      • Managing Date and Time Formats to Display Across the Site
      • Using Varbase Layout Builder
        • Creating a Landing Page With Varbase Layout Builder
        • Activating Varbase Layout Builder for Content Types
      • Configuring Pathologic When Going Live
      • Configuring SEO Features
        • Disallow Oembed Media Links
      • Configuring Security Features
        • Spam Protection
        • Password Policies
        • Other Security Kits
        • Flood Contorl
        • Enable HTTP Password Authentication
      • Configuring JSON:API Features
      • Configure Varbase Media Hero Slider
      • Configuring Varbase Media Header
      • Configuring Varbase Mailer Settings
        • Configure Symfony Mailer
        • Configure Swift Mailer
        • Switch From Swift Mailer to Symfony Mailer
      • Entity Definition Update Manager Class
      • Module Installer Factory Class
    • Theme Development with Varbase
      • Understanding The Vartheme Base Theme
      • Creating Your Own Theme
      • Local Tasks and Moderation Sidebar
      • Add Preloaded Fonts in Vartheme
      • Switch From Node SASS to Dart SASS for Old Sub-Themes
    • Launching a Varbase Site to Production
    • Updating a Varbase Site
      • Understanding Varbase Updater Package
      • Handling Patches When Updating
      • Handling Configuration Updates
      • Updating Varbase to work with Composer 2.0
      • Updating Drush to the Latest Stable Version
      • Version Update Guides
        • Updating Varbase ~9.0 to Drupal 10
        • Switch from CKEditor 4 to CKEditor 5 in Varbase ~9.1.0
        • Updating from Varbase 8.x to 9.x
    • Varbase Patches
    • Frequently Asked Questions (FAQs)
    • Extending Varbase
      • Overriding Varbase
      • Creating Your Own Feature
      • Creating A Sub Profile From Varbase
        • Varbase Sub Profile Generator
        • Varbase Sub Profile Basic Template
        • Varbase Sub Profile Basic Project Template
      • Install Needed YARN and Gulp Tools
      • Compiling Provided Component Styles
      • Check Standards/Practice Coding And Linting
  • Content Designers
    • Terminology
    • Accessing the Administration Area
      • Website Environments
    • User Management
      • Allow Email/Username Login & Registration
      • Default Roles and Site Personas
        • Edit Roles' Permissions
        • Managing Roles
      • Add User
      • Edit User
      • Masquerading as another user
    • Dashboard
      • Reports
    • Content Structure
      • EntityQueues
      • Menu Management
        • Add Menu Items
        • Update Menu Items
        • Adding a Page to the Menu
      • Taxonomies
        • Manage Vocabulary
        • Edit Vocabulary
        • List Terms
        • Add Terms
        • Reorder and Edit Terms
      • Webforms
        • Setting up Emails Handler Conditions
        • Setting up Confirmations Message Settings Types
    • Content Management
      • Create Content
        • Add a Basic page
        • Add a Landing page
        • Add Blog
        • Add Media
          • Image
          • Remote video
          • Video
      • Modify Content
      • Delete Content
      • Preview Content
      • Bulk Edit Content
      • Content Moderation
      • Publish Content
      • URL Alias
    • About Layout Builder
      • Section
      • Custom Blocks
        • Heading Block
        • Basic Block
        • Media Block(Image/Video)
        • Rich-Text Editor “WYSIWYG”
      • Managing Blocks
    • Search Engine Optimization
      • XML Sitemap
      • Meta Tags in Varbase
      • URL Redirects
      • Domain Redirects
      • Prevent a Single Content From Being Indexed
    • Site Configuration
      • System
        • Clearing Varbase Cache
        • Google analytics
        • Mail templates
      • User interface
      • Development
      • Varbase settings
      • Web services
    • Common Error Handling Tips
  • Contributing
    • Contributing to Documentation
Powered by GitBook
On this page
  • Steps to Add a New Web Font
  • 1. Add Web Font Files in the Fonts Folder
  • 2. Add the @font-face CSS File to a Library
  • 3. Add the Font to The preload.twig File
  • 4. Clear Cache And Test The Font
  • Content of the Preload.twig File
  • Available Variables
  • Links to Learn by Example
  1. Developers
  2. Theme Development with Varbase

Add Preloaded Fonts in Vartheme

PreviousLocal Tasks and Moderation SidebarNextSwitch From Node SASS to Dart SASS for Old Sub-Themes

Last updated 1 year ago

Following the new way in the new Drupal default front end theme {% include '@olivero/includes/preload.twig' with { olivero_path: olivero_path } only %}

Check the code on

The html.html.twig file

The preload.twig file

Having the following in Vartheme BS4 and VARTHEM_BS4_SUBTHEME. And the include.

{%- include '@vartheme_bs4/includes/preload.twig' with { vartheme_bs4_path: vartheme_bs4_path, html_dir: html_attributes['dir'] } only -%}
{%- include '@VARTHEME_BS4_SUBTHEME/includes/preload.twig' with { VARTHEME_BS4_SUBTHEME_path: VARTHEME_BS4_SUBTHEME_path, html_dir: html_attributes['dir'] } only -%}

Custom Vartheme Sub Themes are able to add their preloaded assets on demand.

Steps to Add a New Web Font

1. Add Web Font Files in the Fonts Folder

Example: The font

  • Go to the web font page and Download the family.

  • Check the license before using the web font

You can use them freely in your products & projects - print or digital, commercial or otherwise.

This isn't legal advice, please consider consulting a lawyer and see the full license for all details.

Not recommended to use not licensed font!

Buy a license if it was possible and budget for a unique branded font.

Or hire a typography font designer.

  • Copy the list of web font files to "PROJECT_PATH/themes/custom/MYTHEME/fonts/lora/webfonts"

  • Create the "PROJECT_PATH/themes/custom/MYTHEME/fonts/lora/css/lora.css" file

  • Define the @font-face for the Lora font.

/*
 * Lora https://fonts.google.com/specimen/Lora
 */
 @font-face {
  font-family: 'Lora';
  font-style: normal;
  font-weight: 400;
  src: url(../webfonts/Lora-Regular.ttf) format('truetype');
}
@font-face {
  font-family: 'Lora';
  font-style: normal;
  font-weight: 700;
  src: url(../webfonts/Lora-Bold.ttf) format('truetype');
}
  src: url(../webfonts/Lora-Regular.eot);
  src: url(../webfonts/Lora-Regular.eot?#iefix) format('embedded-opentype'),
       url(../webfonts/Lora-Regular.woff2) format('woff2'),
       url(../webfonts/Lora-Regular.woff) format('woff'),
       url(../webfonts/Lora-Regular.ttf) format('truetype');

For sure including a remote web font, But issues with preloading may start follow up

2. Add the @font-face CSS File to a Library

  • List the "PROJECT_PATH/themes/custom/MYTHEME/fonts/lora/css/lora.css" file in libraries to be activated in the theme

global-styles:
  css:
    theme:
      fonts/lora/css/lora.css: {}

3. Add the Font to The preload.twig File

Recommended to add for faster loading of font in pages

  • Edit the "PROJECT_PATH/themes/custom/MYTHEME/templates/includes/preload.twig" file. Add the link rel preload and the patch to the font file with as="font" and type

<link rel="preload" href="/{{ vartheme_bs4_path }}/fonts/lora/Lora-Regular.ttf" as="font" type="font/tff" crossorigin="anonymous">

4. Clear Cache And Test The Font

Content of the Preload.twig File

Change the logic in this file in the generated theme for the project. To match the need of your website.

Available Variables

  • vartheme_bs4_path: Returns the path to the Vartheme BS4 theme. In the Vartheme Sub theme, the variable could be mythemename_path

  • html_dir: contains the language direction. It will either be 'ltr' or 'rtl'.

Example of logic:

  • Multilingual websites may use a language selection for preloaded assets.

  • Sections of the website may have a custom prelead assets for CSS/JS.

Links to Learn by Example

Check the following links:

Vartheme Fonts folder:

Download directly from or from the source in

These fonts are licensed under the .

Having more file formats if that was available by

Example: The font

Have a look at "Preload critical assets to improve loading speed" for more ideas on what/when/where to use preload.

Vartheme html.html.twig file:

Vartheme preload.twig file:

Preload critical assets to improve loading speed
Preload W3C Candidate Recommendation 26 June 2019
Olivero
Olivero
https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/themes/olivero/templates/layout/html.html.twig#L43
https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/themes/olivero/templates/includes/preload.twig
Lora
Lora
fonts.google.com
github.com
https://github.com/cyrealtype/Lora-Cyrillic
Open Font License
web font conversion tools
Lora
https://web.dev/preload-critical-assets/
https://git.drupalcode.org/project/vartheme_bs4/-/tree/9.0.x/fonts
https://git.drupalcode.org/project/vartheme_bs4/-/blob/9.0.x/templates/system/html.html.twig#L69
https://git.drupalcode.org/project/vartheme_bs4/-/blob/9.0.x/templates/includes/preload.twig