lemplate icon indicating copy to clipboard operation
lemplate copied to clipboard

INCLUDE directive with variable generates invalid Lua code

Open mikz opened this issue 8 years ago • 6 comments

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'))

mikz avatar May 15 '17 14:05 mikz

@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.

agentzh avatar May 15 '17 17:05 agentzh

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.

mikz avatar May 15 '17 20:05 mikz

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 avatar May 18 '17 17:05 ingydotnet

@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 avatar May 18 '17 17:05 ingydotnet

@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. :)

ingydotnet avatar May 25 '17 19:05 ingydotnet

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

mikz avatar Jun 30 '17 11:06 mikz