kashifkhan0771 / utils

Common Utilities library for Go
MIT License
5 stars 3 forks source link

[FEATURE] Enhanced HTML Templating Utilities for html/template #32

Open kashifkhan0771 opened 1 week ago

kashifkhan0771 commented 1 week ago

Feature Description

Enhance Go’s html/template library by incorporating a comprehensive set of utility functions. This will enable more expressive and powerful templating, making it easier to handle inline conditionals, loops, and a variety of string, numeric, and date operations within templates. The feature should include a robust suite of built-in functions


Use Case

Dynamic Content Generation: Simplify the creation of dynamic and personalized HTML templates for web applications. With conditional and looping capabilities, content like user dashboards, email templates, or reports can be generated more efficiently. Static Site Generators: Use templating to generate static HTML pages from data sources, with the ability to apply formatting, loops, and conditional rendering. Web Frameworks: For those building custom web frameworks or microservices, the enhanced utilities would streamline template rendering and improve maintainability of HTML templates. HTML Emails: Generate responsive and personalized HTML email templates with conditional logic for customizing content based on user data, such as tailored greetings, product recommendations, or personalized offers. The utility functions simplify email template creation while ensuring all content is safe and well-formatted.


Proposed Solution

Checkout text templates implementation and do similar for html templates. The code should be added under templates package and html.go file.


Pseudo Code


tmpl := `
{{ "<p>Welcome</p>" | safeHTML }} <!-- Ensures safe HTML rendering -->
{{ if isNil .User }}Hello, Guest{{ else }}Hello, {{ .User }}{{ end }}
{{ not .IsActive }}
{{ range uniq (split "red,blue,red,green" ",") }}
    Color: {{ . }}
{{ end }}
{{ formatDate now "Jan 02, 2006" }}
`

result, _ := RenderHTMLTemplate(tmpl, data)
kashifkhan0771 commented 2 days ago

Does someone want's to pick this up? I am looking forward to include this in next release and the implementation should be easier as the text template implementation is already done and this one should be done similarly