General question about use statements in JCB templates #1073

Closed
opened 2024-03-11 15:03:25 +00:00 by kommid · 6 comments
Member

Steps to reproduce the issue

Make use of methods of the component's helper class, or other classes like Factory or RouteHelper within a template that is called in a site view. In contrary to site views, templates don't have a tab for class headers and adding the proper use statements to the site view doesn't seem to work

Expected result

site view is displayed correctly

Actual result

Errors like class [Component]Helper not found or class Factory not found are displayed

System information (as much as possible)

  • OS Name & Version: Linux 5.10.0-28-amd64 #1 SMP Debian 5.10.209-2 (2024-01-31) x86_64
  • MySql Version: 10.5.23-MariaDB-0+deb11u1
  • Apache Version: Apache/2.4.56 (Debian)
  • PHP Version: JCB running on PHP 8.0 and site where the component is installed runs on 8.2
  • Joomla Version: JCB runs on J!3..10.12 and site where the component is installed runs on 4.4.3
  • JCB Version: 3.2.0-beta5
  • Browser: Firefox 115.8.0esr (64-Bit)

Additional comments

I just noticed, that there doesn't seem to be a way to include use statements to JCB templates. While updating my components' code to J!4/5 I got some error messages like 'Class "CmvHelper" not found'. This error points to the file JROOT/components/com_cmv/tmpl/vorkonfigurationlp/default_lpvorkonfiguration.php.

default_lpvorkonfiguration.php already has three use statements:
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper as Html;
use Joomla\CMS\Layout\LayoutHelper;

and adding use Kommid\Component\Cmv\Site\Helper\CmvHelper; to the file itself did solve the issue. So I tried to find the spot in JCB where I could insert this in order for it to be compiled into this file, but I didn't find a spot where this worked out.
I'd also like to add
use Kommid\Component\Cmv\Site\Helper\RouteHelper;
use Joomla\CMS\Factory;
to this file....

So what is the correct way to accomplish this?

### Steps to reproduce the issue Make use of methods of the component's helper class, or other classes like Factory or RouteHelper within a template that is called in a site view. In contrary to site views, templates don't have a tab for class headers and adding the proper use statements to the site view doesn't seem to work ### Expected result site view is displayed correctly ### Actual result Errors like class [[[Component]]]Helper not found or class Factory not found are displayed ### System information (as much as possible) - OS Name & Version: Linux 5.10.0-28-amd64 #1 SMP Debian 5.10.209-2 (2024-01-31) x86_64 - MySql Version: 10.5.23-MariaDB-0+deb11u1 - Apache Version: Apache/2.4.56 (Debian) - PHP Version: JCB running on PHP 8.0 and site where the component is installed runs on 8.2 - Joomla Version: JCB runs on J!3..10.12 and site where the component is installed runs on 4.4.3 - JCB Version: 3.2.0-beta5 - Browser: Firefox 115.8.0esr (64-Bit) ### Additional comments I just noticed, that there doesn't seem to be a way to include use statements to JCB templates. While updating my components' code to J!4/5 I got some error messages like 'Class "CmvHelper" not found'. This error points to the file JROOT/components/com_cmv/tmpl/vorkonfigurationlp/default_lpvorkonfiguration.php. default_lpvorkonfiguration.php already has three use statements: use Joomla\CMS\Language\Text; use Joomla\CMS\HTML\HTMLHelper as Html; use Joomla\CMS\Layout\LayoutHelper; and adding use Kommid\Component\Cmv\Site\Helper\CmvHelper; to the file itself did solve the issue. So I tried to find the spot in JCB where I could insert this in order for it to be compiled into this file, but I didn't find a spot where this worked out. I'd also like to add use Kommid\Component\Cmv\Site\Helper\RouteHelper; use Joomla\CMS\Factory; to this file.... So what is the correct way to accomplish this?
Owner

Since the templates are not classes but more like script files, they don't have namespace. So when you open the raw template "template" you will see that the custom code from JCB is place under the use statements.

This means you can in the custom code add any use statements you would like to add beyond those already in the template.

Like this:
image

I know this is not ideal... and we might add the same direct way to eventually target all header areas, but this is how you can do it now in templates, and layouts.

There are three nice placeholders that you can use all over... in custom code areas that are useful to make code work across multiple components.

  • [[[Component]]]
  • [[[ComponentNamespace]]]
  • [[[NamespacePrefix]]]

So that will mean that this use statement:

use Kommid\Component\Cmv\Site\Helper\CmvHelper;

Should then becomes in the JCB GUI:

use [[[NamespacePrefix]]]\Component\[[[ComponentNamespace]]]\Site\Helper\[[[Component]]]Helper;

