Code Monkey home page Code Monkey logo

Comments (6)

a-h avatar a-h commented on July 23, 2024 1

I'm not sure if you've seen the docs on templ's integration with go templates or the section on template composition.

https://templ.guide/syntax-and-usage/using-with-go-templates

https://templ.guide/syntax-and-usage/template-composition

I'm not sure if those help you get the composition you're looking for, or if the idea you've got is around performance optimisation.

from templ.

a-h avatar a-h commented on July 23, 2024 1

Ah, our messages crossed over there.

It would be possible to check if the passed in component is nil, and show default layout that way.

from templ.

a-h avatar a-h commented on July 23, 2024

I can't really picture the use case. Could you write out the templ code you'd like to write, along with the expected HTML?

from templ.

caleb-sideras avatar caleb-sideras commented on July 23, 2024

This specifically targets the injection of dynamically rendered content into a statically rendered template, almost like partial hydration

Consider the following Templ components:

// Contains UI shared across mutiple pages
templ IndexTempl(){
  <html>
    <body>
      {children...}
    </body>
  </html>
}

// Page level UI
templ PageTempl(data Type){
  // render data
}

// Page level function
func Page(w http.ResponseWriter, r *http.Request) templ.Component {
  data := fetchData()
  return PageTempl(data)
}

While passing Page() into IndexTempl() can be achieved per request like follows:

IndexTempl().Render(templ.WithChildren(r.Context(), Page(w, r)), &buffer)

IndexTempl() has no dynamic data, so theoretically it can be rendered to html on build and have the Page() passed in per request

If NopComponent can be overriden to return {{ template 'page' . }}, I could then use the go html/template library to parse/pass in the Page()

I understand I can replace {children...} with {{ template 'page' . }} but some of my other pages have no dynamic data.

(Sorry if this sounds crazy)

from templ.

caleb-sideras avatar caleb-sideras commented on July 23, 2024

I figured out a solution to this, sorry for the unnecessary issue. I will close this now.

Regardless, I do think it's an interesting idea to have the ability to explicitly define default UI if no children are passed in.

from templ.

caleb-sideras avatar caleb-sideras commented on July 23, 2024

Appreciate the help Adrian! The docs provided on go template integration is very helpful. And yes, my idea was mainly centered about performance optimization.

from templ.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.