template_extract

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 4 Imported by: 0

README

template-extract

A Library for extracting placeholders from the templates.

Getting Library

Install the library with

go get -u "github.com/MeowSaiGithub/template-extract

Import the library

import te "github.com/MeowSaiGithub/template-extract"
How to use
        t, err := template.New("test").Parse("Hello {{.Name}}")
	if err != nil {
		panic(err)
	}
	td, _ := te.NewTemplateDataExtractor(t)
	td.ExtractPlaceHolders()
	fmt.Println(td.GetRawData())
	fmt.Println(td.GetCleanData())
	fmt.Println(td.GetMapData())

Raw Data is every action placeholders such as {{.Name}}, {{$name := .Name}}.

Clean Data is only Name from both data of above. It removed duplicated data and remove un-necessary strings.

Map data is map["Name": ""]. This can be used for json marshalling and unmarshalling the request data.

The original code snippet can be found here - https://stackoverflow.com/questions/40584612/how-to-get-a-map-or-list-of-template-actions-from-a-parsed-template. and Credit to all those answers.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TemplateDataExtractor

type TemplateDataExtractor interface {
	// ExtractPlaceHolders processes the template and extract raw, clean and mapped placeholders.
	ExtractPlaceHolders()
	// GetRawData retrieves raw placeholders data.
	GetRawData() []string
	// GetCleanData retrieves clean placeholders data.
	GetCleanData() []string
	// GetMapData retrieves mapped placeholders data.
	GetMapData() map[string]interface{}
	// contains filtered or unexported methods
}

func NewTemplateDataExtractor

func NewTemplateDataExtractor(t *template.Template) (TemplateDataExtractor, error)

NewTemplateDataExtractor creates a new templateDataExtractor struct with the provided template and returns TemplateDataExtractor interface and an error if parameter *template.Template is nil.

Jump to

Keyboard shortcuts

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