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

parser: update @include in templates, to work with relative paths & prevent recursive calls #21943

Merged
merged 14 commits into from
Aug 8, 2024

Conversation

kylepritchard
Copy link
Contributor

Allows calling a partial template directly especially if the partial was previously included as part of a root template. Reasoning added to discussion #21868

TLDR;

Using @include! will reference the template relative to the base template directory i.e. templates folder. Normal action is to include it relative to the template being called. Allows the partial to be called from any other template within the templates directory and its nested directories.

Allows calling a partial directly
Include the @include! directive
run v fmt
@kylepritchard kylepritchard marked this pull request as ready for review July 27, 2024 10:48
vlib/v/TEMPLATES.md Outdated Show resolved Hide resolved
vlib/v/parser/tmpl.v Outdated Show resolved Hide resolved
vlib/v/parser/tmpl.v Outdated Show resolved Hide resolved
Remove personal traces
Correct path for @include! example
@spytheman
Copy link
Member

Please add a test too. See vlib/v/tests/tmpl_test.v which uses vlib/v/tests/tmpl/base.txt and vlib/v/tests/tmpl/include.txt etc.

@kylepritchard
Copy link
Contributor Author

I was looking into this again and have a few changes which I did not push yet. I got thrown when making additional tests and found that the test templates where in a folder which was named 'tmpl' instead of 'templates'. The logic I originally put down was to search for the 'templates' folder which is only applicable to vweb/veb programs. I was currently looking into using the p.template_paths so that any included template would be relative to it's calling template. That may remove the need to have an extra directive completely. The alternative was to have the root folder named in the @include line so it is easy to find but the syntax could start to get a little verbose @include! [tmpl] 'path/to/template' . It would be good to have it working for both vweb calls and normal $tmpl calls too.

@kylepritchard kylepritchard marked this pull request as draft August 3, 2024 23:41
@kylepritchard kylepritchard changed the title Add @include! directive for templates Templates: update @include to work with relative paths & prevent recursive calls Aug 5, 2024
@kylepritchard kylepritchard marked this pull request as ready for review August 6, 2024 08:38
@kylepritchard
Copy link
Contributor Author

This PR is a rethink on the original PR. When an @include is called, it will now call a recursive function which will import the called template and scan to see if it in turn imports any more templates. Using recursion it is possible to track the current directory which will allow nested templates to call templates in a higher directory. To provide compatibility with the current implementation, it will walk back up the directory path to find the 'templates' folder if the called file is not in the same directory as the template being rendered.

Furthermore there was a bug in the code for circular references. If a template called itself or one of its children called it again then it would result in a circular reference that would crash the program. Now all called templates are entered into a dependency tree which will check if a circular reference exists and will fail with error message should one be encountered.

Finally all called templates are put in a temporary cache which will be used instead of costly IO operations if a template is reused in the code. This should benefit complex web applications were templates may represent web components and could be used many times in the app.

Note: The original idea of @include! has been removed in favour of the fallback to templates folder when finding files.

vlib/v/parser/tmpl_test.v Outdated Show resolved Hide resolved
@spytheman spytheman changed the title Templates: update @include to work with relative paths & prevent recursive calls parser: update @include in templates, to work with relative paths & prevent recursive calls Aug 8, 2024
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work. It makes the @include directive a lot more usable, while it improves the error handling too.

@medvednikov what do you think?

@medvednikov
Copy link
Member

Sounds good to me.

@spytheman spytheman merged commit be1bb60 into vlang:master Aug 8, 2024
73 checks passed
@kylepritchard
Copy link
Contributor Author

Thanks for the consideration and advice. Hopefully I can help out more in time to come

@kylepritchard kylepritchard deleted the enhance-template-engine branch August 9, 2024 23:37
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

Successfully merging this pull request may close these issues.

3 participants