Skip to content
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

proposal: ability to render simple text #631

Closed
adpande opened this issue Mar 19, 2024 · 6 comments
Closed

proposal: ability to render simple text #631

adpande opened this issue Mar 19, 2024 · 6 comments
Labels
enhancement New feature or request NeedsDecision Issue needs some more discussion so a decision can be made

Comments

@adpande
Copy link

adpande commented Mar 19, 2024

Hi,

I am trying to render simple text email using templ. But the templ block formats it and removes all blank lines. I tried using script block as well but it emits <script> tags when rendered.

It would be nice if we add a text block like:

text SimpleEmail(to string) {
Hello {to},

Welcome to our system.

Thanks,
Some Team
}

The text block will not get formatted and simply emits text after it is rendered.

Thanks,
Abhi

@joerdav joerdav added enhancement New feature or request NeedsDecision Issue needs some more discussion so a decision can be made labels Mar 20, 2024
@joerdav
Copy link
Collaborator

joerdav commented Mar 20, 2024

Thanks for the proposal, I wonder what value add this has over something like:

func SimpleEmail(to string) {
    return fmt.Sprintf(`
Hello %s,

Welcome to our system.

Thanks,
Some Team
`, to)
}

Would you suggest for control flow statements such as for, switch, if to be valid? As this may be hard to parse in this context.

@joerdav joerdav changed the title Ability to render simple text proposal: ability to render simple text Mar 20, 2024
@adpande
Copy link
Author

adpande commented Mar 20, 2024

Yes, conditions , loops and expressions would be powerful. We can extent this latter even to support markdown.

@joerdav
Copy link
Collaborator

joerdav commented Mar 20, 2024

It may be powerful, but we need to think about how it would be even possible.

The way templ parses Go code is as follows...

It looks for a keyword at the start of a node such as for.
Then it parses the go expression up to the opening curly brace (this can be multiple lines of go code) {.
And then we continue parsing nodes until the closing curly brace }.

If it finds a keyword but then a subsequent step of parsing the statement fails, we don't assume anything, and throw an error.
By "we don't assume anything", what I mean is that if the stuff after the word for isn't valid go code, we don't fallback to it being regular text, (to avoid leaking code accidentally).

This is why in cases such as <div>for example</div>, you will see an error.
We basically say "hey, we're a bit confused here, you might have been trying to write a for loop, can you help?".
Then the dev can either fix the for loop syntax or show that they were trying to write normal text by doing <div>{for example}</div>

In the context of pure text, I'm struggling to see how we would parse it without encountering lots of errors like this.

@adpande
Copy link
Author

adpande commented Mar 20, 2024

Yes, I understand. I will also think. Maybe also discuss it with other collaborators.

Thanks for considering it.

@silverwind
Copy link

This seems far to error-prone for the parser and JSX also does not allow it, for comparison. What you can do in JSX is either quoted the string "foo" or unquoted inside fragements, e.g. <Fragment>foo</Fragment> or shorthand <>foo</>, both which should be much easier to parse.

@a-h
Copy link
Owner

a-h commented Apr 4, 2024

The mission of templ is to:

Enable Go developers to build strongly typed, component-based HTML user interfaces with first-class developer tooling, and a short learning curve.

So, rendering text isn't part of the plan, but I've suggested how you can use templ to do something similar at #385 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request NeedsDecision Issue needs some more discussion so a decision can be made
Projects
None yet
Development

No branches or pull requests

4 participants