<p>Apart from reading the docs on <a href="https://buildwithcraft.com/docs/plugins/introduction">plugin development</a>, I would suggest checking out a plugin called <a href="https://github.com/lindseydiloreto/craft-businesslogic/">Business Logic</a>, which provides a plugin template for creating project specific business logic, with a basic controller, service, and variables file already set up, and doc blocks which explain what the various components do.</p>

<p>For just printing a string I would look into creating a <a href="http://buildwithcraft.com/docs/plugins/variables">custom variable</a> in your plugins 'variable' file. If the method is more complicated then you can use your variable as a wrapper to call one your <a href="http://buildwithcraft.com/docs/plugins/services">service methods</a> defined in your plugin's 'service' file. Here is an example variables file, with a call to a service method:</p>

<pre><code>&lt;?php namespace Craft;

class MypluginVariable
{
    // return the result directly
    public function exampleVariable()
    {
        return 'Anything you want!';
    }
    // call a service method and return the result
    public function exampleServiceMethodWrapper($value)
    {
        return craft()-&gt;myPlugin-&gt;myServiceMethod($value);
    }

}
</code></pre>

<p>You could call these from your template using <code>{{ craft.myplugin.exampleVariable }}</code> and <code>{{ craft.myplugin.exampleServiceMethodWrapper('customValue') }}</code>. These are assuming you want to just print the result of course.</p>

<p>The contactForm plugin that you are looking at is interacting with the plugin through the hidden form field <code>&lt;input type="hidden" name="action" value="contactForm/sendMessage"&gt;</code> which directs craft to submit the form to the contactForm's <code>actionSendMessage()</code> method defined in the plugin's 'controller' file. See <a href="http://buildwithcraft.com/docs/plugins/controllers">controller docs</a> for more info.</p>

<p>Fyi <code>{{ getCsrfInput() }}</code> is a native craft method so won't be in the plugin.</p>

CC BY-SA 3.0
2014-11-23T15:16:13.043Z
2014-11-23T15:58:56.360Z
2014-11-23T15:58:56.360Z
Douglas McDonald
<p>I am a Joomla developer who has been asked to start developing Craft plugins and I'm afraid my brain is so moulded to the joomla way of doing things that I cannot even see how the template links to the plugin and fires events</p>
<p>I'm especially interested in form processing (being the cornerstone of application functionality) and I've been taking a look at the contact form plugin. In the <code>contact.html</code> template I can see <code>{{ getCsrfInput() }}</code> which is Twig syntax for calling a method- but there is no method called <code>getCsrfInput()</code> anywhere in the plugin. However it works, hidden fields are returned with an 'action' pointing to contactForm/sendMessage which (I'm assuming the Craft app decides, because there is no form tag action) fires the actionSendMessage event in the contact form plugin.</p>
<p>I just don't see the sequence! I can program PHP fine, but (perhaps due to ONLY doing things the Joomla way) don't see how plugins are called/included in a page.</p>
<p>It's a silly example, but could someone explain to me how I could get a function to just echo Hello World on a page:</p>
<pre class="lang-php prettyprint-override"><code>function printHello()
{
echo 'Hello world!'
}
</code></pre>
<p>Would this go in the plugin root class? If so, what do I put in the <code>example.html</code> template to call this plugin function?</p>
<p>If I can do that then I can (presumably) use it to do everything else. Just 'including' a PHP function on a page doesn't seem like the Craft way of doing thing- I've read about hooks and events, and they don't make sense to me. Hooks seem to link a plugin to a URI, and it seems to suggest that you don't need to put anything into the template html calling the plugin. The Other, 'Events', is all PHP, so I'm assuming isn't intended to be put into the template files (Any php I put in there is escaped).</p>
<p>So, in short, how can add a hello world output to a page using a plugin (Stupid, but just to get me started)</p>
<p>My thought is that as soon as I can do that I can have a function that grabs form data from a previous page and processes it in order to do some real work.</p>
<p>Thank you so much for your help!</p>
<p>Andy</p>

CC BY-SA 3.0
2017-05-10T00:16:12.880Z
Welcome to the Craft CMS Stack Exchange, Andy! Please consider accepting the answer Douglas provided if it was helpful, or post your own answer if you achieved "Hello World" and can help other Joomla-Craft-plugin-developers along!
Matt Stein
<plugin-development>
Hello World Craft Plugin
# Hello World Craft Plugin

(Asked by: username_0 on 2014-11-23)

I am a Joomla developer who has been asked to start developing Craft plugins and I'm afraid my brain is so moulded to the joomla way of doing things that I cannot even see how the template links to the plugin and fires events
I'm especially interested in form processing (being the cornerstone of application functionality) and I've been taking a look at the contact form plugin. In the contact.html template I can see {{ getCsrfInput() }} which is Twig syntax for calling a method- but there is no method called getCsrfInput() anywhere in the plugin. However it works, hidden fields are returned with an 'action' pointing to contactForm/sendMessage which (I'm assuming the Craft app decides, because there is no form tag action) fires the actionSendMessage event in the contact form plugin.
I just don't see the sequence! I can program PHP fine, but (perhaps due to ONLY doing things the Joomla way) don't see how plugins are called/included in a page.
It's a silly example, but could someone explain to me how I could get a function to just echo Hello World on a page:
function printHello()
{
echo 'Hello world!'
}

Would this go in the plugin root class? If so, what do I put in the example.html template to call this plugin function?
If I can do that then I can (presumably) use it to do everything else. Just 'including' a PHP function on a page doesn't seem like the Craft way of doing thing- I've read about hooks and events, and they don't make sense to me. Hooks seem to link a plugin to a URI, and it seems to suggest that you don't need to put anything into the template html calling the plugin. The Other, 'Events', is all PHP, so I'm assuming isn't intended to be put into the template files (Any php I put in there is escaped).
So, in short, how can add a hello world output to a page using a plugin (Stupid, but just to get me started)
My thought is that as soon as I can do that I can have a function that grabs form data from a previous page and processes it in order to do some real work.
Thank you so much for your help!
Andy

**Tags:** plugin-development
> **Comments:**
> **username_1**: Welcome to the Craft CMS Stack Exchange, Andy! Please consider accepting the answer Douglas provided if it was helpful, or post your own answer if you achieved "Hello World" and can help other Joomla-Craft-plugin-developers along!  

---

## 1 answers

### Answer by username_2 on 2014-11-23. Score: 8

Apart from reading the docs on plugin development, I would suggest checking out a plugin called Business Logic, which provides a plugin template for creating project specific business logic, with a basic controller, service, and variables file already set up, and doc blocks which explain what the various components do.
For just printing a string I would look into creating a custom variable in your plugins 'variable' file. If the method is more complicated then you can use your variable as a wrapper to call one your service methods defined in your plugin's 'service' file. Here is an example variables file, with a call to a service method:
myPlugin->myServiceMethod($value);
    }

}

You could call these from your template using {{ craft.myplugin.exampleVariable }} and {{ craft.myplugin.exampleServiceMethodWrapper('customValue') }}. These are assuming you want to just print the result of course.
The contactForm plugin that you are looking at is interacting with the plugin through the hidden form field  which directs craft to submit the form to the contactForm's actionSendMessage() method defined in the plugin's 'controller' file. See controller docs for more info.
Fyi {{ getCsrfInput() }} is a native craft method so won't be in the plugin.


---

