Answers.Body: <p>Your <code>blogroll</code> can be treated as a PHP function. In order to use functions you have to pass the required parameters. In your case your <code>blogroll</code> expects the parameter <code>entry</code></p>
  
  <pre><code>{{ blogroll.blogroll(entry) }}
  or
  {{ blogroll.blogroll(category) }}
  </code></pre>
  
  <p>will work</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-03T12:38:19.613Z
Answers.Text: Perfect, thanks! Seems like I was indeed simply missing that parameter.
Answers.UserDisplayName: Tobias S.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-03T10:19:36.510Z
Answers.LastActivityDate: 2018-04-03T10:19:36.510Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I am new to the concept of Twig templates and I can’t get it right from the documentation … What I am trying to achieve is to have a reusable Blogroll snippet that outputs entries in different contexts: ALL entries on the homepage and then according to the entry’s category in other areas of the site.</p>
  
  <p>I created a file <code>blogroll.html</code> in <code>templates/macros</code> containing my blogroll code which works fine if used directly in a template (simplified the HTML here):</p>
  
  <pre><code>{% macro blogroll(entry) %}
  &lt;a href="{{ entry.url }}"&gt;
    &lt;h2&gt;{{ entry.title }}&lt;/h2&gt;
  &lt;/a&gt;
  {% endmacro %}
  </code></pre>
  
  <p>In my <code>index.html</code> for the homepage I try inserting it with this code:</p>
  
  <pre><code>&lt;section class="blogroll"&gt;
  {% for entry in craft.entries.section('blog').limit(15).find() %}
  {% for entry in entries %}
    {% import "macros/blogroll" as blogroll %}
    {{ blogroll.blogroll }}
  {% endfor %}
  &lt;/section&gt;
  </code></pre>
  
  <p>In my category related templates I filter for the <code>category</code> first and try to insert it this way:</p>
  
  <pre><code>{% set entries = craft.entries.relatedTo(category) %}
  {% for entry in entries %}
    {% import "macros/blogroll" as blogroll %}
  {{ blogroll.blogroll }}
  {% endfor %}
  </code></pre>
  
  <p>In both cases I get a <code>Impossible to access an attribute ("url") on a null variable.</code> error though.</p>
  
  <p>Any hints where I am not getting the concept right or screw something up while passing the variable?</p>
  
Tags: <entries><category><loop><macro><null>
Title: Macro for entry loop causing 'null' variable template error
ThreadText: # Macro for entry loop causing 'null' variable template error
  
  (Asked by: username_0 on 2018-04-03)
  
  I am new to the concept of Twig templates and I can’t get it right from the documentation … What I am trying to achieve is to have a reusable Blogroll snippet that outputs entries in different contexts: ALL entries on the homepage and then according to the entry’s category in other areas of the site.
  
  I created a file `blogroll.html` in `templates/macros` containing my blogroll code which works fine if used directly in a template (simplified the HTML here):
  
  ```
  {% macro blogroll(entry) %}
  <a href="{{ entry.url }}">
    <h2>{{ entry.title }}</h2>
  </a>
  {% endmacro %}
  
  ```
  
  In my `index.html` for the homepage I try inserting it with this code:
  
  ```
  <section class="blogroll">
  {% for entry in craft.entries.section('blog').limit(15).find() %}
  {% for entry in entries %}
    {% import "macros/blogroll" as blogroll %}
    {{ blogroll.blogroll }}
  {% endfor %}
  </section>
  
  ```
  
  In my category related templates I filter for the `category` first and try to insert it this way:
  
  ```
  {% set entries = craft.entries.relatedTo(category) %}
  {% for entry in entries %}
    {% import "macros/blogroll" as blogroll %}
  {{ blogroll.blogroll }}
  {% endfor %}
  
  ```
  
  In both cases I get a `Impossible to access an attribute ("url") on a null variable.` error though.
  
  Any hints where I am not getting the concept right or screw something up while passing the variable?
  
  **Tags:** entries, category, loop, macro, null
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-03. Score: 2 | Accepted answer
  
  Your `blogroll` can be treated as a PHP function. In order to use functions you have to pass the required parameters. In your case your `blogroll` expects the parameter `entry`
  
  ```
  {{ blogroll.blogroll(entry) }}
  or
  {{ blogroll.blogroll(category) }}
  
  ```
  
  will work
  
  > **Comments:**
  > **username_0**: Perfect, thanks! Seems like I was indeed simply missing that parameter.  
  
  ---
  
  