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

INCLUDE directive with variable generates invalid Lua code #7

Open
mikz opened this issue May 15, 2017 · 6 comments
Open

INCLUDE directive with variable generates invalid Lua code #7

mikz opened this issue May 15, 2017 · 6 comments

Comments

@mikz
Copy link

mikz commented May 15, 2017

Using INCLUDE with variable generates invalid lua code. Looks like it concatenates how to
get the variable value with the logic of getting the block.

Version 0.11

Template

[% INCLUDE "$foo" %]

Lua

 context.include(context, template_map['cli.tt2/stash_get(stash, 'foo')'] and 'cli.tt2/stash_get(stash, 'foo')' or stash_get(stash, 'foo'))
@agentzh
Copy link
Member

agentzh commented May 15, 2017

@mikz Thanks for the report. It is known that use of variables inside the INCLUDE directive is not yet supported.

This is actually a bit tricky to support unless the lemplate compiler itself is written in OpenResty/Lua. This Perl 5 version of lemplate will probably never do that.

My colleague @ingydotnet is currently working on the fanlang implementation of lemplate atm, which will make the lemplate compiler itself run atop OpenResty/Lua. At that point, this might be possible though running a full lemplate compiler on the edge is definitely not the most efficient choice.

@mikz
Copy link
Author

mikz commented May 15, 2017

Having pure Lua version would be awesome.

My use case is to use these templates to template nginx config. Wanted to use this to compile several included templates to one blob and not depend on a nginx include directive.

Efficiency does not really matter there for me. I'm happy to try any alpha quality software.

@ingydotnet
Copy link
Contributor

I'll add an issue for this to the Fanlang Lemplate.

If understand it correctly, this does not involve runtime compilation. You just want these 2 lines to work the same:

[% INCLUDE some.tt %]
[% tt = 'some.tt'; INCLUDE $tt %]

@mikz I think for your use case you could just use Perl (with the exact same templates) using the real Template Toolkit http://www.template-toolkit.org/

@ingydotnet
Copy link
Contributor

@mikz also take a look at https://metacpan.org/pod/Template::Toolkit::Simple for really simple command line usage like:

tt-render --path=./:template/ --data=values.yaml --post-chomp foo.tt

@ingydotnet
Copy link
Contributor

@mikz I have implemented this in the fanlang version of Lemplate. This should become open source once we have accomplished the prerequisites for doing so. :)

@mikz
Copy link
Author

mikz commented Jun 30, 2017

Sounds great happy to try that when its out.

I've went with using https://github.com/chenxianyu2015/liquid-lua because it is pure Lua and I could easily implement for loop and objects for accessing filesystem from the template.

For my use case - generating nginx configuration - I really want the FS access to for example generate includes or include those files directly.

edit:

Example configuration https://github.com/3scale/cors-proxy/blob/37907f85ce9fa21b1f71451422ee0f3e1f4b2da6/nginx/main.conf.liquid
And the template renderer: https://github.com/3scale/apicast-cli/blob/f54ab559b73b2bd3186c71f7b7a71f1b3a0e8fc9/src/apicast-cli/template.lua

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

3 participants