title: Wrapping long text sections in Jinja2
body: <p>I have the definition of a variable, it's name and an associated comment in a YAML file and am trying to use Jinja2 to create an appropriate target file; in this case a proprietary config file</p>
  
  <pre><code>...
  - comment: &gt;
        This is a comment which will almost certainly end up longer than standard eighty characters or at least on the occasion on which it does. 
    name: my_useful_variable
    value: /a/long/example/path/to/my/file.txt
  </code></pre>
  
  <p>I would like this text to be rendered as follows:</p>
  
  <pre><code># This is a comment which will almost certainly end up
  # longer than standard eighty characters or at least on
  # the occasion on which it does.
  my_useful_variable = "/a/long/example/path/to/my/file.txt"
  </code></pre>
  
  <p><strong>Does Jinja2 have any way of wrapping text so that the long comment line is limited in length and split over however many lines is necessary?</strong></p>
  
  <p>So far I have:</p>
  
  <pre><code># {{item.comment}}    
  {{item.name}} = "{{item.value}}"
  </code></pre>
  
  <p>But this of course does not deal with the length of the comment.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Following on from the answer provided by @blhsing below, I came up with the following macro, which works fine for basic variables and simple lists (i.e. not dictionaries or more complex hierarchical data structures:</p>
  
  <pre><code>{% macro set_params(param_list, ind=4, wid=80) -%}
  {% for item in param_list %}
  {% if item.comment is defined %}{{item.comment|wordwrap(wid - ind - 2)|replace('',  ' ' * ind +'# ', 1)|replace('\n', '\n' + ' ' * ind + '# ')}}
  {% endif %}
  {% if item.value is iterable and item.value is not string %}{{item.name|indent(ind, True)}} = [ {% for item_2 in item.value %}{{item_2}}{{ ", " if not loop.last else " " }}{% endfor %}{% else %}{{item.name|indent(ind, True)}} = {{item.value}}{% endif %}
  {% endfor %}
  {%- endmacro %}
  </code></pre>
  
  <p>To use this, simply pass a list of items similar to the spec given at the top together with the indentation and the page width.</p>
  
  <p>A bit of explanation:</p>
  
  <ul>
  <li>Line 3, If comment is defined then it is word wrapped to the correct length bearing in mind the width and the indent. The first replace deals with indenting the first line and the second indents subsequent lines. All prefixed with '# '</li>
  <li>Line 5, depending on whether the variable is simple or iterable, it is rendered in the form <code>name = value</code> or <code>name = [ value1, value2, value3 ]</code></li>
  </ul>
  
  <p>Of course, it is not fool-proof but it meets my basic requirements.</p>