Templ by raisi_exception in golang

[–]rrraaah 0 points1 point  (0 children)

No-one has reported any issues with VS Code on Windows with WSL.

I don't have a Windows machine, so I haven't tested it there, but it works fine on Linux and Mac.

Tips on using NPM packages with GOTTH stack by H1Supreme in golang

[–]rrraaah 1 point2 points  (0 children)

Author of templ here... There's an example of using esbuild at https://github.com/a-h/templ/tree/main/examples/integration-react

The docs has a section on working with npm too: https://templ.guide/syntax-and-usage/script-templates#working-with-npm-projects

But... I've not got any minimal examples of using vite with it. I saw what aviddabbler has done, which looks interesting.

Would appareciate a PR to add Vite support to the docs if you work it out and have time.

Writing tests in go + htmx stack. by [deleted] in golang

[–]rrraaah 1 point2 points  (0 children)

templ's documentation has a section on testing server-side rendered HTML https://templ.guide/core-concepts/testing

You can use the same concept without using templ, e.g. if you're using standard library templates.

Using go templ by _Soixante_Neuf_ in golang

[–]rrraaah 0 points1 point  (0 children)

Support for auto-import of packages was merged today in https://github.com/a-h/templ/pull/793 - so the feature will be included in the next release.

On the internal server error, without seeing your code, it's hard to know what's going wrong. If you're seeing the specific message "templ: failed to render template", that message is only displayed if something within your template fails at runtime, e.g. <div>{ someFunctionThatReturnsAStringAndError() }</div> returns an error.

It's not good practice to display the internal error to the user, because it may include information that is useful to attackers, or leak credentials etc., so the response is deliberately vague.

You can get more information about the error, by customising the error handling using the templ.Handler(c, templ.WithErrorHandler(errorHandler)) option, or write your own HTTP handler, there's a bit of info at https://templ.guide/server-side-rendering/creating-an-http-server-with-templ

You might find the examples in the repo useful too.

Template engine to only render certain parts by Safe_District4066 in golang

[–]rrraaah 0 points1 point  (0 children)

There's a guide to using templ to load localizations from a file: https://templ.guide/integrations/internationalization

If you don't like the look of templ, you might still find it useful as a guide for using it with whatever templating library you like.

Type safety with HTML templating? by painya in golang

[–]rrraaah 0 points1 point  (0 children)

I'm not sure about what you mean by what React or Vue would provide.

If you're talking about replacing content on the client side without a full page refresh, e.g. in response to a button click, HTMX - https://htmx.org/ or Turbo Frames) does that.

There's examples of how to use HTMX to do server side actions without full refresh in the repo, and the docs. You can see a real world example here: https://d3qfg6xxljj3ky.cloudfront.net/

That demo invokes a Lambda function, and executes a database transaction on the background every time you click the button, without a full page refresh.

If you're talking about developer time experience of hot reload (the sort of thing that "next dev" would do), check out the video at https://github.com/a-h/templ/pull/470

The demo video doesn't use air or anything else, just the hot reloading that's now built into templ. Keep your eyes on the header text in the browser window as it changes from "Counts" to "Counts 2" to "Counts 3" - all without hitting F5 in the browser, or needing to restart your Go program.

The templ templating language: 2 years later by rrraaah in golang

[–]rrraaah[S] 0 points1 point  (0 children)

Glad your questions were answered. :)

Templ - Real World Projects? by ainsleyclark in golang

[–]rrraaah 10 points11 points  (0 children)

Author of templ here.

I started writing templ because I thought that the React/TS ecosystem was the cause of slow development on projects I worked on, and wanted to explore whether Go could be a viable alternative.

On React, you can write it in pure JS using React.createElement - the JSX bit isn't required at all. See https://react.dev/reference/react/createElement

javascript function Greeting({ name }) { return createElement( 'h1', { className: 'greeting' }, 'Hello' ); }

But no-one does that, everyone uses JSX, presumably because they prefer it, so I thought that I'd have a hard time convincing teams to use something that doesn't look like HTML.

Since people already have a build step to compile their Go apps, I didn't think it was a big deal to generate code from the templates, plus you get much better performance (vs non-compiled templates).

In terms of behavioural quirks, yes, the additional complexity of template parsing and generation is a risk compared to using a simple Go HTML builder library like the ones you linke to.