Then in what ever component that code is used... it will update the placeholders accordingly. Specially with layouts and templates that we use across multiple projects this is very useful.

Since the templates are not classes but more like script files, they don't have namespace. So when you open the [raw template "template"](https://git.vdm.dev/joomla/Component-Builder/src/branch/3.x/admin/compiler/joomla_4/default_site_template.php#L22) you will see that the custom code from JCB is place under the use statements. This means you can in the custom code add any use statements you would like to add beyond those already in the template. Like this: ![image](https://git.vdm.dev/attachments/37619e56-0091-48e4-951a-6e93c1ef862a) I know this is not ideal... and we might add the same direct way to eventually target all header areas, but this is how you can do it now in templates, and layouts. There are three nice placeholders that you can use all over... in custom code areas that are useful to make code work across multiple components. - `[[[Component]]]` - `[[[ComponentNamespace]]]` - `[[[NamespacePrefix]]]` So that will mean that this use statement: ``` use Kommid\Component\Cmv\Site\Helper\CmvHelper; ``` Should then becomes in the JCB GUI: ``` use [[[NamespacePrefix]]]\Component\[[[ComponentNamespace]]]\Site\Helper\[[[Component]]]Helper; ``` Then in what ever component that code is used... it will update the placeholders accordingly. Specially with layouts and templates that we use across multiple projects this is very useful.
Author
Member

Thank you, this sort of did solve my problems:
now I have "defined('_JEXEC') or die;" inbetween my use-statements, but this doesn't seem to be an issue, since it's working:

`use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper as Html;
use Joomla\CMS\Layout\LayoutHelper;

// No direct access to this file
defined('_JEXEC') or die;

use Kommid\Component\Cmv\Site\Helper\CmvHelper;
use Kommid\Component\Cmv\Site\Helper\RouteHelper;
use Joomla\CMS\Factory;`

JCB didn't know the placeholders [ComponentNamespace] and the [NamespacePrefix] and I had to set them up in the placeholder section of JCB in order for them to work, but if this is as it is intended, everything concerning this issue seems fine.

Thank you, this sort of did solve my problems: now I have "defined('_JEXEC') or die;" inbetween my use-statements, but this doesn't seem to be an issue, since it's working: `use Joomla\CMS\Language\Text; use Joomla\CMS\HTML\HTMLHelper as Html; use Joomla\CMS\Layout\LayoutHelper; // No direct access to this file defined('_JEXEC') or die; use Kommid\Component\Cmv\Site\Helper\CmvHelper; use Kommid\Component\Cmv\Site\Helper\RouteHelper; use Joomla\CMS\Factory;` JCB didn't know the placeholders [[[ComponentNamespace]]] and the [[[NamespacePrefix]]] and I had to set them up in the placeholder section of JCB in order for them to work, but if this is as it is intended, everything concerning this issue seems fine.
Owner

Nope JCB does know them, but only on from JCB v3.2.0-beta-6

You should also note the 3 [[[ braces notation not just one [.

Nope JCB does know them, but only on from JCB [v3.2.0-beta-6](https://git.vdm.dev/joomla/pkg-component-builder/releases/tag/v3.2.0-beta6) You should also note the 3 `[[[` braces notation not just one `[`.
Owner

Here you can see the commit for that change two days ago... so its new :)

image

Here you can see the [commit](https://git.vdm.dev/joomla/Component-Builder/commit/f5f308dbf2ca4021f581a1f57ad88e7d1ed1f62b#diff-511212384d1e048c40056a3ddbb1a2677e9d08b4) for that change two days ago... so its new :) ![image](https://git.vdm.dev/attachments/6796611f-f911-4b55-841e-9622a1c2db5a)
Owner

You should therefore take note that your component placeholders can overwrite these, and use these at the same time ;)

Looking at the class you will see it loads the standard placeholders and then load the component placeholders, while using the standard placeholders as updating keys. Really very dynamic... and important to realize its power.

You should therefore take note that your component placeholders can overwrite these, and use these at the same time ;) Looking at [the class](https://git.vdm.dev/joomla/Component-Builder/src/commit/f5f308dbf2ca4021f581a1f57ad88e7d1ed1f62b/libraries/jcb_powers/VDM.Joomla/src/Componentbuilder/Compiler/Component/Placeholder.php#L140) you will see it loads the standard placeholders and then load the component placeholders, while using the standard placeholders as updating keys. Really very dynamic... and important to realize its power.
Author
Member

...ok, I missed beta-6, going to update now. Thank you :)

...ok, I missed beta-6, going to update now. Thank you :)
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: joomla/Component-Builder#1073
No description provided.