Answers.Body: <p>Searching for uses of <code>renderMacro</code> in the Craft code I see that it is always called like:</p>
  
  <pre><code>craft()-&gt;templates-&gt;renderMacro('_includes/forms', 'textield', array(
    array(
      'name' =&gt; 'find',
      'placeholder' =&gt; Craft::t('Find'),
    )
  ));
  </code></pre>
  
  <p>So, what you want is probably</p>
  
  <pre><code>$html = craft()-&gt;templates-&gt;renderMacro($template, $macro, array($args));
  </code></pre>
  
  <p>Which is not at all clear from the <a href="http://buildwithcraft.com/classreference/services/TemplatesService#renderMacro-detail" rel="nofollow">renderMacro</a> documentation...</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T08:13:15.990Z
Answers.Text: Agreed, i'm just getting an empty var back from this call :( - No error which is good mind.
Answers.UserDisplayName: Sam Hibberd
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T08:33:35.423Z
Answers.Text: So it looks like the renderMacro function is throwing me a dummy, the issue looks to be with the returnJson function messing with the raw html that the rendermacro function is returning. Any thoughts?
Answers.UserDisplayName: Sam Hibberd
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T08:09:44.660Z
Answers.LastActivityDate: 2015-04-24T08:09:44.660Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <p>The format of the <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="nofollow">include tag</a> is</p>
  
  <pre><code>{% include 'template' with {var1: value1, var2: value2} only %}
  </code></pre>
  
  <p>What you need after the <code>with</code> is an associative array (an array with keys and values).</p>
  
  <p>In your specific case you want this:</p>
  
  <pre><code>{% include "_macros/elements/listing" with {config: config} only %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T07:16:01.840Z
Answers.Text: At a twig level the config variable is an associative array in the same way the macros work in the the craft core. Isn't  it more likely the format of the $args variable??
Answers.UserDisplayName: Sam Hibberd
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T07:53:46.397Z
Answers.Text: I think you're right. Examples of renderMacro in the Craft code look like:
  `renderMacro('template', 'macroName', array(array('key'=>'value')))`. So `renderMacro($template, $macro, array($args))`?
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-24T05:36:43.313Z
Answers.LastActivityDate: 2015-04-24T05:36:43.313Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>Im trying to render a site macro from my plugin, i’m getting the following error:</p>
  
  <pre><code>Argument 1 passed to Twig_Template::display() must be of the type array, object given, called in /path/to/file.php on line 51 and defined in "_macros/elements" at line 3
  </code></pre>
  
  <p>My service contains the following method:</p>
  
  <pre><code>public function renderSiteMacro($template, $macro, array $args)
  {
      // Get the current template path
      $originalPath = craft()-&gt;path-&gt;getTemplatesPath();
  
      // Point Twig at the site templates
      craft()-&gt;path-&gt;setTemplatesPath(craft()-&gt;path-&gt;getSiteTemplatesPath());
  
      // Render the macro.
      $html = craft()-&gt;templates-&gt;renderMacro($template, $macro, $args);
  
      // Restore the original template path
      craft()-&gt;path-&gt;setTemplatesPath($originalPath);
  
      return TemplateHelper::getRaw($html);
  }
  </code></pre>
  
  <p>And i'm calling it as follows:</p>
  
  <pre><code>$args = array(
      'entry' =&gt; $user,
      'layout' =&gt; 'friend'
  );
  
  $userHtml = craft()-&gt;logic-&gt;renderSiteMacro('_macros/elements', 'listing', $args);
  </code></pre>
  
  <p>The issue appears to be with the include line in my macro template file:</p>
  
  <pre><code>{% macro listing(config) %}
  {% include "_macros/elements/listing" with config only %}
  {% endmacro %}
  </code></pre>
  
  <p>Does anyone know what i'm missing?</p>
  
Tags: <plugin-development><macro>
Title: How to render a macro from a plugin?
ThreadText: # How to render a macro from a plugin?
  
  (Asked by: username_0 on 2015-04-23)
  
  Im trying to render a site macro from my plugin, i’m getting the following error:
  
  ```
  Argument 1 passed to Twig_Template::display() must be of the type array, object given, called in /path/to/file.php on line 51 and defined in "_macros/elements" at line 3
  
  ```
  
  My service contains the following method:
  
  ```
  public function renderSiteMacro($template, $macro, array $args)
  {
      // Get the current template path
      $originalPath = craft()->path->getTemplatesPath();
  
      // Point Twig at the site templates
      craft()->path->setTemplatesPath(craft()->path->getSiteTemplatesPath());
  
      // Render the macro.
      $html = craft()->templates->renderMacro($template, $macro, $args);
  
      // Restore the original template path
      craft()->path->setTemplatesPath($originalPath);
  
      return TemplateHelper::getRaw($html);
  }
  
  ```
  
  And i'm calling it as follows:
  
  ```
  $args = array(
      'entry' => $user,
      'layout' => 'friend'
  );
  
  $userHtml = craft()->logic->renderSiteMacro('_macros/elements', 'listing', $args);
  
  ```
  
  The issue appears to be with the include line in my macro template file:
  
  ```
  {% macro listing(config) %}
  {% include "_macros/elements/listing" with config only %}
  {% endmacro %}
  
  ```
  
  Does anyone know what i'm missing?
  
  **Tags:** plugin-development, macro
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-04-24. Score: 2 | Accepted answer
  
  Searching for uses of `renderMacro` in the Craft code I see that it is always called like:
  
  ```
  craft()->templates->renderMacro('_includes/forms', 'textield', array(
    array(
      'name' => 'find',
      'placeholder' => Craft::t('Find'),
    )
  ));
  
  ```
  
  So, what you want is probably
  
  ```
  $html = craft()->templates->renderMacro($template, $macro, array($args));
  
  ```
  
  Which is not at all clear from the [renderMacro](http://buildwithcraft.com/classreference/services/TemplatesService#renderMacro-detail) documentation...
  
  > **Comments:**
  > **username_0**: Agreed, i'm just getting an empty var back from this call :( - No error which is good mind.  
  > **username_0**: So it looks like the renderMacro function is throwing me a dummy, the issue looks to be with the returnJson function messing with the raw html that the rendermacro function is returning. Any thoughts?  
  
  ---
  
  ### Answer by username_1 on 2015-04-24. Score: 2
  
  The format of the [include tag](http://twig.sensiolabs.org/doc/tags/include.html) is
  
  ```
  {% include 'template' with {var1: value1, var2: value2} only %}
  
  ```
  
  What you need after the `with` is an associative array (an array with keys and values).
  
  In your specific case you want this:
  
  ```
  {% include "_macros/elements/listing" with {config: config} only %}
  
  ```
  
  > **Comments:**
  > **username_0**: At a twig level the config variable is an associative array in the same way the macros work in the the craft core. Isn't  it more likely the format of the $args variable??  
  > **username_1**: I think you're right. Examples of renderMacro in the Craft code look like: `renderMacro('template', 'macroName', array(array('key'=>'value')))`. So `renderMacro($template, $macro, array($args))`?  
  
  ---
  
  