Ruby template indentation issue - puppet - ruby

I'm unable to fix indentation for ruby template, I've spent hours on this and still couldn't get it to work as expected.
Here is the template:
<%= #log_path -%> {
<%= #rotate_every %>
rotate <%= #rotate_count %>
compress
<% if #delaycompress == true %>delaycompress
<% end -%>missingok
notifempty
create <%= #create_mode -%> <%= #create_owner -%> <%= #create_group %>
<% if #postrotate == true -%>postrotate
<% #postrotate_cmd.each do |value| -%><%= value %>
<% end -%>endscript<% end %>
}
When #postrotate is false the output looks like this. The } is after a empty line.
/var/log/auth.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0644 root adm
}
When #postrotate is true the output looks like this. The second invoke commnad should be a bit to the right.
/var/log/auth.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0644 root adm
postrotate
invoke-rc.d rsyslog reload > /dev/null
invoke-rc.d rsyslog reload > /dev/null
endscript
}
Expected output when #postrotate is false:
/var/log/auth.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0644 root adm
}
Expected output when #postrotate is true:
/var/log/auth.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0644 root adm
postrotate
invoke-rc.d rsyslog reload > /dev/null
invoke-rc.d rsyslog reload > /dev/null
endscript
}

You seem to have incorrect expectations about the meaning of the -%> end-of-tag token. If you close a tag with -%> and it is the last non-whitespace on that line of the template, then any trailing whitespace and the newline following it are consumed instead of being copied to the output. But that does nothing for you where the tag closed with -%> is not the last non-whitespace on the line, which is often the case in your template.
You also seem not to know about the <%- start-of-tag token, which has a similar effect on any leading indentation of a line where a tag starting with that token is the first non-whitespace. There are several places where using that could help you make your template easier to read.
Overall, I might write your template more like this:
<%= #log_path %> {
<%= #rotate_every %>
rotate <%= #rotate_count %>
compress
<%- if #delaycompress -%>
delaycompress
<%- end -%>
missingok
notifempty
create <%= #create_mode %> <%= #create_owner %> <%= #create_group %>
<%- if #postrotate -%>
postrotate
<%- #postrotate_cmd.each do |value| -%>
<%= value %>
<%- end -%>
endscript
<%- end -%>
}
In particular, note that lines that contain nothing but whitespace and a non-printing tag that starts with <%- and ends with -%> produce no output at all. Using that to separate flow-control statements from template text and output tags makes the template clearer and easier to read, and it will also ensure that your loop body is consistently indented. Furthermore, it may help you see and very likely will help you debug issues with leaving off the - where you wanted it, as your original template does in its last <% end %> tag.

Related

Chef cookbook template REGEX to match

I have a template file
#fileName: test.properties.erb
<% if #env == 'STAGING' && node['hostname'] =~ /^staging/ %>
somekey=node['hostname']
<% else %>
somekey=defaultNode
<% end %>
My Recipe is as follows
template ("test.properties") do
source 'test.properties.erb'
variables(
env: "STAGING"
)
end
I wanted to perform && operation along with REGEX check
when i use the below in the erb file it works with &&
<% if #env = 'STAGING' && node['hostname'].include?('staging-01') %>
...
I wanted to apply a regex and check if the hostname starts with string staging
say I have staging-01, staging-02 and apply on both
The answer seems to be simple Ruby string matching expression
So i made the change to expression node['hostname'].match(/staging*/) and this evaluated the cookbook successfully.
#fileName: test.properties.erb
<% if #env == 'STAGING' && node['hostname'].match(/staging*/) %>
somekey=node['hostname']
<% else %>
somekey=defaultNode
<% end %>

How to use a ERB file in Ruby to write some text in a text file

I have a play.erb file:
<p>Welcome!<br><p>
<% if $wgplaying==true %>
Template: <%= $template %>
<% end %>
<br><br>
<% if $wginword==true && $wgplaying==true && $a!='' %>
Character <%= $a %> is in word.
<% end %>
<% if $wginword==false && $wgplaying==true && $a!='' %>
Character <%= $a %> is not in word.
<% end %>
<br><br>
I want to write
"Character <%= $a %> is in word."
or
"Character <%= $a %> is not in word."
in a text file, but I do not know what command should I use. Or, should I do this in a Ruby file?
ERB is in Ruby's Standard Library, so try something like this:
require 'erb'
TEMPLATE_FILE = 'template.erb'
$wgplaying = true
$template = "This is my template"
$wginword = true
$wgplaying = true
$a = "something"
render = ERB.new(File.read(TEMPLATE_FILE),trim_mode: ">")
puts render.result
The new method has many options.
These are the trim_mode options:
% enables Ruby code processing for lines beginning with %
<> omit newline for lines starting with <% and ending in %>
> omit newline for lines ending in %>
- omit blank lines ending in -%>

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 -%>;
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

Rails- Index in form_for field names

I have a form that I am trying to build to edit multiple records. It's complicated, doesn't map straight to the database, and there can be any number of records. I have the code written so all of the data is passed to the view as a hash. Like this:
#formdata = {"datafield_1"=>"value_1", "datafield_2"=>"value_2"}
What I want to do is to create something like:
f.textfield :datafield_1
f.textfield :datafield_2
f.textfield :datafield_3
etc. etc. etc.
But I don't know how to pass the index of my for loop into the variable name. In short, how do I do :datafield_i where i is my index?
<% %w(1 2 3).each do |i| %>
<%= f.textfield(:"datafield_#{i}") -%>
<% end %>
or
<% #formdata.keys.each do |datafield| %>
<%= f.textfield(datafield.to_sym) -%>
<% end %>

Ruby if else syntax

I have an app where users can sign up with Facebook, in which case I take the Facebook image, or they can sign up using regular authentication, in which case they're uploading a photo using CarrierWave gem.
Because of some sort of conflict, I had to create a column in the database (:image)to store the url to the Facebook image, and then a different column (:dimage) to store the url for the image if they signed up using the non-facebook sign up.
So when it comes time to display the user, I'm checking to see if there's an :image, and, if not, then displaying the other :dimage.
However, I don't want to require them to upload an image, in which case, I want to display an anon image (here represented by the rails.png). However, the Rails.png isn't showing up (just a broken image path), so I'm assuming there's some sort of error with my if else syntax because the image_tag("rails.png") is taken straight from the api
<% if user.image %>
<%= image_tag user.image %>
<% elsif user.dimage %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
The generated html on the rails.png
<img alt="Assets" src="/assets/">
Use the present? method to check if the attribute is not empty:
<% if user.image.present? %>
<%= image_tag user.image %>
<% elsif user.dimage.present? %>
<%= image_tag user.dimage_url(:thumb).to_s %>
<% else %>
<%= image_tag("rails.png") %>
<% end %>
You'll save yourself a headache or two because in Ruby the only falsehoods are false and nil. This means that the empty string "" will actually return true.
You need to look at look into user.image.nil? user.image.empty? and user.image.blank? depending on what you're using and apply the same to the rest of your conditional. You need to find out specifically what your looking for or the if else won't give you what you want. I would try empty first which checks if something is nil or is an empty string.
When you create a new rails application, rails stores its logo in PROJECT_PATH/app/assets/images folder. Could you check if that image exists?

Resources