📋
pimp
  • Home
  • Installation
  • FAQ
  • Getting Started
    • Welcome
    • Expand commands
    • Run tasks
    • Render templates
    • Execute scripts
    • Next steps
  • User Guide
    • Pimpfile
    • Configuration File
    • Command Expander
    • Task Runner
    • Template Engine
      • Functions
    • Script Engine (PimpScript)
    • Go Library
  • Integrations
    • Bash, Zsh, Fish
Powered by GitBook
On this page
  • 1. Render templates
  • 2. Execute PimpScript
  • 3. Use the pimp template functions with `text/template`
  1. User Guide

Go Library

PreviousScript Engine (PimpScript)NextBash, Zsh, Fish

Last updated 4 years ago

It is possible to import pimp as a standalone Go library. The 3 examples below respectively show you how to print the git branches of a repository by:

  1. rendering a template

  2. executing a PimpScript

  3. using the native package along with the pimp template functions

1. Render templates

package main

import (
    "os"

    "github.com/aymericbeaumet/pimp"
)

func main() {
    _ = pimp.RenderTemplate(os.Stdout, `Git branches in {{pwd}}:
{{- range GitBranches}}
  - {{.}}
{{- end}}
`)
}

2. Execute PimpScript

package main

import (
    "os"

    "github.com/aymericbeaumet/pimp"
)

func main() {
    _ = pimp.ExecuteScript(os.Stdout, `
    printf "Git branches in %s:\n" pwd

    range GitBranches
      printf "- %s\n" .
    end
  `)
}

3. Use the pimp template functions with `text/template`

package main

import (
    "os"
    "text/template"

    "github.com/aymericbeaumet/pimp"
)

func main() {
    t, _ := template.New("git_branches").
        Funcs(pimp.FuncMap()).
        Parse(`Git branches in {{pwd}}:
{{- range GitBranches}}
  - {{.}}
{{- end}}
`)

    _ = t.Execute(os.Stdout, nil)
}
text/template