shortcode

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: GPL-3.0 Imports: 4 Imported by: 7

README

Shortcode Open in Gitpod

Tests Status Go Report Card PkgGoDev

Introduction

Shortcodes are powerful tools that allow you to easily embed interactive elements or create complex layouts on your webpages with just a single line of code.

This makes them especially convenient for Content Management Systems (CMS) where editors can insert pre-defined components without needing to write complex backend code.

Shortcodes as seen in popular platforms like Wordpress, allow you to effortlessly embed interactive elements and create stunning layouts with just a single line of code.

Key Benefits of Shortcodes:

  • Simplicity: Shortcodes provide a user-friendly way to add complex elements without writing HTML or other code.
  • Flexibility: They can be used to create custom content blocks, embed external content, and perform various other functions.
  • Extensibility: Developers can create custom shortcodes to expand the functionality of a CMS.

Installation

go get -u github.com/gouniverse/shortcode

Example

Source file:

<html>
  <body>
[myshortcode id="111"][/myshortcode]
[myshortcode id="222"][/myshortcode]
  </body>
</body>

Your Golang Shortcode

// The shortode function
func myShortcode(content string, args map[string]string) string {
	return "MY SHORTCODE WITH ID " + args["id"]
}

// Use the shortcode
sh, err := NewShortcode(WithBrackets("[", "]"))
parsed := sh.Render(text, "myshortcode", myShortcode)

Result

<html>
  <body>
MY SHORTCODE WITH ID 111
MY SHORTCODE WITH ID 222
  </body>
</body>

Example With Request

In this example, the RenderShortocdes function find search the provided content for shortcodes.

If a supported shortcode is found in the content it will render the corresponding widget.

The request is passed to the shortcode and can be used, i.e. to find the authenticated user, path, etc.

func RenderShortcodes(req *http.Request, content string) string {
	shortcodes := map[string]func(*http.Request, string, map[string]string) string{
		"x-latest-blogs":           widgets.NewLatestBlogsWidget().Render,
		"x-course-list":            widgets.NewCourseListWidget().Render,
		"x-flash-message":          widgets.NewFlashMessageWidget().Render,
		"x-language-dropdown":      widgets.NewLanguageDropdownWidget().Render,
		"x-login-form":             widgets.NewLoginFormWidget().Render,
		"x-register-form":          widgets.NewRegisterFormWidget().Render,
		"x-forgot-password-form":   widgets.NewForgotPasswordFormWidget().Render,
		"x-top-menu-user-dropdown": widgets.NewTopMenuDropdownWidget().Render,
		"x-website-header":         widgets.NewWebsiteHeader().Render,
	}

	sh, err := shortcode.NewShortcode(shortcode.WithBrackets("<", ">"))
	if err != nil {
		return content
	}

	for k, v := range shortcodes {
		content = sh.RenderWithRequest(req, content, k, v)
	}

	return content
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Shortcode

type Shortcode struct {
	// contains filtered or unexported fields
}

Shortcode defines a shortcode engine

func NewShortcode

func NewShortcode(opts ...ShortcodeOption) (*Shortcode, error)

NewShortcode creates a new shortcode engine

func (Shortcode) Render

func (sh Shortcode) Render(str string, shortcode string, fn func(string, map[string]string) string) string

func (Shortcode) RenderWithRequest added in v0.0.3

func (sh Shortcode) RenderWithRequest(req *http.Request, str string, shortcode string, fn func(*http.Request, string, map[string]string) string) string

type ShortcodeOption

type ShortcodeOption func(*Shortcode)

ShortcodeOption options for the shortcode

func WithBrackets

func WithBrackets(bracketOpening string, bracketClosing string) ShortcodeOption

WithBracketType sets the type of bracket used for the shortcode

Jump to

Keyboard shortcuts

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