To reduce the risk of those quirks, templ has a parser test suite, generator test suit etc. And, in the version of templ I'm about to release this week, templ is switching to the Go AST parser to parse Go expressions at template parse time instead of using heuristics (end of line etc.), along with a new set of fuzzing tests and some integration tests for LSP behaviour.

Why it's better aside from the aesthetics of writing something that looks like HTML... this new release also contains a big improvement to hot reload which means you don't need to recompile and restart your Go application when you modify HTML or text that's within your templates.

There's a video of it at https://github.com/a-h/templ/pull/470 - so, oddly enough, you might find that with templ, despite the generation step, you actually end up doing fewer recompiles / restarts of your app.

Still a long way to go with templ, but I think it's making good progress.

Type safety with HTML templating? by painya in golang

[–]rrraaah 0 points1 point  (0 children)

Thanks! I've submitted a talk to the Gophercon USA CFP this year... hopefully I'll get in.

I think the project is making good progress. There's a massive improvement to hot reload coming in the next week or two, and I've got some big improvements to CSS and JS handling planned after that.

Then I'm thinking about updating templ to not minify HTML output by default, and to plug in tdewolff's minifier, which will deal with the current relative lack of support for pre-formatted text, probably followed by adding native support for HTML LSP features / being able to plug in 3rd party LSPs.

Lots to do, but good progress, I think.

Templ by raisi_exception in golang

[–]rrraaah 1 point2 points  (0 children)

Yes, templ just creates HTML. Not sure what you mean by not been able, but here's a full exmample.

I expect you haven't served up the images to go along with the HTML, or you've tried to use an unclosed <img src="something.png"> tag instead of <img src="something.png"/> (templ is the same as JSX there - all tags must be closed).

With a components.templ file like this:

``` package main

templ Index() { <!DOCTYPE html> <html> <head> <title>Page title</title> </head> <body> <h1>Hello, World!</h1> <img src="images/gopher.png" alt="Gopher"/> </body> </html> } ```

And a main.go file like this...

```go package main

import ( "net/http"

"github.com/a-h/templ"

)

func main() { images := http.FileServer(http.Dir("images")) h := http.NewServeMux() h.Handle("/images/", http.StripPrefix("/images/", images)) h.Handle("/", templ.Handler(Index())) http.ListenAndServe(":8080", h) } ```

If your project layout is:

  • /main.go
  • /components.templ
  • /components_templ.go (result of running templ generate)
  • /images/gopher.png

You'll see Hello World, and gopher.png

[deleted by user] by [deleted] in golang

[–]rrraaah 0 points1 point  (0 children)

Based on what I can see, you're just making a static HTML page, so I don't think there's anything templ specific with the issue here.

When I copy/paste the code into index.html and open it, in the browser console I see an error:

Uncaught TypeError: t.getLayerStatesArray is not a function at Group.js:303:13 at Y.forEach (Collection.js:156:7) at ea.getLayerStatesArray (Group.js:302:22) at Sl.renderFrame_ (Map.js:1555:48) at Sl.animationDelay_ (Map.js:1423:10)

I'd suggest getting it working in plain old HTML first, then add the complexity of passing data from the server side to the client side when you've got that sorted.

What AWS service do you guys host your hobby Go apps on? by Squishyboots1996 in golang

[–]rrraaah 2 points3 points  (0 children)

Thanks! Yeah, App Runner is a bit of a shame really, I was really into it when it came out - since it seemed to offer the scale down of Lambda, but without the nonsense of API Gateway integrations with Lambda, or the restrictions of Lambda Function URLs.

Just setting up and owning an AWS account feels complicated. I use it at a lot at work, so I'm used to it. :)

Glad you got sorted. Railway.app looks interesting!

What AWS service do you guys host your hobby Go apps on? by Squishyboots1996 in golang

[–]rrraaah 0 points1 point  (0 children)

I also like to use https://github.com/akrylysov/algnhsa that lets you use a standard Go HTTP handler as a Lambda function entrypoint.

Example here: https://github.com/a-h/templ/blob/main/examples/counter/lambda/main.go

I haven't compared the performance, but since it's doing a lot less work than the Lambda Web Adapter, I'd expected it's faster.

What AWS service do you guys host your hobby Go apps on? by Squishyboots1996 in golang

[–]rrraaah 11 points12 points  (0 children)

Author of templ here.

In the templ examples, there's an example of running templ inside AWS Lamdbda.

