Ask Your Question

How to Format text in a template?

asked 2013-11-06 02:11:52 -0500

Krist van Besien gravatar image

updated 2013-11-07 05:52:46 -0500

I use puppet together with foreman. I have a module that sets the /etc/motd file. It generates the content of that file from a template.

The template contains, amongst other things this:

<%= @comment %>

The "comment" variable comes from Foreman, and can contain anything from a few words to several lines of text. I want format this text in some way, fix it to for example 40 columns, with proper word wrap.

I know that there exist several "pretty print" functions on ruby. I've tried this:

<% pp( @comment, width = 40) %>

But this threw an error.

Error: Could not retrieve ...
edit retag flag offensive close merge delete


Can you post the error message that you received? You should be able to call functions from inside ERB files.

GregLarkin gravatar imageGregLarkin ( 2013-11-06 15:47:40 -0500 )edit

I've added the error message to the post

Krist van Besien gravatar imageKrist van Besien ( 2013-11-07 05:53:02 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-07 08:21:38 -0500

banjer gravatar image

You'll need to require the prettyprint gem.

Also, pp will spit the output to stdout instead of to the actual resulting template file, so you'll want to store it in a variable first and then output the variable in the template.

<% require 'pp' %>
<% cmmt = PP.pp(@comment,'',width=40) %>
<%= cmmt %>
edit flag offensive delete link more


thanks. This worked. Although not with the desired effect, but now that I know how to get other ruby libraries in to my templates I'll be able to work it out.

Krist van Besien gravatar imageKrist van Besien ( 2013-11-08 08:19:20 -0500 )edit

if you're getting extra whitespace, use -%> in place of the %> tag (notice the added dash). this suppresses line breaks that get added. that could trip you up with what ...(more)

banjer gravatar imagebanjer ( 2013-11-08 08:36:53 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-11-06 02:11:52 -0500

Seen: 542 times

Last updated: Nov 07 '13