5

I am developing a shortcodes plugin that searches trough Joomla content and creates html outputs from the matched string.

Currently I am using this

   public function onContentPrepare($context, &$row, &$params, $page = 0){

        if ($this->run_plg != 1)return;
        require_once 'shortcodes.php';
        $row->text = shortcodes($row->text);

        return true;
    }

And it works very well. As you can see this runs trough every Joomla content basically , including modules ( Not sure about Virtuemart or some other 3rd party extensions ) . But if you were to use the shortcode in your template files it would not find it.

So I am thinking on placing the function in onAfterRender like this:

function onAfterRender() {
    require_once 'shortcodes.php';
    $body        = JResponse::getBody();
    $body       = shortcodes($body);
    JResponse::setBody($body);
}

This would match the string anywhere on the site. My concern is the performance. Here is simple example of shortcodes function.

https://eval.in/141437

the original one is refined with PREG_SET_ORDER and runs only if the string from $shortcode['type'] is found.

So the question is , what would you recommend and why if you have time to explain.

Thank you and CONGRATS :)

4
  • 6
    Just fyi JResponse::setBody is deprecated as of Joomla 3.2 and replaced with JFactory::getApplication()->setBody() Apr 25, 2014 at 18:07
  • Still works on 3.3dev beta. Just tested
    – Dan
    Apr 25, 2014 at 18:23
  • 2
    Of course it works - we can't break b/c in the 3.x series - it is deprecated to be removed in favour of the other method. In fact JResponse::setBody just uses the JApplicationWeb alternative anyhow (see github.com/joomla/joomla-cms/blob/staging/libraries/legacy/…) but the JResponse will work until the end of 3.x Apr 25, 2014 at 20:58
  • NOTE: JFactory::getApplication()->getBody() / setBody() do not work on Joomla! v3.0.3.
    – Anibal
    May 12, 2014 at 13:58

2 Answers 2

1

As with my other answer on plugin performance, the best way to find out is to test.

Take a look at the other answer for details, but it should be easy to add time logging statements using the JLog class to compare the timing results of each method.

That said, if you need to catch the shortcode everywhere on the site then onAfterRender is the quick, easy and perhaps only way (using plugins).

5
  • I thank you for suggestion but to be honest , those docs are no where near informational or code ready. Especially in J2.5, how ridiculous is this please prntscr.com/3dbk3n,( no info where the getErrorMsg is being used) and than I found over 400 instances of getErrorMsg in Joomla 2.5.19 core itself. Creating timer on my own is not a problem , but this is StackExchange and not Joomla forum so code examples and explanations as to why, are desired over , "test it", especially if OP has set the ground work and asked what the recommended use is.
    – Dan
    Apr 25, 2014 at 16:12
  • You've already written the code. You just need to add JLog statements to it. If you would like code examples for how to use JLog, please ask another question. This question is about which trigger to use for rendering content, NOT how to use a logger. Apr 25, 2014 at 16:14
  • Never asked for JLog code sample, I asked for prntscr.com/3dbkzk Dont get mad now. Was not intention to upset you. But the question is why is one preferred over the other and why.
    – Dan
    Apr 25, 2014 at 16:16
  • Perhaps you could compare the 2 options in practice and find out that way? Both approaches are technically valid. As much of the Joomla codebase isn't particularly well documented it would be interesting to see if there's any difference. Apologies if I sounded mad, not my intention :) Apr 25, 2014 at 16:18
  • We're working on removing all instances of JError for Joomla 4 - unfortunately we can't do it during major versions because of b/c - people can't handle both exceptions and JError's. So whilst they flag deprecated it is more as advance warning for people in the logs. Apr 25, 2014 at 18:06
1

onContentPrepare is more or less build for the output of extensions. It is my opinion that there should be no where else a find/replace on load should be added. The reason I say this is to make sure that Joomla stays consistent with its methods.

So if some sort of find/replace is needed it can be best to put it in a custom HTML module instead (you can also create a custom layout for it and such). This way content plugins stay parsing your content, leaving your system plugins to adjust environment settings and such, or load extra libraries (basically, adjust Joomla's core and extend it).

Look into the nonumber extensions, mainly the Advanced module manager, it lets you target modules with many more settings. While it might not help you, the control they give can lead to alternate solutions you would not think of otherwise. http://www.nonumber.nl/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.