-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from AccentDesign/add-if-element-839
add if helper func
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/accentdesign/gtml" | ||
"os" | ||
"time" | ||
) | ||
|
||
func Nav(isLoggedIn bool) *gtml.Element { | ||
return gtml.UL( | ||
gtml.NA, | ||
gtml.LI(gtml.NA, gtml.A(gtml.NA, gtml.Text("Home"))), | ||
gtml.If(isLoggedIn, gtml.LI(gtml.NA, gtml.A(gtml.NA, gtml.Text("Profile")))), | ||
) | ||
} | ||
|
||
func main() { | ||
defer func(start time.Time) { | ||
fmt.Println("") | ||
fmt.Println(time.Since(start)) | ||
}(time.Now()) | ||
|
||
nav := Nav(true) | ||
|
||
_ = nav.Render(context.Background(), os.Stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gtml | ||
|
||
// If returns the element if the condition is true, otherwise returns an empty element. | ||
func If(condition bool, element *Element) *Element { | ||
if condition { | ||
return element | ||
} | ||
return Empty() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package gtml | ||
|
||
import "testing" | ||
|
||
// Test the If function with true and false conditions. | ||
func TestIfFunction(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
condition bool | ||
element *Element | ||
expected string | ||
}{ | ||
{"IfTrue", true, Div(NA, Text("Visible")), `<div>Visible</div>`}, | ||
{"IfFalse", false, Div(NA, Text("Visible")), ``}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
node := If(tt.condition, tt.element) | ||
output := renderElement(t, node) | ||
if output != tt.expected { | ||
t.Errorf("Expected %q, got %q", tt.expected, output) | ||
} | ||
}) | ||
} | ||
} |