Code Monkey home page Code Monkey logo

Comments (3)

a-h avatar a-h commented on June 24, 2024

Hi, sorry for the delay in responding.

At the moment, templ script components don't do any bundling or transpilation of the JavaScript that's in them. That means that you can only use browser-supported features in templ script components.

However, you can bundle your own JavaScript or TypeScript and use any features you like, including async/await, importing npm modules.

I put together an example of how to bundle TypeScript in the PR at https://github.com/a-h/templ/pull/738/files - it's the same process for JS, but with different file names.

It also covers your use case of getting Go data into your JavaScript function.

Note how ./ts/src/index.ts is bundled and minified by esbuild (https://esbuild.github.io/getting-started/#install-esbuild) to create ./assets/js/index.js.

I will update the https://templ.guide documentation to mention this approach more clearly.

I'm also proposing two new features to add to templ to make this simpler to achieve: #739

And in #740 I discuss where I think we might want to go with CSS.

from templ.

miniscruff avatar miniscruff commented on June 24, 2024

For use with htmx I feel the advantage of being able to, per response, generate a dynamic async function would be quite useful. Just that, my one particular use case for passwordless is extremely minimal.

For that I do have to question this point you made.

That means that you can only use browser-supported features in templ script components.

https://caniuse.com/async-functions is widely supported ( over 97% ), which I am not sure where you draw the line at supported or not but I understand if it needs to be 100%.

Additionally, the experience of writing short javascript functions is not that bad. I don't feel its any different from writing inside a <script></script> tag in a normal html file.

I think I have a solution using strings, it just means I have to match up the function name by string rather then directly but to me that is a better trade off then adding nodejs and esbuild as a dependency. Maybe if I had hundreds of lines of javascript it would be worth it. But via HTMX I really only need a little bit.

from templ.

joerdav avatar joerdav commented on June 24, 2024

It seems there is a work around for this, and with the deprecation of script templates as recommended features I think we can close this.

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.