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

Pre-parse all templates #21

Open
Abbe98 opened this issue Sep 26, 2021 · 1 comment
Open

Pre-parse all templates #21

Abbe98 opened this issue Sep 26, 2021 · 1 comment
Labels
large-project-support Things that mostly affects large projects 🏅 enhancement New feature or request

Comments

@Abbe98
Copy link
Collaborator

Abbe98 commented Sep 26, 2021

Currently, templates are parsed each time they are invoked with include or include_text. This affects the build times of large sites.

To avoid parsing the same file multiple times we should parse them during discovery and store the templates(text & HTML), include or include_text would then just look up the needed template and reuse it each time.

@Abbe98 Abbe98 added the 🏅 enhancement New feature or request label Sep 26, 2021
@Abbe98
Copy link
Collaborator Author

Abbe98 commented Sep 27, 2021

Something like the following but somehow without causing circular dependencies:

func DiscoverTemplates() error {
	filepath.Walk("templates", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if !info.IsDir() && !strings.HasPrefix(path, "templates/layouts") {
			textTemplate, err := text_template.New("").Funcs(function.GetTextQueryFuncs()).Funcs(function.GetTextStringFuncs()).Funcs(function.GetTextMathFuncs()).Funcs(function.GetTextUtilsFuncs()).Funcs(function.GetTextIncludeFuncs()).ParseFiles(path)
			htmlTemplate, err := html_template.New("").Funcs(function.GetHTMLQueryFuncs()).Funcs(function.GetHTMLStringFuncs()).Funcs(function.GetHTMLMathFuncs()).Funcs(function.GetHTMLUtilsFuncs()).Funcs(function.GetHTMLIncludeFuncs()).ParseFiles(path)
			if err != nil {
				return err
			}

			templateCollection := TemplateCollection{
				ParsedTextTemplate: textTemplate,
				ParsedHTMLTemplate: htmlTemplate,
			}
			TemplateIndex[path] = templateCollection
		}
		return nil
	})
	return nil
}

@Abbe98 Abbe98 added the large-project-support Things that mostly affects large projects label Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
large-project-support Things that mostly affects large projects 🏅 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant