I have



and then the layout intro_image.php

I wanted to count the number of blog posts, which is already being done in leading and intro articles in the blog.

But how can I pass that variable to either blog_item.php or to the layout intro_image.php.

I see that there is $displayData in the layout file, so perhaps I can pass the value to that object somehow so I can retrieve it?

I know I can get things how can I set things?

One of the reasons for wanting to know this is because in the foreach loop there is a $counter++ and if I know what is the current number I can apply a bit of code to every 3rd item.


1 Answer 1


There are some different ways to get the number of articles (I think that is what you mean under blog items), I'll just give you one example.

First, you have to access the article list model:

You can do that the usual way like (I have not checked this in Joomla 3.9):

$model = JModelLegacy::getInstance('Articles', 'ContentModel', array( 'ignore_request' => true ));

Or you can do that with using newer classes like this (I use this in Joomla 3.9):

// on the top of your layout file:

defined('_JEXEC') or die;
use \Joomla\CMS\MVC\Model\BaseDatabaseModel;

BaseDatabaseModel::addIncludePath('components/com_content/models', 'ContentModel');

$model = BaseDatabaseModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
// var_dump($model); // just to check it
$numberOfItems = count($model->getItems());

echo "This is the total of items: " . $numberOfItems . "<br>";

When you have the list model then you just use the getItems() method of that and count the items.

And the reason I’m a bit avoiding your question about how to pass a variable among layouts and views is because those variables’ values are coming from the models too (created in the views), thus you can get and create them by accessing the models directly if you need them.


If you need for example the number of leading articles or the number of intro articles only, then since these are set in com_content parameters thus you can reach those values from anywhere in Joomla and get those data directly from the content params (registry object), using the component helper:

$content_params = JComponentHelper::getParams('com_content');
// var_dump($content_params); // you can check all of the available params of com_content
$number_leading = $content_params->get('num_leading_articles');
$number_intro = $content_params->get('num_intro_articles');
echo "number of leading articles: " . $number_leading . "<br>";
echo "number of intro articles: " . $number_intro . "<br>";

The other part of your question on this like: what if there are not enough articles for intro then the number from the set parameters would be incorrect. I do not fully understand that part of your question. I do not see what exactly you want to accomplish to figure out a solution on that.


As the particular variable needed was cleared more in the comments then the use of content params is still a viable option to pass the foreach loop counter value for each items from blog.php to blog_item.php.

in blog.php you call and define the content params (like I already did above):

$content_params = JComponentHelper::getParams('com_content');

And within the lead items' foreach loop you set a variable (before the code line: echo $this->loadTemplate('item');)

$content_params->set('lead_count_number', $leadingcount + 1);

And within the intro items' foreach loop you set a variable (before the code line: echo $this->loadTemplate('item');)

$content_params->set('intro_count_number', $counter + 1);

This way you can call and check this param value item by item if needed in blog_item.php:

$content_params = JComponentHelper::getParams('com_content');
$introCounterNumb = $content_params['intro_count_number'];
$leadCounterNumb = $content_params['lead_count_number'];
if (isset($introCounterNumb)){
    echo "this is the intro item's number: " . $introCounterNumb; // or do whatever
if (isset($leadCounterNumb)) {
    echo "this is the lead item's number: " . $leadCounterNumb;


you can avoid using content params if you just simply define a variable in blog.php inside both foreach loops like:

$this->lead_item_count = $leadingcount + 1;
$this->intro_item_count = $counter + 1;

And then you can call these variables in the item_blog layout (or any layout which belongs to this view class.

(The above is tried and works)

Anyway, I hope this will help you.

  • 1
    @Eoin - if you mean under passing a variable from a list view layout to an item view layout, and that variable is a foreach loop counter value in the list view layout, then I’d try to solve that with javascript/jQuery for example, since, I would say it is a front-end variable value which is dynamically created in the list view layout file at loading. When you click on an item in a list view layout, the item view layout is getting it’s variables/values from an item view file, and that item layout does not have connection to the list view layout basically, and as I understand that is your issue.
    – Zollie
    Nov 18, 2019 at 6:31
  • 1
    @Eoin - However you can still “echo out” the counter value to every items into a hidden <div> in the list view layout (blog.php) and from that div value you can always see and use that value with jQuery for example in item view layouts (blog_item.php or intro_image.php layout or any other item layout).
    – Zollie
    Nov 18, 2019 at 6:34
  • 1
    @Eoin - that is how I usually solve this and I see that this is how some other developers usually solve this, since this is a template level logic not a model/view level logic and data.
    – Zollie
    Nov 18, 2019 at 6:43
  • 1
    @Eoin - I updated with a possible solution using the content params registry object. This is also a workable solution for now.
    – Zollie
    Nov 19, 2019 at 13:38
  • 1
    @Eoin - it is not easy to tell you without seeing exactly your task but I think you want to use $this->item object and in this object you should use $this->item->images->image_fulltext which is available before any additional layout is rendered for an item. In a rendered JLayout file $this->item becomes $displayData object but elsewhere you should be able to use $this->item. If we are talking about an article item.
    – Zollie
    Apr 30, 2020 at 8:53

Your Answer

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

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