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

How about partial support with _ prefix? #32

Open
carryall opened this issue Jul 19, 2022 · 0 comments
Open

How about partial support with _ prefix? #32

carryall opened this issue Jul 19, 2022 · 0 comments

Comments

@carryall
Copy link

Hi, I'm working on a small Go project and would like to organize my HTML files properly. For example, if there's a dashboard screen with a form and a table, I'll put the form and table in separate partial files within the same folder, prefixed with _.

views/
├─ dashboard/
├── index.html
├── _table.html
├── _form.html

And here's the way configured it

config := goview.Config{
    ...
    Partials:  getPartialList(),
    ...
}

goview.New(config)

// .....

func getPartialList() []string {
	partials := []string{}

	err := filepath.Walk(ROOT_VIEW_PATH, func(path string, info fs.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if !info.IsDir() {
			fileName := info.Name()
			fileExtension := filepath.Ext(fileName)

			if fileExtension == ".html" && strings.HasPrefix(fileName, "_") {
				filePath := strings.Split(path, ROOT_VIEW_PATH)[1]
				filePathWithoutExt := filePath[:len(filePath)-len(fileExtension)]
				partials = append(partials, filePathWithoutExt)
			}
		}

		return nil
	})

	if err != nil {
		log.Info("Fail to get partial files", err.Error())
	}

	return partials
}

I saw one of the open issue also mention a need for a way to configure a partial folder and I think maybe registering any HTML file prefixed with _ within the view folder might be a good idea.
So I just want to propose this idea so you don't need to manually add your folder list or write a function to get all of your partial paths to be able to use them as partials.

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