Здравствуйте! Очень поверхностно знаком с функциями. Есть такой код для вывода комментариев. PHP: <?php /** * JComments - Joomla Comment System * * @version 3.0 * @package JComments * @author Sergey M. Litvinov (smart@joomlatune.ru) * @copyright (C) 2006-2013 by Sergey M. Litvinov (http://www.joomlatune.ru) * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html */ defined('_JEXEC') or die; /** * Threaded comments list template * */ class jtt_tpl_tree extends JoomlaTuneTemplate { function render() { $comments = $this->getVar('comments-items'); if (isset($comments)) { $this->getHeader(); ?> <div class="comments-list" id="comments-list-0"> <?php $i = 0; $count = count($comments); $currentLevel = 0; foreach($comments as $id => $comment) { if ($currentLevel < $comment->level) { ?> </div> <div class="comments-list" id="comments-list-<?php echo $comment->parent; ?>"> <?php } else { $j = 0; if ($currentLevel >= $comment->level) { $j = $currentLevel - $comment->level; } else if ($comment->level > 0 && $i == $count - 1) { $j = $comment->level; } while($j > 0) { ?> </div> <?php $j--; } } ?> <div class="<?php echo ($i%2 ? 'odd' : 'even'); ?>" id="comment-item-<?php echo $id; ?>"> <?php echo $comment->html; if ($comment->children == 0) { ?> </div> <?php } if ($comment->level > 0 && $i == $count - 1) { $j = $comment->level; } while($j > 0) { ?> </div> <?php $j--; } $i++; $currentLevel = $comment->level; } ?> </div> <div id="comments-list-footer"><?php echo $this->getFooter();?></div> <?php } else { // display single comment item (works when new comment is added) $comment = $this->getVar('comment-item'); if (isset($comment)) { $i = $this->getVar('comment-modulo'); $id = $this->getVar('comment-id'); ?> <div class="<?php echo ($i%2 ? 'odd' : 'even'); ?>" id="comment-item-<?php echo $id; ?>"><?php echo $comment; ?></div> <?php } else { ?> <div class="comments-list" id="comments-list-0"></div> <?php } } } /* * * Display comments header and small buttons: rss and refresh * */ function getHeader() { $object_id = $this->getVar('comment-object_id'); $object_group = $this->getVar('comment-object_group'); $btnRSS = ''; $btnRefresh = ''; if ($this->getVar('comments-refresh', 1) == 1) { $btnRefresh = '<a class="refresh" href="#" title="'.JText::_('BUTTON_REFRESH').'" onclick="jcomments.showPage('.$object_id.',\''. $object_group . '\',0);return false;"> </a>'; } if ($this->getVar('comments-rss') == 1) { $link = $this->getVar('rssurl'); if (!empty($link)) { $btnRSS = '<a class="rss" href="'.$link.'" title="'.JText::_('BUTTON_RSS').'" target="_blank"> </a>'; } } ?> <h4 id="comments"><?php echo JText::_('COMMENTS_LIST_HEADER');?> <?php echo $btnRSS; ?><?php echo $btnRefresh; ?></h4> <?php } /* * * Display RSS feed and/or Refresh buttons after comments list * */ function getFooter() { $footer = ''; $object_id = $this->getVar('comment-object_id'); $object_group = $this->getVar('comment-object_group'); $lines = array(); if ($this->getVar('comments-refresh', 1) == 1) { $lines[] = '<a class="refresh" href="#" title="'.JText::_('BUTTON_REFRESH').'" onclick="jcomments.showPage('.$object_id.',\''. $object_group . '\',0);return false;">'.JText::_('BUTTON_REFRESH').'</a>'; } if ($this->getVar('comments-rss', 1) == 1) { $link = $this->getVar('rssurl'); if (!empty($link)) { $lines[] = '<a class="rss" href="'.$link.'" title="'.JText::_('BUTTON_RSS').'" target="_blank">'.JText::_('BUTTON_RSS').'</a>'; } } if ($this->getVar('comments-can-subscribe', 0) == 1) { $isSubscribed = $this->getVar('comments-user-subscribed', 0); $text = $isSubscribed ? JText::_('BUTTON_UNSUBSCRIBE') : JText::_('BUTTON_SUBSCRIBE'); $func = $isSubscribed ? 'unsubscribe' : 'subscribe'; $lines[] = '<a id="comments-subscription" class="subscribe" href="#" title="' . $text . '" onclick="jcomments.' . $func . '('.$object_id.',\''. $object_group . '\');return false;">'. $text .'</a>'; } if (count($lines)) { $footer = implode('<br />', $lines); } return $footer; } } В функции render есть переменная $count. Если добавить вывод содержимого переменного прямо в функции (фргамент кода) PHP: <?php $i = 0; $count = count($comments); $currentLevel = 0; echo $count; foreach($comments as $id => $comment) { if ($currentLevel < $comment->level) { ?> То значение выводится, но не в том месте, где это нужно. Нужно, что бы оно выводилось в заголовке h4 HTML: <h4 id="comments"><?php echo JText::_('COMMENTS_LIST_HEADER').' '.$count;?> <?php echo $btnRSS; ?><?php echo $btnRefresh; ?></h4> Но ничего выводится. Объясните пожалуйста, как передать туда значение переменной. Спасибо.
Не функция, а метод класса --- Добавлено --- https://php.ru/manual/language.oop5.visibility.html https://php.ru/manual/language.variables.scope.html
Судя по второму мануалу, нужно использовать global так и делаю PHP: class jtt_tpl_tree extends JoomlaTuneTemplate { function render() { global $count, $comments; $comments = $this->getVar('comments-items'); if (isset($comments)) { $this->getHeader(); ?> <div class="comments-list" id="comments-list-0"> <?php $i = 0; $count = count($comments); $currentLevel = 0; foreach($comments as $id => $comment) { if ($currentLevel < $comment->level) { ?> Не выводит...
Голову включать и гуглить http://forum.joomla.org/viewtopic.php?p=2460747 --- Добавлено --- Догадаешься или просто скопируешь все? --- Добавлено --- Задачу опиши, может не правильно досказал) Тебе количество комментариев нужно вывести в h4?
Я видел этот вариант Дело в ом, что я вывожу не в стороннем компоненте, а самом шаблоне Jcomments. Я не понимаю, как там получить PHP: $count = JComments::getCommentsCount($this->article->id, 'com_content'); В моем случае из компонента easyblog пробую так PHP: <?php $commentsAPI = JPATH_SITE . '/components/com_jcomments/jcomments.php'; if (file_exists($commentsAPI)) { require_once($commentsAPI); $count = JComments::getCommentsCount($this->post->id, 'com_easyblog'); echo '('. $count . ')'; } Выводит общее количество комментариев, а не из текущего материала. И потом, мне казалось, что в самом шаблоне можно получить количество просто из переменной. --- Добавлено --- Ок, получилось так. PHP: <?php $commentsAPI = JPATH_SITE . '/components/com_jcomments/jcomments.php'; $object_id = $this->getVar('comment-object_id'); if (file_exists($commentsAPI)) { require_once($commentsAPI); $count = JComments::getCommentsCount($object_id, 'com_easyblog'); echo '('. $count . ')'; } ?> Но мне казалось, это очень громоздко. Если в этом же файле можно просто переменную получить...
Ну ибать... либо в $this->getHeader(); передай значение $comments либо в getHeader() вызови count($this->getVar('comments-items')) И ради этого ты потратил целое утро...
в getHeader() вызвал count($this->getVar('comments-items')) Я же сразу написал - нубер. Работает и красиво. Спасибо!