openresty / lemplate

OpenResty/Lua template framework implementing Perl's TT2 templating language
54 stars 9 forks source link

INCLUDE directive with variable generates invalid Lua code #7

Open mikz opened 7 years ago

mikz commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

@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 commented 7 years ago

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