title: How to make a macro in Twig?
body: <p>I use a big piece of code for displaying friends and I use it in several templates, so I wanted to make it a macro, but the syntax is a way unusal and I don't know if there is a way this to be done. </p>
  
  <p>The part of the code, which I want to seperate is:</p>
  
  <pre><code>       {% if(fr.email != null) %}
              &lt;p&gt;
                  &lt;span class ="label"&gt;Email Address: &lt;/span&gt; 
                  &lt;a  class="email" href="{{ path('friend_email', {'id': fr.id}) }}"&gt;
                      {{ fr.email }}
                  &lt;/a&gt;
              &lt;/p&gt;
          {% endif %}
  
          {% if(fr.phone != null) %}
              &lt;p&gt;
                  &lt;span class="label"&gt;Phone: &lt;/span&gt;
          {{ fr.phone }}
              &lt;/p&gt;
          {% endif %}
  </code></pre>
  
  <p>and so on for about 10 variables. In another template I use this, but instead of fr.email, fr.phone and so on, I need friend.email, friend.phone...</p>
  
  <p>I tried this but without success:</p>
  
  <pre><code>{% macro display_friend(fr) %}
  
      {% if({{ fr }}.email != null) %}
              &lt;p&gt;
                  &lt;span class ="label"&gt;Email Address: &lt;/span&gt; 
                  &lt;a  class="email" href="{{ path('friend_email', {'id': {{ fr }}.id}) }}"&gt;
                      {{ {{ fr }}.email }}
                  &lt;/a&gt;
              &lt;/p&gt;
          {% endif %}
  
          {% if({{ fr }}.phone != null) %}
              &lt;p&gt;
                  &lt;span class="label"&gt;Phone: &lt;/span&gt;
          {{ {{ fr }}.phone }}
              &lt;/p&gt;
          {% endif %}
  {% endmacro %}
  </code></pre>
  
  <p>If necessary, I can use fr.email, fr.phone, fr.* ... in each template, so maybe inheritance will work? </p>
  
  <p>So my question is: is there a way to make this part of code macro and if yes will it be better or inheritance will be better?</p>