Using Shortcode code from minicomponents

In this article I am going to discuss how you as a developer can use the code in minicomponents to help you to call discreet Jomres scripts.

This information is helpful to you if you want to include code from Jomres into other scripts, or just if you're building your own minicomponent.

The article actually addresses two distinct scenarios, the first is how to access a minicomponent you've written yourself, and the second is how to include rendered snippets from a minicomponent into your own code. As a developer you are probably more interested in the second part, but I recommend you read the first part so that you can understand how things are interconnected.

Getting started with the code

Let's dive right in.

These examples are taken from the Show Property Reviews script on Github

https://github.com/WoollyinWalesIT/jomres/blob/master/core-minicomponents/j06000show_property_reviews.class.php

 

This is what you will see at the top of the script.

$this->shortcode_data = array(
'task' => 'show_property_reviews',
'info' => '_JOMRES_SHORTCODES_06000SHOW_PROPERTY_REVIEWS',
'arguments' => array(
array(
'argument' => 'property_uid',
'arg_info' => '_JOMRES_SHORTCODES_06000SHOW_PROPERTY_REVIEWS_ARG_PROPERTY_UID',
'arg_example' => '1',
),
array(
'argument' => 'reviews_limit',
'arg_info' => '_JOMRES_SHORTCODES_06000SHOW_PROPERTY_REVIEWS_LIMIT',
'arg_example' => '3',
),
),
);

 

Before I continue, I should point out that one argument that's always missing from this list of options is "output_now". Most minicomponents that output stuff to the page have it, so in general you can assume that it's available but if not you should check out the minicomponent script itself to be sure. Note, this is only relevant if you are calling one script from another. If you are calling the script via the url (more on that in a moment) you can assume that the output will be immediate.

I should also point out that the shortcode_data at the top of the scripts isn't used by the script itself in any way. It's there solely to allow the Admin > Jomres > Tools > Shortcodes page to generate it's output. It is, however, useful to you as a developer. You don't need to refer to each script to see this information.

The Shortcodes page in the administrator area will use this information to build a shortcode suggestion like this : [jomres task="show_property_reviews" params="&property_uid=1&reviews_limit=3"]

Calling via the url

If you are developing your own minicomponent and you want to test it to see how it looks, you could go through the process of adding scripts to menus, but often that's not needed. Fortunately, you don't need to do that.

You can call 06XXXX minicomponents directly from the url.

Firstly, please remember that if you've built a new script that looks something like j06000xxxxx.class.php (or 06100/06200/06500) then don't forget to rebuild the registry once you have created it. That tells Jomres that the script exists. Once you've done that you can do the next step.

Let's assume that the site I'm working on generates urls that look like http://localhost/Joomla_4/index.php?option=com_jomres&view=default&Itemid=103 or http://localhost/wordpress-5.9.3/index.php/bookings/?option=com_jomres& (Joomla and Wordpress respectively) when I'm on a Jomres page. I'll refer to these urls as {URL} from now on.

If I want to call the Show Property Reviews page directly from the address bar of the browser, I can by putting together the information from the shortcode to create a url that looks like

{URL}&task=show_property_reviews&property_uid=1&reviews_limit=3

If I put that into the address bar of the browser, then the property reviews page will show.

You might be thinking "So what? I can get that from the page itself." and you'd be right. What I'm doing here is demonstrating to you how to use information in ways that might not be immediately obvious to you.

 

Pulling rendered snippets in your own code

Moving on, let's assume that you are a developer who wants to include Jomres output in his or her own page.This saves you bucketloads of time because you don't want to re-invent the wheel. I've already done that for you, all you need to do is work your own magic.

So, instead of telling you to dig into the code of such a large system as Jomres to see how to do it, I'll use the shortcode as a shortcut to help you out.

Remember, the shortcode for property reviews looks like this : [jomres task="show_property_reviews" params="&property_uid=1&reviews_limit=3"]

Now, as the developer you will first need to pull in the Jomres framework so that you can use what's on offer. That is described in the Using the Jomres Framework article. Once you have access to the Jomres framework you can then call a script like this :

$MiniComponents = jomres_singleton_abstract::getInstance('mcHandler');

$reviews
= $MiniComponents->specificEvent('06000', 'show_property_reviews', array('output_now' => false, 'property_uid' => $property_uid));

Notice how the argument output_now is set to false? This tells the script to return the rendered template inside of showing it.

Now that you have this wonderful nugget of information, you have learned that you can do the same with other shortcodes.

Extrapolation

Take a look at the Property features shortcode

[jomres task="show_property_features" params="&property_uid=1"]

To include that code in your own script you would do this :

$features = $MiniComponents->specificEvent('06000', 'show_property_features', array('output_now' => false, 'property_uid' => $property_uid));

Another example :

[jomres task="show_property_slideshow" params="&property_uid=1"]

$slideshow = $MiniComponents->specificEvent('06000', 'show_property_slideshow', array('output_now' => false, 'property_uid' => $property_uid));

Conclusion

So, here I have shown you ways that you can use the Jomres framework in ways that you may not have been previously aware of. Hope it helps.