Skip to content

Commit

Permalink
Merge pull request #147 from chasefleming/docs/json-strings
Browse files Browse the repository at this point in the history
Update docs json strings and special characters
  • Loading branch information
chasefleming authored Sep 1, 2024
2 parents 114679a + 0f4e3f3 commit 2df274d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ content := elem.Div(nil,

In this example, the Fragment function is used to insert the nodes into the parent div without introducing any additional wrapper elements. This keeps the HTML output clean and simple.

### Handling JSON Strings and Special Characters in Attributes

When using attributes that require JSON strings or special characters (like quotes), make sure to wrap these strings in single quotes. This prevents the library from adding extra quotes around your value. For example:

```go
content := elem.Div(attrs.Props{
attrs.ID: "my-div",
attrs.Class: "special 'class'",
attrs.Data: `'{"key": "value"}'`,
}, elem.Text("Content"))
```

## Advanced CSS Styling with `StyleManager`

For projects requiring advanced CSS styling capabilities, including support for animations, pseudo-classes, and responsive design via media queries, the `stylemanager` subpackage offers a powerful solution. Integrated seamlessly with `elem-go`, it allows developers to programmatically create and manage complex CSS styles within the type-safe environment of Go.
Expand Down
13 changes: 12 additions & 1 deletion htmx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,15 @@ contentDiv := elem.Div(attrs.Props{attrs.ID: "result-div"}, elem.Text("Initial c
pageContent := elem.Div(nil, button, contentDiv)
```

When the button is clicked, `htmx` will fetch content from the `/fetch-content` endpoint and replace the content inside the `#result-div`.
When the button is clicked, `htmx` will fetch content from the `/fetch-content` endpoint and replace the content inside the `#result-div`.

## Handling JSON Strings in Attributes

When using attributes like `hx-vals` that require JSON strings, ensure the JSON is wrapped in single quotes. This is necessary for correct rendering by `htmx`. For example:

```go
content := elem.Div(attrs.Props{
htmx.HXGet: "/example",
htmx.HXVals: `'{"myVal": "My Value"}'`,
}, elem.Text("Get Some HTML, Including A Value in the Request"))
```

0 comments on commit 2df274d

Please sign in to comment.