composite-new-component-with-go.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package digging_deeper
  2. import (
  3. "github.com/qor5/docs/docsrc/examples/e00_basics"
  4. "github.com/qor5/docs/docsrc/generated"
  5. "github.com/qor5/docs/docsrc/utils"
  6. . "github.com/theplant/docgo"
  7. "github.com/theplant/docgo/ch"
  8. )
  9. var CompositeNewComponentWithGo = Doc(
  10. Markdown(`
  11. Any Go function that returns an ~htmlgo.HTMLComponent~ is a component,
  12. Any Go struct that implements ~MarshalHTML(ctx context.Context) ([]byte, error)~ function is an component.
  13. They can be composite into a new component very easy.
  14. This example is ported from [Bootstrap4 Navbar](https://getbootstrap.com/docs/4.3/components/navbar/):
  15. `),
  16. ch.Code(generated.CompositeComponentSample1).Language("go"),
  17. utils.Demo("Composite New Component With Go", e00_basics.CompositeComponentSample1PagePath, "e00_basics/composite-components.go"),
  18. Markdown(`
  19. You can see from the example, We have created ~Navbar~ and ~Carousel~ components by
  20. simply create Go func that returns ~htmlgo.HTMLComponent~.
  21. It is easy to pass in components as parameter, and wrap components.
  22. By utilizing the power of Go language, Any component can be abstracted and reused with enough parameters.
  23. The ~Navbar~ is a responsive navigation header, Resizing your window, the nav bar will react to device window size and change to nav bar popup and hide search form.
  24. For this ~Navbar~ component to work, I have to import Bootstrap assets in this new layout function:
  25. `),
  26. ch.Code(generated.DemoBootstrapLayoutSample).Language("go"),
  27. Markdown(`
  28. You can utilize the command line tool [html2go](https://github.com/sunfmin/html2go) to convert existing html code to htmlgo code.
  29. By writing html in Go you get:
  30. - The static type checking
  31. - Abstract out easily to different functions
  32. - Easier refactor with IDE like GoLand
  33. - Loop and variable replacing is just like in Go
  34. - Invoke helper functions is just like in Go
  35. - Almost as readable as normal HTML
  36. - Not possible to have html tag not closed, Or not matched.
  37. Once you have these, Why generate html in any interpreted template language!
  38. `),
  39. ).Title("Composite new Component With Go").
  40. Slug("components-guide/composite-new-component-with-go")