<p>I'm not aware of this being possible. The control panel uses a different template path than the front end. This makes plugin templates generally inaccessible. One possible option is to create a template variable that runs a macro or renders a template on behalf of the user. Something like:</p>

<p><code>{{ craft.pluginHandle.macro(arguments) }}</code></p>

<p>You will need to make sure that the template path is set to the Control Panel so that your macro can be found, and that it's set back afterwards. At the start of the function set this:</p>

<p><code>craft()-&gt;path-&gt;setTemplatesPath(craft()-&gt;path-&gt;getCpTemplatesPath())</code></p>

<p>Then when you are done rendering your macro (preferably to a variable to return later), you set it back:</p>

<p><code>craft()-&gt;path-&gt;setTemplatesPath(craft()-&gt;path-&gt;getSiteTemplatesPath())</code></p>

CC BY-SA 3.0
2014-06-23T01:41:01.500Z
So, I just attempted this, but renderMacro seems to only have access to the front end template path, even when doing it through template vars. :-/
Christopher Mancini
CC BY-SA 3.0
2014-06-23T02:04:28.407Z
My bad. I added a couple commands that will allow the plugin to find the macro template for rendering.
Bryan Redeagle
CC BY-SA 3.0
2014-06-22T21:04:53.050Z
2014-06-23T02:02:59.277Z
2014-06-23T02:02:59.277Z
Bryan Redeagle
<p>I have a plugin that I am writing and I want to make available a macro that can be used within the front end sites templates. What is the best way to do that?</p>

CC BY-SA 3.0
2014-06-22T19:14:24.407Z
Do you need to specify it as a macro or would a Twig function work, too?
Victor
CC BY-SA 3.0
2014-06-22T20:01:45.760Z
What are you trying to do with the macro? What purpose is it serving?
Bryan Redeagle
CC BY-SA 3.0
2014-06-22T20:29:25.370Z
@VictorIn, twig functions could probably work, however it doesn't feel right having several lines of markup within PHP classes....
Christopher Mancini
CC BY-SA 3.0
2014-06-22T20:31:31.797Z
@ChrisMancini Can you add an example of what you are trying to do to your question?
Victor
CC BY-SA 3.0
2014-06-22T20:31:33.270Z
@BryanRedeagle, I see many use cases for this sort of functionality, but my first use case is for forms that need to be submitted that are core to my plugins functionality. The end user could invoke the macro passing through a few required / optional parameters and it renders accordingly. Similar to how we can use Craft form macros within our plugins CP.
Christopher Mancini
<templating><plugin-development>
How can I create a template macro within my plugin for use within front end templates?
# How can I create a template macro within my plugin for use within front end templates?

(Asked by: username_0 on 2014-06-22)

I have a plugin that I am writing and I want to make available a macro that can be used within the front end sites templates. What is the best way to do that?

**Tags:** templating, plugin-development

> **Comments:**
> **username_1**: Do you need to specify it as a macro or would a Twig function work, too?  
> **username_2**: What are you trying to do with the macro? What purpose is it serving?  
> **username_0**: @username_1In, twig functions could probably work, however it doesn't feel right having several lines of markup within PHP classes....  
> **username_1**: @ChrisMancini Can you add an example of what you are trying to do to your question?  
> **username_0**: @BryanRedeagle, I see many use cases for this sort of functionality, but my first use case is for forms that need to be submitted that are core to my plugins functionality. The end user could invoke the macro passing through a few required / optional parameters and it renders accordingly. Similar to how we can use Craft form macros within our plugins CP.  

---

## 1 answers

### Answer by username_2 on 2014-06-22. Score: 4 | Accepted answer

I'm not aware of this being possible. The control panel uses a different template path than the front end. This makes plugin templates generally inaccessible. One possible option is to create a template variable that runs a macro or renders a template on behalf of the user. Something like:

`{{ craft.pluginHandle.macro(arguments) }}`

You will need to make sure that the template path is set to the Control Panel so that your macro can be found, and that it's set back afterwards. At the start of the function set this:

`craft()->path->setTemplatesPath(craft()->path->getCpTemplatesPath())`

Then when you are done rendering your macro (preferably to a variable to return later), you set it back:

`craft()->path->setTemplatesPath(craft()->path->getSiteTemplatesPath())`

> **Comments:**
> **username_0**: So, I just attempted this, but renderMacro seems to only have access to the front end template path, even when doing it through template vars. :-/  
> **username_2**: My bad. I added a couple commands that will allow the plugin to find the macro template for rendering.  

---

