SEO
The SEO library facilitates the optimization of Search Engine results by managing and injecting dynamic data into HTML tags.
Usage
Initialize a Collection
instance. The Collection
manages all the registered models and hold global seo settings
collection := seo.NewCollection()
// Turn off the default inherit the upper level SEO data when the current SEO data is missing
collection.SetInherited(false)
Register models to SEO
// Register mutiple SEO by name
collection.RegisterSEOByNames("Product", "Announcement")
// Register a SEO by model
type Product struct{
Name string
Setting Setting
}
collection.RegisterSEO(&Product{})
Remove models from SEO
// Remove by struct
collection.RemoveSEO(&Product{})
// Remove by name
collection.RemoveSEO("Not Found")
Configuration
Change the default global SEO name
collection.SetGlobalName("My Global SEO")
Change the default context db key
collection.SetDBContextKey("My DB")
Change the default SEO name
collection.RegisterSEO(&Product{}).SetName("My Product")
Register customized variables
collection.RegisterSEO(&Product{}).
RegisterContextVariables("og:image", func(obj interface{}, _ *Setting, _ *http.Request) string {
// this will render "og:image" with the value of the object in the current request
return obj.image.url
}).
RegisterContextVariables("Name", func(obj interface{}, _ *Setting, _ *http.Request) string {
return obj.Name
})
Register setting variable
This variable will be saved in the database and available as a global variable while editing SEO settings.
collection.RegisterSEO(&Product{}).RegisterSettingVaribles(struct{ProductTag string}{})
Render SEO html data
// Render Global SEO
collection.RenderGlobal(request)
// Render SEO by name
collection.Render("product", request)
// Render SEO by model
collection.Render(Product{}, request)
Customization
You can customize your SEO settings by implementing the interface and adding functions such as l10n and publish.
Suppose MySEOSetting
implemented the above interface
type MySEOSetting struct{
QorSEOSetting
// publish
// l10n
}
Use SetSettingModel
function to set it
collection.SetSettingModel(&MySEOSetting{})
Example
Definition
Collection
manages all the registered models and hold global seo settings.
SEO
provides system-level default page matadata.
You can use seo setting at the model level, but you need to register the model to the system SEO