Ecosyste.ms: Packages

An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 4.5% on proxy.golang.org
Top 1.1% dependent packages on proxy.golang.org
Top 0.5% dependent repos on proxy.golang.org
Top 0.9% docker downloads on proxy.golang.org

proxy.golang.org : github.com/jba/templatecheck

Package templatecheck checks Go templates for problems. It can detect many errors that are normally caught only during execution. Use templatecheck to find template errors early, and along template execution paths that might only rarely be reached. Given a template t from one of the text/template, html/template, or github.com/google/safehtml/template packages, calling NewChecked[T](t) type-checks t and returns a CheckedTemplate[T]. The only things you can do with a CheckedTemplate[T] are get its name, and execute it with data of type T. The execution will be free of type errors. To make that guarantee, the semantics of templates are restricted. CheckHTMLStrict has details, but the general idea is that types must be known so they can be checked. For example, the template will succeed as long as dot contains a field or zero-argument method named "F". The non-strict CheckXXX functions of this package will check that if the type of dot is known, but if it is unknown or an interface type, they will not complain, since template execution might succeed. By contrast, strict checking will fail unless it can be sure that the reference to "F" is valid, implying that it must know the type of dot. When all of a program's templates are CheckedTemplates, it is not necessary to write tests to check templates as described below. All type-checking happens when the CheckedTemplates are created. The rest of this documentation describes the original templatecheck mechanism, accessed through the CheckXXX functions. It may still be useful, especially for existing programs, but CheckedTemplate[T] is preferable because it is safer. A template must be invoked with the same Go type each time to use the templatecheck.CheckXXX functions. Passing that type to templatecheck gives it enough information to verify that all the field references in the template are valid. templatecheck can also verify that functions are called with the right number and types of arguments, that the argument to a range statement can actually be ranged over, and a few other things. Consider a web server that parses a template for its home page: Use templatecheck to catch errors in tests, instead of during serving: To check associated templates, use Template.Lookup. This can be necessary for non-strict checking if full type information isn't available to the main template. (It isn't needed for strict checking, because all calls to associated templates are type-checked.) For example, here the base template is always invoked with a basePage, but the type of its Details field differs depending on the value of IsTop. The template text is Checking only the main template will not provide much information about the two associated templates, because their data types are unknown. All three templates should be checked, like so:

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/jba/templatecheck
License: MIT
Latest release: 9 months ago
First release: over 3 years ago
Namespace: github.com/jba
Dependent packages: 21
Dependent repositories: 177
Stars: 33 on GitHub
Forks: 1 on GitHub
Docker dependents: 56
Docker downloads: 57,243
See more repository details: repos.ecosyste.ms
Last synced: 23 days ago

    Loading...
    Readme
    Loading...