about summary refs log tree commit diff stats
path: root/gomponents.go
Commit message (Collapse)AuthorAgeLines
* Add else part to If/IffAlan Pearce23 hours-3/+22
|
* add MapWithIndex and MapMap helpersAlan Pearce23 hours-0/+20
|
* Set slice capacity based on input in `Map` and `Classes.Render` (#243)Gabe Cook2025-03-03-1/+1
| | | | | | | | | Hey! I just discovered this repo and I love being able to write templates while still having compile-time checks. I was looking through the code and noticed slices aren't currently preallocated in `Map()` and `Classes.Render()`. Preallocating will improve performance since the resulting slice won't have to be grown dynamically during the append loop.
* Remove mention of SVG package in doc commentsMarkus Wüstenberg2024-10-03-2/+3
|
* Fix links in package documentationMarkus Wüstenberg2024-09-24-3/+3
| | | | Apparently, [Node]s doesn't link in Godoc, while [Node]-s does.
* Fix doc comments regarding `Group`Markus Wüstenberg2024-09-24-2/+3
|
* Make `Group` a type (#202)Markus Wüstenberg2024-09-24-17/+11
| | | | | | | | | | | | This changes `Group` to be a type instead of a function, which means it can support both grouping `[]Node` as well as doing variadic-ish slice things like `Group{n1, n2}`. This also means that `Map` can just return a `Group`. Special thanks to @deitrix for this simple and elegant solution that, for some reason or another, has had me perplexed for a long time. Fixes #201.
* Drop support for Go 1.16 and Go 1.17 (#203)Markus Wüstenberg2024-09-19-0/+9
| | | Fixes #184
* Make Groups renderable (#181)Markus Wüstenberg2024-09-19-16/+29
| | | | | | | | | | | | | | This change makes the result of `Group` renderable directly, instead of panicking, with the important caveat that root-level attributes are _ignored_. I don't think this will give problems in practice, as the main use case for rendering `Group` is basically to return root-level elements to the client using something like HTMX. I tried adding a `Fragment`, but it was weird and confusing having two functions (`Group` and `Fragment`) do essentially the same thing, the only difference being whether the argument was a slice of `Node`s or varargs. Fixes #162
* Add links to godoc (#183)Markus Wüstenberg2024-06-26-30/+34
| | | | | Also add an example for `http.Adapt`. Fixes #182
* Adjust documentation on Iff (#179)Markus Wüstenberg2024-06-25-4/+6
|
* Add an `Iff` helper (#172)Julien Tant2024-06-19-0/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I ran into some situation where I want to conditionally render a node if some variable is not nil and obviously I got a panic ```go // package viewmodels type SomePage struct { s *string } // package views func SomePage (vm viewmodels.SomePage) g,Node { return Div( If(vm.s == nil, Text("s is nil"), If(vm.s !- nil, Text("s is " + vm.s), // this will panic when `s` is nil ) } ``` In this situation, go will interpret the code of the second `if` regardless of the condition because the code itself is not in a condition. This PR introduces a new `Iff` helper that accepts a callback. The callback content is only interpreted when it's called, making the code safe: ```go // package viewmodels type SomePage struct { s *string } // package views func SomePage (vm viewmodels.SomePage) g,Node { return Div( Iff(vm.s == nil, func () g.Node { return Text("s is nil") }, Iff(vm.s !- nil, func () g.Node { return Text("s is " + vm.s) }, ) } ``` I'm aware of the `Lazy` effort on the side, but I guess this is no a breaking change and can still exist in addition to the `Lazy` effort. Co-authored-by: Markus Wüstenberg <markus@maragu.dk>
* Document Text/Textf helpers betterMarkus Wüstenberg2023-05-11-2/+2
| | | | | | Both in the readme and package doc. Fixes #133.
* Add Rawf (#114)Markus Wüstenberg2022-11-03-2/+10
| | | Like `Raw`, but interpolates like `Textf`.
* Fix small typo occured -> occurred (#110)Markus Wüstenberg2022-10-17-1/+1
|
* Add generic Map implementation (#108)Markus Wüstenberg2022-09-22-9/+0
| | | | | | | | | | | Instead of waiting for a good time to switch the `Map` implementation, I've decided to just offer two implementations: one for Go versions before 1.18, and one for 1.18 and onwards. This is achieved using build tags in the source files. This is obviously a breaking change for consumers of this library that use Go 1.18 and onwards. See #88
* Make void elements a map (#103)Markus Wüstenberg2022-05-27-19/+39
| | | | | Makes it easier to lookup by name. Also, fixed some doc comments.
* Update package doc to reflect new content (#102)Markus Wüstenberg2022-05-25-5/+11
| | | It hadn't been updated in a while.
* Add examples in test files (#80)Markus Wüstenberg2021-06-08-13/+1
| | | These show up in godoc.
* Escape Attr values (#77)Markus Wüstenberg2021-05-18-1/+1
| | | | | Because this can be a place of injection if untrusted data is passed, escape all attribute values. Fixes #74.
* Return Node from all helpers instead of NodeFunc (#62)Markus Wüstenberg2021-01-07-12/+12
| | | | | This makes it clearer that the helpers return a `Node` of any kind, and that the type is not important. This also streamlines the API, as attribute helpers already return just `Node`.
* Add If helper function (#57)Markus Wüstenberg2020-12-22-3/+16
| | | Used to inline conditional nodes.
* Minor refactoring and comment changes (#51)Markus Wüstenberg2020-12-08-10/+9
|
* Add NodeType which replaces the Placer interface (#45)Markus Wüstenberg2020-11-16-30/+23
| | | The `Placer` interface was a weird interface that tried to abstract away nodes being elements or attributes, but it doesn't really make sense. Now `Nodes` just have a `NodeType`.
* Render correct HTML5 (#44)Markus Wüstenberg2020-11-16-29/+37
| | | | | | | | | Previously, elements of kind void and empty elements generally would be rendered auto-closing (with a final `/` character in the start tag), which is allowed sometimes but arguably wrong. See https://dev.w3.org/html5/spec-LC/syntax.html#end-tags This created problems with for example `textarea` and `script`, which cannot be auto-closing, or the browser renders it wrong. Also clarified in the docs that this library outputs HTML5. Fixes #42.
* Add Map function (#40)Markus Wüstenberg2020-11-02-0/+17
| | | `Map` makes it easier to build lists of elements without having to iterate.
* Render to Writer instead of string (#39)Markus Wüstenberg2020-11-02-55/+88
| | | | | | | The Render function has been changed to take a `Writer` instead of returning a string. This makes it possible to generate documents without having the whole content in memory. This also removes the `gomponents.Write` function, which is now redundant. Furthermore, the `el.Document` function has been changed to only take one child, as multiple children never make sense for it. (It's not even a child, more a sibling.)
* Remove fmt.Sprintf call in attribute Render (#38)Markus Wüstenberg2020-10-29-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Just concatenating the strings is much faster. Before: ``` make benchmark go test -bench=. goos: darwin goarch: amd64 pkg: github.com/maragudk/gomponents BenchmarkAttr/boolean_attributes-8 8194791 139 ns/op BenchmarkAttr/name-value_attributes-8 5143292 229 ns/op PASS ok github.com/maragudk/gomponents 2.841s ``` After: ``` make benchmark go test -bench=. goos: darwin goarch: amd64 pkg: github.com/maragudk/gomponents BenchmarkAttr/boolean_attributes-8 16755404 67.0 ns/op BenchmarkAttr/name-value_attributes-8 10208625 116 ns/op PASS ok github.com/maragudk/gomponents 2.702s ```
* Pass attributes as pointers (#37)Markus Wüstenberg2020-10-29-5/+5
|
* Don't render or error on nil children (#32)Markus Wüstenberg2020-10-23-0/+3
| | | This makes it easier to e.g. conditionally skip a component for rendering.
* Add Group function to group Nodes (#29)Markus Wüstenberg2020-10-22-11/+36
|
* Update package doc for gomponents (#28)Markus Wüstenberg2020-10-21-1/+1
|
* Use HTMLEscapeString in Textf (#27)Markus Wüstenberg2020-10-21-1/+1
|
* Use HTMLEscapeString (#24)Markus Wüstenberg2020-09-25-1/+1
| | | HTMLEscaper is not needed with strings.
* Add space before auto-closing element (#20)Markus Wüstenberg2020-09-24-2/+2
|
* Introduce Placer interface (#18)Markus Wüstenberg2020-09-24-8/+36
| | | | | When implemented, the `Place` method of the `Placer` interface tells `Render` in `El` where to put a Node. This is relevant for helpers that want to be rendered like attributes, inside the parent element. Fixes the bug where `attr.Classes` was rendered outside the element.
* Add Textf function (#17)Markus Wüstenberg2020-09-23-0/+7
| | | Like Text, but calls fmt.Sprintf before rendering.
* Add a Write function (#11)Markus Wüstenberg2020-09-21-0/+7
|
* Don't render twice with children (#9)Markus Wüstenberg2020-09-21-4/+3
|
* Make NodeFunc and attr implement fmt.Stringer (#6)Markus Wüstenberg2020-09-18-0/+10
|
* Add package documentation (#1)Markus Wüstenberg2020-09-14-0/+7
|
* Add first implementation of Node, El, Attr, TextMarkus Wüstenberg2020-09-13-0/+100