-
-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feedback, Inputs & Proposals #35
Comments
Thanks so much for the feedback, there's some great ideas in here. Please let me take a bit of time to think about some of these before I get back to you. However, I can give some immediate thoughts right away...
Totally agree with you on this - I've forgotten to do this myself on more than one occasion. It would be easy to get
Good idea on comments, I know it's something that's lacking from my own use. I hadn't settled on an idea for how it would look yet. The {% comment %} idea looks good, but I do like the idea of using something from Go syntax or allowing HTML comments. I'm looking for something that people would guess at, and just be right the first time. I'm just not sure what that is without doing a bit of research first (i.e. asking people to guess at how you do a comment, and looking at what the most popular templating languages do). I'll have a chat with some users and canvas some more feedback here.
I've been using templ a lot with a team recently to build email templates, and found the start / end syntax a bit out-of-the-way too, on an English keyboard. I chose I also thought that since The built-in html/template in Go uses At the moment, once the templ start of expression token This means that Go code inside templ expressions isn't parsed at all... but it could be interesting to try parsing the code instead. I used I'll need to think about this some more. If we were to support an alternative, I'd want to get Any ideas you've got here would be great.
Yeah, that's just annoying isn't it. What was I thinking!? :)
I'm not sure what you mean by a virtual call. Do you mean that the
Yes, that would be easy enough. |
I can see the benefit of the pointer receiver templates, and could see another Go developer assuming this was possible while writing templ code. func (x ViewModel) View() templ.Component { return myTemplate(x) } |
btw: that is exactly how I use it now. |
I've just added a feature for implicit package selection, as per your suggestion, see #36 |
@aight8 - implicit package selection and flexible spacing are now available in version https://github.com/a-h/templ/releases/tag/v0.0.162 |
And I've added a new flag to |
Is it possible to implement "template as struct method" easily? (I referr to "1. Pointer Receiver Templates") |
What I hover achived is to allow the template function names contains underscore. I use it to group templace functions. Maybe templ should allow any valid go function names as template name? |
I think it should be possible to allow the generated function to generate a receiver. My understanding is that you'd like it so that you've got: data.gopackage data
type Data struct {
A string
B string
} data.templ{% templ (d Data) Template() %}
<div>
{%= d.A %}
</div>
<div>
{%= d.B %}
</div>
{% endtempl %} And you'd expect it to produce Go code like this? data_templ.gopackage data
func (d Data) Template() templ.Component {
//TODO: Return a component.
} On how much processing the @joerdav made an improvement to the output speed of Go code by introducing a write buffer instead of writing directly to files. This resulted in a large reduction in the number of OS syscalls being made, see 2a47eaf This approach could probably be reused to place a limit on how much RAM is used, and to limit Even without this improvement, I think templ is still 6 x faster than React (I haven't had chance to verify that my benchmarks make sense yet) based on the benchmark here: https://github.com/a-h/templ/tree/benchmark/benchmarks |
I had a play with this. If the input writer happens to be a If the input is not a StringWriter (e.g. it's a func Candidate(p Person) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, ww io.Writer) (err error) {
w, ok := ww.(io.StringWriter)
if !ok {
w3 := bufio.NewWriter(ww)
w = w3
defer w3.Flush()
}
ctx, _ = templ.RenderedCSSClassesFromContext(ctx)
ctx, _ = templ.RenderedScriptsFromContext(ctx)
_, err = w.WriteString("<div>")
if err != nil { I'd need to test this a bit more, especially with a real HTTP server for comparison, before modifying the code generation to use this. I want to stick with the standard library as much as possible. |
@a-h Yes exactly. I try to extend the parser to allow using |
* feat: start reworking the parser to support bare Go code * wip: continuing parser work * wip: add the if expression back in to support the template parser * wip: fixed broken template file test * wip: refactor from functions to static variables * wip: added support for if statements * wip: add comments to output to show what was parsed * wip: reimplement switch statements * wip: add the if unit tests back in * wip: update README * wip: add for loop support back in * feat: don't render scripts if they are not required * refactor: fix the formatting tests * feat: add support for templates to be receivers, see #46 and #35 (item 1) * refactor: have v1 and v2 side-by-side to enable easier migration * refactor: continue v1 and v2 side-by-side * feat: throw an error if a legacy format is encountered * wip: added start of updated migration function * fix: manually copy between v1 and v2 instead of using copier * feat: complete migration * docs: update for version 2 * fix: broken package test. * fix: broken reformatting of case statements. * feat: continue case statement work * fix: remove additional whitespace * fix: break on newline to allow inline if * fix: bug in Windows line end parsing affecting CSS * feat: add a -w parameter to choose how many cores to use during templ generation * fix: ensure that the server always closes * fix: ensure that code can be at the end of files too * refactor: simplify parsing behaviour to be line oriented * feat: normalise whitespace * feat: use the number of CPUs available to the machine * feat: handle trailing whitespace after case statements * feat: improve the formatting behaviour * feat: set tables to format block style * feat: add style and script elements that ignore the contents * feat: add textDocument/codeLens support * refactor: remove TODO * refactor: copy code from github.com/sourcegraph/go-lsp * refactor: migrate to go.lsp.dev/protocol * feat: migrate the LSP server and rewrite functionality to go.lsp.dev * feat: add sourcemap visualisation * feat: improve sourcemap rendering * feat: fix the source location mapping * refactor: use zero-indexed line indices to match the LSP specification * security: CVE-2022-28948 * chore: upgrade goquery to 1.8.0 * chore: move example to _example to get Go tools to ignore from the top level * feat: rewrite the hover output positions * fix: handle that hover can return nil * feat(lsp): add Implementation * feat: improve LSP capabilities * feat: add declaration and definition support * feat: add DocumentLink, DocumentColor * feat: update LSP edits, replace SourceGraph code to deprecate sourceLength * refactor: use an array of lines to store content, instead of storing the string, to make updates less expensive * fix: update snippets for new syntax * docs: add updated GIF * fix: DidChange storage of updates
Hello, since the {% %} operator was removed, whats the way to insert go code into templ templates? |
For example, here's a The example is at https://github.com/a-h/templ/blob/main/example/posts.templ
Where the line doesn't start with a
There are also examples that use the features in https://github.com/a-h/templ/tree/main/generator There's currently no way to define a variable within
If you want to define
Hope that helps! |
New idea: 13. Renderable ComponentGenerate Render for Named type when no func name and param list passed: Example:
should generate:
|
New idea: 14. (WIP!) Omit empty string valued attributeGenerate Render for Named type when no func name and param list passed: Example:
should generate:
|
New idea: 15. Use "//line" go prefix in generated file so that go directly points to the bad code in the template |
@aight8 I think some of these might be easier to track in separate issues. |
There's been a lot of changes in the last year. Idea 14 was dealt with by conditional attribute expressions, and I think 15 is dealt with by improvements to the LSP handling, in particular, the new features that help with debugging (there's now a HTTP server that provides a window into the LSP internals). I'm not thinking to think too much about 13 right now. My main focus over the next few months is going to be adding examples, more docs (see the new docs website at https://templ.guide), and HTMX. I think it's time to close this issue. Happy for you to raise new issues though. :) |
I really like templ. I hope it have a great future.
I have made some notes and thoughts while using it which I want to share now - I write it compact, but let's discuss about some points if you want.
1. Pointer Receiver Templates
I commonly use structs as
ViewModels
. Instead wire up all of those with it's render function manually this could help a lot:I used this pattern very much in quicktemplate to make all kind of structures "renderable". Currently in templ I have to create a struct method manually and invoke the render function, for every piece of template.
2. Autocompletion
In this scenario a Component is expected. All functions in the current package, which return a Component are possible candidates.
3. Implicit Package selection
This line of code is most time redundant because the package name can be derrived by the directory name.
When using templ on daily basis I forget this line sometimes.
Also, it is not adjusted by the IDE if the name of the package name changes (i.e. the directory name).
4. Comments
Comment out quickly some code like
{% comment %}
in quicktemplate.What about HTML comment? Or
{{/* a comment */}}
5. Alternative START/END-Tag
This point is mabye a matter of taste. On swiss german keyboard, and maybe a lot others, this
{%
and%}
is arduous to write. And you do it a lot when writing templates.Maybe the
{{
and}}
which are used in regular go templates is not bad at all.6. Required Space after START-Tag and before END-Tag
The space after the
{%
or before the%}
took some getting used to. Strict on the one hand, prone to error on the other.Perhaps the parser could point out this error more clearly - or even autofix it (like go fmt for templ).
7. The Context
A really happy that a context I passed arround the Component Render methods.
This way I can access global data or a localizer helper.
But this is undocumented feature and somehow not so nice.
I have no concrete implementation-idea currently. I make some thoughts...
But in the abstract a shortcut to access a global "struct" variable would do the job.
An easier implementation would be:
A pointer to a struct could be passed via context value, while templ make it visible in a variable
x
.In both cases the qualified-id must be specified somewhere (for generation and LSP)
Don't get me wrong, I'm not a make it all global type of guy. But it reduces all this argument passtroughts of really important things (i18n, basic user info etc.).
It's just a third source of (typed) data beside receiver variable + argument variables.
8. The Writer
If I'm not mistaken, virtual calls are 5 times slower than direct method calls. And a template calls it thousands of times including the error check.
A benchmark might help.
Maybe it would be interesting to have a look at the implementation of Quicktemplate, where among other things an acquirable byte buffer comes into play.
Another idea would be to be able to define a writer type via CLI argument.
If I manually implement templ components in go, the
io.Writer
interface is a bit tedious to work with directly.9. Switch case improvement
Instead:
What about:
10. Range improvement
Instead:
What about:
This construct is rarely found in template languages. To be honest, I have no idea why.
11. Else-If support
Mabye the switch is a better alternative anyway when it have at least the same capacibilities.
12. Compile a specific file only
Currently recursively all
templ
files are compiled.I configured my IDE or modd to autocompile templ files on every change.
The text was updated successfully, but these errors were encountered: