layout-function-and-page-injector.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package advanced_functions
  2. import (
  3. "github.com/qor5/docs/docsrc/generated"
  4. "github.com/qor5/docs/docsrc/utils"
  5. . "github.com/theplant/docgo"
  6. "github.com/theplant/docgo/ch"
  7. . "github.com/theplant/htmlgo"
  8. )
  9. var LayoutFunctionAndPageInjector = Doc(
  10. Markdown("Read this code first, Guess what it does."),
  11. ch.Code(generated.DemoLayoutSample).Language("go"),
  12. Markdown(`
  13. ~ctx.Injector~ is for inject html into default layout's html head, and bottom of body.
  14. html head normally for page title, keywords etc all kinds meta data, and css styles,
  15. javascript libraries etc. You can see we put vue.js into head, but put main.js into the bottom of body.
  16. Next part describe about these asset references:
  17. `),
  18. ch.Code(generated.ComponentsPackSample).Language("go"),
  19. Markdown(`
  20. ~web.JSComponentsPack~ is the production version of QOR5 core javascript code.
  21. Created by using [@vue/cli](https://cli.vuejs.org/guide/creating-a-project.html),
  22. It does the basic functions like render server side returned html as vue templates.
  23. Provide basic event functions that call to server, and manage push state
  24. (change browser address urls before or after do ajax requests). do page partial refresh etc.
  25. the javascript or css code are packed by using [embed](https://pkg.go.dev/embed).
  26. `),
  27. ch.Code(generated.PackrSample).Language("go"),
  28. Markdown(`
  29. And with ~web.PacksHandler~, You can merge multiple javascript or css assets together into one url.
  30. So that browser only need to request them one time. and cache them. The cache is set to the start
  31. time of the process. So next time the app restarts, it invalid the cache.
  32. `),
  33. utils.Anchor(H2(""), "Summary"),
  34. Markdown(`
  35. For a new project:
  36. - Use [@vue/cli](https://cli.vuejs.org/guide/creating-a-project.html) to create an asset project that manage your javascript and css. and compile them for production use
  37. - Use [embed](https://pkg.go.dev/embed) to pack them into Go code as ~ComponentPack~, which is a string
  38. - Use ~PacksHandler~ to mount them as available http urls
  39. - Write Layout function to reference them inside head, or bottom of body
  40. `),
  41. ).Title("Layout Function and Page Injector").
  42. Slug("basics/layout-function-and-page-injector")