简体   繁体   中英

Puppet 3 loop execution only if variable is defined within a template

I am trying to create a loop only if index is defined. But it looks like erb can't handle a loop within a if clause.

<% if(@index) %>
index <% index_files.each do |i| %> <%= i %> <% end %>;
<% end %>

Expected Result was: index index.html index.php or ""

Syntax error i got:

My flat approach failed as expected:

<% if(@index_files) %> try_files <% end %>  <% index_files.each do |i| %> <%= i %> <% end %>

I defined index_files as undef => broke the each loop I defined an empty array => since an empty array is defined it didn't work.

Maybe I can check the length of index_files ? Or do I need a complete different way to solve the problem?

I'm doing the same and it works for me, also for nginx ;).

For example: <% if @proxy_ignore_headers %> proxy_ignore_headers<% proxy_ignore_headers.each do |i| -%> <%= i %><% end -%>; <% if @proxy_ignore_headers %> proxy_ignore_headers<% proxy_ignore_headers.each do |i| -%> <%= i %><% end -%>;

That works like a charm, the only difference with you is using () for the if condition, but I bet puppet supports (). It's weird, maybe you had pressed a bad combination generating a character that can't be seen but it's messing with your code, try writing all from scratch just in case.

You can see the full template here

Good luck

At first glance you just need to change

index_files.each

to

@index_files.each

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM