0

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?

2 Answers 2

2

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

2
  • Agreed, i'm just getting an empty var back from this call :( - No error which is good mind. Commented Apr 24, 2015 at 8:13
  • 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? Commented Apr 24, 2015 at 8:33
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 %}
2
  • 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?? Commented Apr 24, 2015 at 7:16
  • 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))? Commented Apr 24, 2015 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.