HTML Function
This component is somewhat a widget, and intended to replace an HTML tag with a large block of HTML code that also requires processing via PHP each time it is displayed. This could be anything such as a real-time stock ticker, a user's trading activity, and so on. You may create a HTML function by using the create html-function CLI command, such as:
apex create html-function <PACKAGE> <ALIAS>
apex create html-function my-shop order-history
Using the above example, two new files named OrderHistry.php
and OrderHistory.html
were created within the /src/MyShop/Opus/HtmlFunctions/
directory. The PHP class contains one method named render()
which accepts the following parameters.
Parameter | Type | Description |
---|---|---|
$html |
string | The contents of the .html file that was created, if there is any contents. |
$e |
StackElement | The HTML tag that called the class. This variable is passed to retrieve the attributes of the HTML tag. |
You may optionally place any desired HTML code within the .html file that was generated, which is then passed as the $html
argument to the render()
method. This is not required, but may be useful if you're outputting a large block of HTML with merge fields, et al.
Then simply process whatever is necessary within the render()
method, and whatever the method returns is what will be displayed within the web browser.
Displaying HTML Functions on Web Pages
You can place HTML functions anywhere within any view that is rendered by Apex with a single HTML tag, which will then be replaced by the results of the render()
method. Using the above example, the HTML tag would be:
<s:function alias="my-shop.order-history">
You may also include any desired attributes within the tag, including those with merge fields as values, for example:
<s:function alias="my-shop.order-history" type="premium" uuid="~uuid~">
This will then execute the HTML function located at /src/MyShop/Opus/HtmlFunctions/OrderHistory.php
, and within the render()
method you may retrive those two attributes with:
$type = $e->getAttr('type') ?? 'none';
$uuid = $e->getAttr('uuid') ?? '';