Friday, March 24, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

Building stylesheets using Leaf – The.Swift.Dev.

learningcode_x1mckf by learningcode_x1mckf
September 21, 2022
in Swift
0
Building stylesheets using Leaf – The.Swift.Dev.
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Encoding and decoding data using the Hummingbird framework

Hummingbird routing and requests – The.Swift.Dev.

Building a Scrollable Custom Tab Bar in SwiftUI

A fast CSS demo mission

The very first step is so as to add Leaf as a dependency to your mission. You need to observe that Leaf 4 just isn’t completed but and these model new options are solely obtainable from the tau pre-release.


import PackageDescription

let package deal = Package deal(
    identify: "myProject",
    platforms: [
       .macOS(.v10_15)
    ],
    dependencies: [
        
        .package(url: "https://github.com/vapor/vapor", from: "4.32.0"),
        .package(url: "https://github.com/vapor/leaf", .exact("4.0.0-tau.1")),
        .package(url: "https://github.com/vapor/leaf-kit", .exact("1.0.0-tau.1.1")),
    ],
    targets: [
        .target(name: "App", dependencies: [
            .product(name: "Leaf", package: "leaf"),
            .product(name: "Vapor", package: "vapor"),
        ]),
        .goal(identify: "Run", dependencies: ["App"]),
        .testTarget(identify: "AppTests", dependencies: [
            .target(name: "App"),
            .product(name: "XCTVapor", package: "vapor"),
        ])
    ]
)


We’re able to import Leaf in your Swift recordsdata, since there’s a new LeafFileMiddleware obtainable as a part of Leaf we’ll create some publicly obtainable template recordsdata and use this middleware to render them. Create a brand new Public listing inside the basis folder of the mission and place an new index.html file there. You may as well use a .leaf extension, however for the sake of simplicity (and Xcode syntax highlighting causes) we’ll use the .html extension this time.


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta identify="viewport" content material="width=device-width, initial-scale=1">
    <title>#(title)</title>
    <hyperlink rel="stylesheet" href="/css/model.css">
</head>
<physique>
    <header>
        <h1>#(title)</h1>
    </header>
</physique>
</html>

Fairly fundamental HTML5 boilerplate code, besides that we’ll print the title utilizing a Leaf tag. We’ll set a worth for this context variable via some Swift code in a second. Within the head part we additionally import our css/model.css stylesheet file. Now you need to create a css folder contained in the Public listing and place a model.css file within it.

* 
    margin: 0;
    padding: 0;

physique 
    font-family: -apple-system, system-ui, BlinkMacSystemFont, "Helvetica", "Segoe UI", Roboto, Ubuntu;
    font-size: 16px;
    line-height: 1.4em;
    background: #(background);

h1 
    padding: #(padding);

@media (max-width: 599px) 
@media (min-width: 600px) 
@media (min-width: 900px) 
@media (min-width: 1200px) 
@media (min-width: 1800px) 

Since this file is “secretly” a leaf template file we are able to use the #(variable) syntax to print out values. We’re going to cross a background shade key and a padding key with some customized values as context variables.

Now let me present you how one can configure this new LeafFileMiddleware, so we are able to render each our html and css templates.

import Vapor
import Leaf
    
public func configure(_ app: Software) throws 

    if !app.setting.isRelease 
        LeafRenderer.Choice.caching = .bypass
    

    LeafFileMiddleware.defaultMediaType = .html
    LeafFileMiddleware.processableExtensions = ["leaf", "html", "css", "js"]
    LeafFileMiddleware.contexts = [
        .css: [
            "background": "#eee",
            "padding": "16px",
        ],
        .html: [
            "title": "Hello world!"
        ],
    ]
    
    if let lfm = LeafFileMiddleware(publicDirectory: app.listing.publicDirectory) 
        app.middleware.use(lfm)
    
    app.views.use(.leaf)

First we disable the cache, however that is a fairly apparent chunk of code, subsequent we set the default media sort to html. This will probably be used to set the Content material-Kind header if the file extension within the request is an unknown sort. The processableExtensions property will inform the LeafFileMiddleware to course of and render solely these recordsdata, every part else with a distinct extension will probably be streamed identical to once you use an everyday FileMiddleware.

As you possibly can see we are able to set completely different context values for particular media sorts, in our case all of the css recordsdata can use the background and padding properties and each html file can reap the benefits of the title context variable. It’s also doable to set them via a subscript syntax:

LeafFileMiddleware[.css] = [
    "background": "green",
    "padding": "16px",
]

LeafFileMiddleware[.html] = [
    "title": "Hello world!"
]

The final step is to create the precise middleware with a publicDirectory argument. This listing is the placement the place the system will search for publicly obtainable recordsdata and if wanted they are often processed as common Leaf templates. You may as well setup listing indexing via the LeafFileMiddleware, however that is a distinct matter.

When you navigate to the http://localhost:8080/index.html deal with you need to see your rendered index.html file with the suitable stylesheet utilized to it. After all you possibly can register a customized route and render your templates utilizing the standard Assets / Views location if wanted, however I simply wished to point out you this cool trick, because it allows us to serve public recordsdata utilizing a extra dynamic strategy.



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Encoding and decoding data using the Hummingbird framework

by learningcode_x1mckf
March 22, 2023
0
Encoding and decoding data using the Hummingbird framework

HTTP is all about sending and receiving information over the community. Initially it was solely utilized to switch HTML paperwork, however these days we use HTTP to switch...

Read more

Hummingbird routing and requests – The.Swift.Dev.

by learningcode_x1mckf
March 17, 2023
0
Hummingbird routing and requests – The.Swift.Dev.

Routing on the server facet means the server goes to ship a response primarily based on the URL path that the consumer referred to as when firing up...

Read more

Building a Scrollable Custom Tab Bar in SwiftUI

by learningcode_x1mckf
March 10, 2023
0
Building a Scrollable Custom Tab Bar in SwiftUI

Whether or not you’re making a social media app or a productiveness device, the tab bar interface can improve the consumer expertise by making it extra intuitive and...

Read more

Beginner’s guide to server-side Swift using the Hummingbird framework

by learningcode_x1mckf
March 8, 2023
0
Beginner’s guide to server-side Swift using the Hummingbird framework

Swift on the Server in 2023 Three years in the past I began to focus on Vapor, the preferred web-framework written in Swift, which served me very...

Read more

What’s new in Swift 5.8 – Hacking with Swift

by learningcode_x1mckf
March 8, 2023
0
What’s new in Swift 5.8 – Hacking with Swift

Though many main Swift modifications are at present percolating by way of Swift Evolution, Swift 5.8 itself is extra of a clear up launch: there are additions, sure,...

Read more
Next Post
What Is Bun.js and Why Is the JavaScript Community Excited About It?

What Is Bun.js and Why Is the JavaScript Community Excited About It?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

The top Java training courses and bundles from TechRepublic Academy of 2022

The top Java training courses and bundles from TechRepublic Academy of 2022

December 5, 2022
Data Oriented Programming in Java

Data Oriented Programming in Java

November 20, 2022
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

C++ just overtook Java as the world's most popular programming … – TechRadar

February 21, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java Developer Survey Reveals Increased Need for Java … – PR Newswire
  • What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy
  • Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?