A basic Hello World component example?

Yeti
Member | 8
+
+1
-

I can't seem to get my head around creating a basic component without coming across an error such as “Component with name ‘helloControl’ does not exist”.

Is there a basic complete “Hello World” example around that I could look at? Just something that prints “Hello World” from any views that include {control hello}.

So far I have the folder App/Components with a file helloControl.php that contains the following

<?php

declare(strict_types=1);

namespace App\Components;

use Nette\Application\UI\Control;

class HelloControl extends Control
{
	// Not sure what to do here for example to print a simple "Hello World"
    // message to any template that includes {control hello}
}

I added it as a service
- App\Components\HelloControl

But that's where I get stumped and the errors start to appear

Last edited by Yeti (2023-09-21 03:10)

mystik
Member | 312
+
+2
-

Where and how do you use that component? If you want to use component in some presenter you have to add component creation method to that presenter

public function createComponentHello(): HelloControl {
  return new HelloControl();
}

Component instance is created automatically by calling this presenter method first time component is used. If this method is missing component cannot be instantiated and you get that error.

In component itself you should add method render() that is called when component is rendered. It can be simple echo "Hello world"

Last edited by mystik (2023-09-21 08:51)

materix
Backer | 83
+
0
-

Are Components only to be used in presenters?

mystik
Member | 312
+
+1
-

You can use components in components too. Logic is the same. But at top level every request is processed by presenter.

materix
Backer | 83
+
0
-

Thanks for the reply, which I guess also answers this question of mine: https://forum.nette.org/…ide-of-nette.

The Nette Component-Model is primarily used for Nette Presenters, right? 🙂

mystik
Member | 312
+
+1
-

It is build primarily for nette but I think there is nothing that prevents to use it witnout Nette Application

dsar
Backer | 53
+
+1
-

I see components as a reusable pieces of software in the small, while extensions as a reusable pieces of software in the large.

Other frameworks only offer the latter (for example Symfony uses bundles) and you have no way (or complicated way) to have reausability within a single project. Using bundles for simple tasks is just overkilling.

In my opinion using components outside Nette doesn't make much sense because they are integrated in the whole framework (you have reusability especially with renderable components)

Yeti
Member | 8
+
0
-

Ok thanks for the reply but I am now getting the following error Class "App\Presenters\HelloControl" not found (Not sure why it's not looking in App\Controllers)

Again I have - App\Components\HelloControl added to common.neon and the HelloControl.php file in app\Components has been updated to the following

declare(strict_types=1);

namespace App\Components;

use Nette\Application\UI\Control;

class HelloControl extends Control
{
    public function render(): void
    {
        echo "Hello World";
    }
}

Now in the presenter I have the following

declare(strict_types=1);

namespace App\Presenters;

final class SomePresenter extends BasePresenter
{

   	public function createComponentHello(): HelloControl {
        return new HelloControl();
    }

    public function renderIndex(): void
    {
    }
}

And finally in the index.latte template I have {control hello}

I am obviously missing something somewhere?

mystik
Member | 312
+
+2
-

You are missing use App\Components\HelloControl in your presenter so HelloControl is interpreted as class in App\Presenters namespace.

Adding component to neon is useless. You create instance in createComponent method not using ot from DI container.

Yeti
Member | 8
+
0
-

mystik wrote:

You are missing use App\Components\HelloControl in your presenter so HelloControl is interpreted as class in App\Presenters namespace.

Adding component to neon is useless. You create instance in createComponent method not using ot from DI container.

That was indeed the problem. Thanks a lot for all your help!