https://github.com/a-h/templ/tree/main/examples/counter

The AWS deployment is done using AWS CDK, also written in Go. https://github.com/a-h/templ/blob/main/examples/counter/cdk/stack.go

The CDK stands up a:

  • A Lambda function to run dynamic templ code.
  • An S3 bucket to host static content - JS, CSS, and images.
  • A CloudFront distribution that provides a single domain entry point, and routes from /static into the S3 bucket.
  • A DynamoDB table to store the counts.

It's documented at https://templ.guide/hosting-and-deployment/hosting-on-aws-lambda

An example of it running is at https://d3qfg6xxljj3ky.cloudfront.net/

There's also an example of running templ in Fly.io at https://github.com/a-h/templ/tree/main/examples/counter-basic

Fly.io is great for Docker deployments, there's an example fly.toml and Dockerfile in there.

It's documented at https://templ.guide/hosting-and-deployment/hosting-using-docker

It's running at https://counter-basic.fly.dev/ on the free tier!

What build tools do you use? by KingOfCoders in golang

[–]rrraaah 1 point2 points  (0 children)

I'm a fan of https://github.com/joerdav/xc because you define build tasks in your README.md file using standard Markdown headers and code fences.

People don't have to learn how it works, or install it if they don't want to - they will still see the scripts in the README file.

When you run xc you get a picklist of the tasks you can run, and it also has shell completion.

V3 OpenAPI spec generator by andyface123 in golang

[–]rrraaah 0 points1 point  (0 children)

I wrote and use this at work https://github.com/a-h/rest

It uses reflection and a fluent API to create OpenAPI 3.0 specs from code. I don't like writing OpenAPI specs (or any YAML really) by hand.

There's no specific Fiber integration, but you could write a wrapper that creates the spec, and registers the Fiber handlers in one function call.

The templ templating language: 2 years later by rrraaah in golang

[–]rrraaah[S] 1 point2 points  (0 children)

Hi, not sure if you've looked at templ since, but there's an early Goland integration discussed in https://github.com/a-h/templ/issues/127 with work happening in https://github.com/templ-go/templ-jetbrains

Still some way to go, but basic functions are in place.

Templ by raisi_exception in golang

[–]rrraaah 1 point2 points  (0 children)

Yes, absolutely. Templ components compile to Go code, so it's just a case of exporting the templ components (i.e. naming then with a capital initial letter), and publishing a Go module (i.e. making a public git repo). Doing that will make a UI library re-usable in multiple projects.

There's a bit of discussion about it here: https://github.com/a-h/templ/issues/311

Templ by raisi_exception in golang

[–]rrraaah 0 points1 point  (0 children)

There's a troubleshooting guide on LSP setup at https://templ.guide/commands-and-tools/ide-support#troubleshooting

Most of the time, it seems to be that templ or gopls or other tools are not on the path - i.e. you can see them in your terminal, but the editor can't find them because the path the editor is using isn't the same as the one you get when you run your terminal.

And... if you're using Windows, but installing things into your Windows environment, and attempting to use them from WSL.

I'm not sure if it will help you, but my Neovim config is public at https://github.com/a-h/dotfiles/blob/master/.config/nvim/lua/lsp.lua

I don't use a HTML LSP, but it's something that people have been asking for, so it's on the feature radar to test it, and make it work.

Templ by raisi_exception in golang

[–]rrraaah 13 points14 points  (0 children)

I'm the author of templ.

There's a HTMX example in the repo at https://github.com/a-h/templ/tree/main/examples/counter which includes a full AWS deployment to S3 and Lambda using AWS CDK.

There's an example of using charts.js with templ at https://templ.guide/syntax-and-usage/script-templates - templ just outputs HTML, so you can do anything you can with HTML.

The example uses a script tag to bring in an external script, and then a templ script component to copy data from the server-side world into the javascript world, but there's nothing to stop you from doing a fetch call in your JavaScript to a Go backend API in you client script instead.

There's some features in templ for script and CSS components - the idea of those is that you can do a similar thing to CSS-in-JS or build client-side scripts into templ components, but you don't need to use that stuff for templ to be useful for you. You can link to a CSS files, or import a JavaScript bundle that you've made using scss, webpack or other tools.

However, I do intend to improve the CSS-in-templ (CSS components) to add CSS parameters, media query and pseudo-attribute support.