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

Implicit package selection #36

Closed
2 tasks done
a-h opened this issue Mar 13, 2022 · 2 comments
Closed
2 tasks done

Implicit package selection #36

a-h opened this issue Mar 13, 2022 · 2 comments

Comments

@a-h
Copy link
Owner

a-h commented Mar 13, 2022

As noted in #35, templ currently requires a `{% package "packageName" %} at the top of each file.

This is the same behaviour as Go, but to make it faster to write templates, the following approach could be used:

  • Ensure that the parser supports missing package instructions, and inserts them into the object model.
  • Ensure that templ fmt inserts missing package instructions.

The automatic package algorithm:

  • Uses the contents of the package instruction in the *.templ file if there is one.
  • Uses the name of the directory as the package name if it is a valid Go identifier (see https://go.dev/ref/spec#identifier)
  • Uses main as the package if it's not, or a directory context is not available (e.g. parsing a string).

This is simpler than attempting to read any Go files that already exist in the directory.

@a-h
Copy link
Owner Author

a-h commented Mar 13, 2022

To test the fmt command, I used a sample file:

{% templ test() %}
Hello
{% endtempl %}

templ fmt rewrote it to include:

{% package testimplicitpackage %}

@a-h
Copy link
Owner Author

a-h commented Mar 13, 2022

Closed in 4234940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant