<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>

CC BY-SA 3.0
2015-04-24T08:13:15.990Z
Agreed, i'm just getting an empty var back from this call :( - No error which is good mind.
Sam Hibberd
CC BY-SA 3.0
2015-04-24T08:33:35.423Z
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?
Sam Hibberd
CC BY-SA 3.0
2015-04-24T08:09:44.660Z
2015-04-24T08:09:44.660Z
Marion Newlevant
<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>

CC BY-SA 3.0
2015-04-24T07:16:01.840Z
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??
Sam Hibberd
CC BY-SA 3.0
2015-04-24T07:53:46.397Z
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))`?
Marion Newlevant
CC BY-SA 3.0
2015-04-24T05:36:43.313Z
2015-04-24T05:36:43.313Z
Marion Newlevant
<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>

<plugin-development><macro>
How to render a macro from a plugin?
# 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 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 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))`?  

---

