General question about use statements in JCB templates #1073
Labels
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: joomla/Component-Builder#1073
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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)
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?
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:
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:
Should then becomes in the JCB GUI:
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.
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.
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[
.Here you can see the commit for that change two days ago... so its new :)
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.
...ok, I missed beta-6, going to update now. Thank you :)