summaryrefslogtreecommitdiff
path: root/tags
diff options
context:
space:
mode:
authorBobby <[email protected]>2025-12-19 18:01:24 +0530
committerBobby <[email protected]>2025-12-19 18:01:24 +0530
commitb1bfec1ce2987d9fe0cc52e5ae9115977fdf8c24 (patch)
tree7080b7dc97522ffe0837a1e0b2965489d7e67664 /tags
parent767297e28d47ee9cf3722054e41caa837f0e68d2 (diff)
downloadlain-b1bfec1ce2987d9fe0cc52e5ae9115977fdf8c24.tar.xz
lain-b1bfec1ce2987d9fe0cc52e5ae9115977fdf8c24.zip
added utils, templates, routes, types, middleware, processors and a whole lot of things for a basic login page
Diffstat (limited to 'tags')
-rw-r--r--tags/tags.go25
-rw-r--r--tags/url.go75
2 files changed, 100 insertions, 0 deletions
diff --git a/tags/tags.go b/tags/tags.go
new file mode 100644
index 0000000..9a20b55
--- /dev/null
+++ b/tags/tags.go
@@ -0,0 +1,25 @@
+package tags
+
+import (
+ "log"
+
+ "github.com/flosch/pongo2/v6"
+)
+
+type templateTag struct {
+ Name string
+ Fn pongo2.TagParser
+}
+
+func Initialize() {
+
+ tags := []templateTag{
+ {"url", url},
+ }
+
+ for _, t := range tags {
+ if err := pongo2.RegisterTag(t.Name, t.Fn); err != nil {
+ log.Println("Failed to register tag:", t.Name, "Error:", err)
+ }
+ }
+}
diff --git a/tags/url.go b/tags/url.go
new file mode 100644
index 0000000..194d00b
--- /dev/null
+++ b/tags/url.go
@@ -0,0 +1,75 @@
+package tags
+
+import (
+ "fmt"
+ "lain/utils/urls"
+ "strings"
+
+ "github.com/flosch/pongo2/v6"
+)
+
+type urlNode struct {
+ routeName string
+ params map[string]pongo2.IEvaluator
+}
+
+func url(doc *pongo2.Parser, start *pongo2.Token, arguments *pongo2.Parser) (pongo2.INodeTag, *pongo2.Error) {
+ routeNameToken := arguments.MatchType(pongo2.TokenString)
+ if routeNameToken == nil {
+ return nil, arguments.Error("expected route name string", nil)
+ }
+ routeName := routeNameToken.Val
+
+ params := make(map[string]pongo2.IEvaluator)
+
+ for arguments.Remaining() > 0 {
+ keyToken := arguments.MatchType(pongo2.TokenIdentifier)
+ if keyToken == nil {
+ return nil, arguments.Error("expected param key identifier", nil)
+ }
+
+ if arguments.Match(pongo2.TokenSymbol, "=") == nil {
+ return nil, arguments.Error("expected '=' after param key", nil)
+ }
+
+ valueExpr, err := arguments.ParseExpression()
+ if err != nil {
+ return nil, err
+ }
+
+ params[keyToken.Val] = valueExpr
+ }
+
+ return &urlNode{
+ routeName: routeName,
+ params: params,
+ }, nil
+}
+
+func (n *urlNode) Execute(ctx *pongo2.ExecutionContext, writer pongo2.TemplateWriter) *pongo2.Error {
+ path, ok := urls.GetFullPath(n.routeName)
+ if !ok {
+ return &pongo2.Error{
+ Sender: "tag:url",
+ OrigError: fmt.Errorf("route not found: %s", n.routeName),
+ }
+ }
+
+ for key, expr := range n.params {
+ val, err := expr.Evaluate(ctx)
+ if err != nil {
+ return err
+ }
+ path = strings.ReplaceAll(path, ":"+key, fmt.Sprintf("%v", val.Interface()))
+ }
+
+ _, err := writer.WriteString(path)
+ if err != nil {
+ return &pongo2.Error{
+ Sender: "tag:url",
+ OrigError: err,
+ }
+ }
+
+ return nil
+}