You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/frontend/html/implement-components.md
+26-89Lines changed: 26 additions & 89 deletions
Original file line number
Diff line number
Diff line change
@@ -30,70 +30,40 @@ class Standard
30
30
// optional
31
31
}
32
32
33
-
public function init()
33
+
public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
34
34
{
35
35
// optional
36
36
}
37
37
38
-
public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
38
+
protected function getSubClientNames() : array
39
39
{
40
40
// optional
41
41
}
42
42
43
-
protected function getSubClientNames() : array
43
+
public function init()
44
44
{
45
45
// optional
46
46
}
47
-
}
48
-
```
49
-
50
-
Differences arise from the required code inside these methods as they have to care about caching (if you want to) and exception handling. A component can implement the same optional methods as any subpart. For a detailed description of these methods, please refer to the article about [creating new subparts](create-subparts.md#optional-methods).
51
47
52
-
The [getSubClient()](create-subparts.md#getSubClient) and [getSubClientNames()](create-subparts.md#getSubClientNames) methods are also exactly the same as in any other subpart and won't be described in this article again.
53
-
54
-
# init()
55
-
56
-
This method is not affected by caching at all because its purpose is to execute code once during each request. The difference to `init()` methods of subclients is only that you need to catch thrown exceptions and assign error messages to the view if necessary.
57
-
58
-
If you don't need to process any input in your new component, you can copy & paste the code below into your new class:
59
-
60
-
```php
61
-
public function init()
62
-
{
63
-
$context = $this->context();
64
-
$view = $this->view();
48
+
public function modify()
49
+
{
50
+
// optional
51
+
}
65
52
66
-
// your required code
67
-
parent::init();
53
+
public function data( Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : Aimeos\MW\View\Iface
54
+
{
55
+
// optional
56
+
}
68
57
}
69
58
```
70
59
71
-
The only thing you have to **adapt is the name of the error list** assigned to the view. It should be named after your class name to something like `$view->...ErrorList`.
72
-
73
-
The cascade of `catch()` statements ensures that all exceptions are caught. Furthermore, all error messages translated that are passed to the view and shown to the customers. Unspecific exceptions are logged and only a generic error message is shown in the front-end to avoid giving away sensitive information.
74
-
75
-
You need to print these error messages in your component view which is described in the next section.
76
-
77
-
# Display error messages
78
-
79
-
The messages of the exceptions thrown in the different methods are assigned to the view using the `error` variable, which is an array of error messages. You need to add a snippet similar to this one at the top of your component body view:
These few lines of code create an HTML block that will contain all error messages. It will be styled by the used theme, so you don't have to care about this.
60
+
A component can implement the same optional methods as any subpart. For a detailed description of these methods, please refer to the article about [creating new subparts](create-subparts.md#optional-methods).
91
61
92
-
# With content caching
62
+
The [data()](create-subparts.md#data), [getSubClient()](create-subparts.md#getSubClient), [getSubClientNames()](create-subparts.md#getSubClientNames) and [modify()](create-subparts.md#modify) methods are also exactly the same as in any other subpart and won't be described in this article again.
93
63
94
-
Components that can cache its output are extremely fast. Once the content is generated and stored, it can be retrieved within milliseconds and directly pushed to the browser. The downside is that some additional code is needed.
64
+
Differences arise from the required code inside these methods as they have to care about caching (if you want to). Components that can cache its output are extremely fast. Once the content is generated and stored, it can be retrieved within milliseconds and directly pushed to the browser. The downside is that some additional code is needed.
95
65
96
-
##body()
66
+
# body()
97
67
98
68
When caching comes into play, the first thing you have to think about is: What does your output depend on? Usually, two external sources can influence what content needs to be generated: The request parameters and the configuration settings. Both has to be part of the cache key.
99
69
@@ -129,7 +99,7 @@ The details for this are described in the article about [creating new subparts](
129
99
130
100
In doubt, have a look into a full example of a working [body() component method](https://github.com/aimeos/ai-client-html/blob/master/src/Client/Html/Catalog/Detail/Standard.php) which implements caching.
131
101
132
-
##header()
102
+
# header()
133
103
134
104
For `header()`, implementing caching is very similar to the implementation of `body()`. You also have to specify which parameters are used in your component and what's the shared configuration prefix for all settings. The calls to `getCached()` and `setCached()` require "header" to be passed to ensure that the content is stored for the header.
135
105
@@ -156,50 +126,17 @@ Only keep in mind that you also need to call the `modify()` method after success
156
126
157
127
In doubt, have a look into a full example of a working [header() component method](https://github.com/aimeos/ai-client-html/blob/master/src/Client/Html/Catalog/Detail/Standard.php) which implements caching.
158
128
159
-
# Factory class
129
+
# Display error messages
160
130
161
-
All components are instantiated by factories which care about creating the HTML client and decorating it with additional classes added via configuration. The factory class is a rather simple piece of code that contains only a `create()` method:
131
+
The messages of the exceptions thrown in the different methods are assigned to the view using the `error` variable, which is an array of error messages. You need to add a snippet similar to this one at the top of your component body view:
The code above is a factory for the catalog detail client. You can copy the code and replace the "Catalog\Detail" and "catalog/detail" strings with the name of your own component. For example, if you want to create a new "catalog homepage" component, you should replace the strings like this:
192
-
193
-
```
194
-
Catalog\Detail -> Catalog\Homepage
195
-
catalog/detail -> catalog/homepage
196
-
```
197
-
198
-
Component factories for other purposes can created the same way, e.g. for a "basket upsell" component, replace the strings in that way:
The factory and the default implementation of your component must be saved to the appropriate directory, i.e. to the *./src/Catalog/Homepage* or *./src/Basket/Upsell* directory of your own extension.
142
+
These few lines of code create an HTML block that will contain all error messages. It will be styled by the used theme, so you don't have to care about this.
0 commit comments