fontawesome

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2020 License: MIT Imports: 4 Imported by: 0

README

Go(lang) SSR Font Awesome library

A free (as in beer), open-source Go(lang) library for server side rendering Font Awesome icons.

Setup

  1. go get -u github.com/MagnusFrater/fontawesome
  2. Download the Font Awesome for Desktop package.
  3. Once downloaded, find metadata/icons.json (full path: fontawesome-free-X.X.X-desktop/metadata/icons.json).
  4. Copy the icons.json file into your repository (e.g. repo/static/).

Example

  1. git clone https://github.com/MagnusFrater/fontawesome
  2. cd examples/example-server/
  3. go run example-server.go
  4. Visit localhost:8080 in your favourite browser!
package main

import (
	"log"
	"net/http"
	"text/template"

	"github.com/MagnusFrater/fontawesome"
)

func main() {
	// initialize a Font Awesome library by loading the `icons.json` file
	fa, err := fontawesome.New("static/icons.json")
	if err != nil {
		log.Fatalln(err)
	}

	// create a FuncMap and create a new mapping for the new Font Awesome library's `SVG` function
	funcMap := template.FuncMap{
		"fontawesome": fa.SVG,
	}

	// call the 'fontawesome' mapped function in your template(s); it takes two parameters:
	// 1) icon prefix (e.g. "fab" = brands, "fal" = light, "far" = regular, "fas" = solid)
	// 2) icon name
	const exampleTemplate = `
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Go(lang) SSR Font Awesome library</title>
    <meta name="description" content="Go(lang) SSR Font Awesome library">
    <meta name="author" content="Todd Griffin">

    <style>svg{width:5em;height:5em;}</style>
  </head>
  <body>
    {{fontawesome "fas" "home"}}
    {{fontawesome "fas" "hamburger"}}
    {{fontawesome "fas" "dice"}}
    {{fontawesome "fas" "american-sign-language-interpreting"}}
  </body>
</html>
`
	// create a template, add the `funcMap`, and parse the `exampleTemplate`
	tmpl, err := template.New("fontawesomeTest").Funcs(funcMap).Parse(exampleTemplate)
	if err != nil {
		log.Fatalf("parsing: %s", err)
	}

	// create a simple http route
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		tmpl.Execute(w, nil)
	})

	// run the example server
	log.Println("Listening on localhost:8080!")
	log.Fatal(http.ListenAndServe(":8080", nil))
}
Result

Example Server Result

Credits

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Icon

type Icon struct {
	Changes   []string `json:"changes"`
	Ligatures []string `json:"ligatures"`
	Search    struct {
		Terms []string `json:"terms"`
	} `json:"search"`
	Styles  []string       `json:"styles"`
	Unicode string         `json:"unicode"`
	Label   string         `json:"label"`
	Voted   bool           `json:"voted"`
	SVG     map[string]SVG `json:"svg"`
	Free    []string       `json:"free"`
}

Icon is a Font Awesome icon.

type Library

type Library struct {
	Path string
	// contains filtered or unexported fields
}

Library is a container for Font Awesome icons.

func New

func New(path string) (fa *Library, err error)

New returns a new Font Awesome Library loaded with data from the given path.

func (*Library) Icon

func (fa *Library) Icon(name string) (Icon, error)

Icon returns the Font Awesome icon with the given name.

func (*Library) SVG

func (fa *Library) SVG(prefix, name string) (template.HTML, error)

SVG returns the given icon as a raw SVG element.

type SVG

type SVG struct {
	LastModified int      `json:"last_modified"`
	Raw          string   `json:"raw"`
	ViewBox      []string `json:"viewBox"`
	Width        int      `json:"width"`
	Height       int      `json:"height"`
	Path         string   `json:"path"`
}

SVG is a Font Awesome icon's SVG data.

type TemplateIcon

type TemplateIcon struct {
	Name   string `json:"name"`
	Prefix string `json:"prefix"`
}

TemplateIcon is how a Font Awesome icon is defined in a template.Template.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL