[chef] Re: Re: Re: Re: how do I share common functions between templates


Chronological Thread 
  • From: Matthew Moretti < >
  • To:
  • Subject: [chef] Re: Re: Re: Re: how do I share common functions between templates
  • Date: Thu, 17 Jan 2013 14:52:34 -0500

Xianfeng,

The responses so far are very good answers to the general question.  I think the best solution for your case is John's solution.  However,  I thought I might mention that the specific ruby code you posted could be simplified quite a bit.  I would write that as

my_array.join(', ') if my_array.is_a?(Array)

Actually, I just noticed that your method surrounds each element in the array with escaped double quotes.  If you want that, you could do

my_array.map{ |s| "\"#{s}\"" }.join(', ') if my_array.is_a?(Array)

-Matt Moretti


On Mon, Jan 14, 2013 at 9:52 PM, Sascha Bates < " target="_blank"> > wrote:
Tapas?


On Mon, Jan 14, 2013 at 5:04 PM, Bryan Stenson < " target="_blank"> > wrote:
Surely, we can all agree to call these something more chef-fy...a "buffet" or "serving dish".


On Mon, Jan 14, 2013 at 3:02 PM, Josiah Kiehl < " target="_blank"> > wrote:
Partials are a new feature in chef (just merged to master, in fact: https://github.com/opscode/chef/pull/498)

For now, I'm not sure there is a good solution. You can possibly use ERB to evaluate the a subtemplate and pass that into a template via variables.


On Mon, Jan 14, 2013 at 2:38 PM, < " target="_blank"> > wrote:
Hi,

I have a snippet as following and want to share it in mulitiple templates. Is
there a way to do it?

thanks,

Xianfeng

<%
def output_array(s)
    str = ''
    if s.is_a?(Array)
       if s.length >= 1
          str = s.at(0)
          str = "\"#{str}\""
          if s.length > 1
             for i in 1..(s.length - 1)
                 w = s.at(i)
                 str = "#{str}, \"#{w}\""
             end
          end
       end
    end
    "#{str}"
end
%>







Archive powered by MHonArc 2.6.16.

§