<p>There's a lot going on here, but a) assuming you're really using Craft 3 and b) you generated your plugin, service and variable scaffolding on <a href="https://pluginfactory.io" rel="nofollow noreferrer">https://pluginfactory.io</a>, this should work:</p>

<p><code>KGVariable.php</code> Variable</p>

<pre><code>&lt;?php

namespace kg\variables;

class KGVariable
{
    public function sayHello()
    {
        return \kg\KG::$plugin-&gt;useTR-&gt;exampleService();
    }
}
</code></pre>

<p><code>UseTR.php</code> Service</p>

<pre><code>&lt;?php

namespace kg\services;

use craft\base\Component;

class UseTR extends Component
{
    public function exampleService()
    {
        $result = 'hello';
        return $result;
    }
}
</code></pre>

<p>Then using this in a template will return <code>hello</code>:</p>

<pre><code>{{ craft.kG.sayHello() }}
</code></pre>

CC BY-SA 4.0
2018-11-06T03:43:27.373Z
It gives me the following error:
user8983
CC BY-SA 4.0
2018-11-06T03:49:39.907Z
Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()
user8983
CC BY-SA 4.0
2018-11-06T19:52:31.417Z
You've got something going on somewhere else, then. The string `keyword` isn't in any of the code posted above or generated by pluginfactory.io.
Brad Bell
CC BY-SA 4.0
2018-11-05T20:04:22.410Z
2018-11-05T20:04:22.410Z
Brad Bell
<p>I have a service called <code>useTR.php</code>. It has a public method <code>exampleService()</code>. </p>

<p>In the variables folder I have added the following code in my <code>KGVariable.php</code> (KG is my plugin name)</p>

<pre><code>public function sayHello() {
    return craft()-&gt;useTR-&gt;exampleService();
}
</code></pre>

<p>In my templates folder I have an html that gets loaded after a form submission and that html has the following code. </p>

<pre><code>&lt;p&gt;Thank you {{ craft.KG.sayHello() }} will contact you soon&lt;/p&gt;
</code></pre>

<p>When this HTML is loaded, I get the following error:</p>

<blockquote>
  <p>Twig Runtime Error – Twig_Error_Runtime
  Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()</p>
</blockquote>

<p>What am I doing wrong? Kindly help (I am a beginner at Craft). Thanks.</p>

CC BY-SA 4.0
2018-11-04T19:44:10.943Z
Do you use Craft 3 or Craft 2. The code is for Craft 2 but the exception for Craft 3. It's likely you are using the wrong code. Furthermore the exception is not really related to your code. You might want to take a look at pluginfactory.io - it creates the entire structure with example code for you
Robin Schambach
CC BY-SA 4.0
2018-11-05T18:15:04.147Z
I am using Craft 3. I created the entire structure using pluginfactory.io. Do you see any issue with the way I am writing the code, I mean any obvious issue.
user8983
CC BY-SA 4.0
2018-11-05T18:26:16.127Z
Yes it's for Craft 2 and not Craft 3. There is no `craft()` function in v3
Robin Schambach
<craft3>
Plugin service and variable
# Plugin service and variable

(Asked by: username_0 on 2018-11-04)

I have a service called useTR.php. It has a public method exampleService(). 
In the variables folder I have added the following code in my KGVariable.php (KG is my plugin name)
public function sayHello() {
    return craft()->useTR->exampleService();
}

In my templates folder I have an html that gets loaded after a form submission and that html has the following code. 
Thank you {{ craft.KG.sayHello() }} will contact you soon

When this HTML is loaded, I get the following error:

Twig Runtime Error – Twig_Error_Runtime
  Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()

What am I doing wrong? Kindly help (I am a beginner at Craft). Thanks.

**Tags:** craft3
> **Comments:**
> **username_1**: Do you use Craft 3 or Craft 2. The code is for Craft 2 but the exception for Craft 3. It's likely you are using the wrong code. Furthermore the exception is not really related to your code. You might want to take a look at pluginfactory.io - it creates the entire structure with example code for you  
> **username_0**: I am using Craft 3. I created the entire structure using pluginfactory.io. Do you see any issue with the way I am writing the code, I mean any obvious issue.  
> **username_1**: Yes it's for Craft 2 and not Craft 3. There is no `craft()` function in v3  

---

## 1 answers

### Answer by username_2 on 2018-11-05. Score: 1

There's a lot going on here, but a) assuming you're really using Craft 3 and b) you generated your plugin, service and variable scaffolding on https://pluginfactory.io, this should work:
KGVariable.php Variable
useTR->exampleService();
    }
}

UseTR.php Service

Then using this in a template will return hello:
{{ craft.kG.sayHello() }}

> **Comments:**
> **username_0**: It gives me the following error:  
> **username_0**: Calling unknown method: craft\web\twig\variables\CraftVariable::keyword()  
> **username_2**: You've got something going on somewhere else, then. The string `keyword` isn't in any of the code posted above or generated by pluginfactory.io.  

---

