Stylesheet Use - Frontend Component

115 views
Skip to first unread message

Glenn Arkell

unread,
Feb 20, 2012, 2:50:12 AM2/20/12
to Joomla! General Development
Hi team,

I know I'm going to be embarrassed when the answer comes back, but
here goes...

My stylesheet is not being included in my display page of my small
component and I can't see the woods for the trees.

........ view.html.php ..........

defined('_JEXEC') or die;
jimport('joomla.application.component.view');
jimport('joomla.html.html');


class MyComponentViewMyClass extends JView
{
protected $state;
protected $item;

function display($tpl = null) {
$app = JFactory::getApplication();
$params = $app->getParams();
$state = $this->get('State');
$item = $this->get('Item');

JHtml::stylesheet('com_mycomponent/css/site.stylesheet.css',
array(), true, false, false);
parent::display($tpl);

}
}

My stylesheet file "site.stylesheet.css" is in the "joomlainstallation/
media/com_mycomponent/css/" folder.

From my reading of the stylesheet API doco I think I have it right but
obviously have missed something. Any advice/guidance gratefully
received. Cheers.

Glenn
Joomla!2.5.1

JM Simonet

unread,
Feb 20, 2012, 4:19:46 AM2/20/12
to joomla-de...@googlegroups.com
Maybe adding it in the tpl itself as
JHtml::_('stylesheet', 'com_mycomponent/css/site.stylesheet.css',
array(), true);

JM

>--
>You received this message because you are subscribed to the Google
>Groups "Joomla! General Development" group.
>To post to this group, send an email to joomla-de...@googlegroups.com.
>To unsubscribe from this group, send email to
>joomla-dev-gene...@googlegroups.com.
>For more options, visit this group at
>http://groups.google.com/group/joomla-dev-general?hl=en-GB.


--
>Please keep the Subject wording in your answers
This e-mail and any attachments may be confidential. You must not
disclose or use the information contained in this e-mail if you are
not the
intended recipient. If you have received this e-mail in error, please
notify us immediately and delete the e-mail and all copies.
-----------------------------------------------------------
Jean-Marie Simonet / infograf768
Joomla Production Working group
Joomla! Translation Coordination Team

Sam Coult

unread,
Feb 20, 2012, 10:34:03 AM2/20/12
to joomla-de...@googlegroups.com
In your MyComponentViewMyClass class you should use the following :

$doc =& JFactory::getDocument();
$doc->addStyleSheet('media/com_mycomponent/css/site.stylesheet.css');

rather than:


JHtml::stylesheet('com_mycomponent/css/site.stylesheet.css', array(), true, false, false);

Hope that helps.

Glenn Arkell

unread,
Feb 20, 2012, 11:46:58 PM2/20/12
to Joomla! General Development
Ok, the following results from the suggestions provided.

@JM - sorry but your suggestion didn't work for me, but thank you
anyway. Cheers.

@ Sam - your suggestion worked a treat, but I don't understand why.
I'm not referring to $doc anywhere else in my component... is this one
of those magical elements that Joomla looks for and defaults if
nothing found? Is this something I've missed in the doco - any
pointers appreciated? But thank you for the solution to my problem.
Cheers.

Glenn

Sam Coult

unread,
Feb 21, 2012, 1:39:23 PM2/21/12
to joomla-de...@googlegroups.com
The line: $doc =& JFactory::getDocument(); gets the global document object (your page) and the addStylesheet function adds a stylesheet to the head. 
You can also use $doc to get and set metadata and to add scripts to the head amongst other things. For more info look here and here.

Rouven Weßling

unread,
Feb 21, 2012, 2:08:20 PM2/21/12
to joomla-de...@googlegroups.com
Small hint: you don't need the & in PHP5 anymore so you save that bit. Also the advantage of JHtml is that templates can override those files, in case you don't need that functionality JDocument::addStylesheet() is just as good.

Rouven

On 21.02.2012, at 19:39, Sam Coult wrote:

The line: $doc =& JFactory::getDocument(); gets the global document object (your page) and the addStylesheet function adds a stylesheet to the head. 
You can also use $doc to get and set metadata and to add scripts to the head amongst other things. For more info look here and here.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/UaHM8BXnk-QJ.

Glenn Arkell

unread,
Feb 21, 2012, 3:48:30 PM2/21/12
to Joomla! General Development
@ Sam - Thanks for the pointers, more reading required. Appreciate
you help. Cheers.

@ Rouven - Yes I thought the & wasn't necessary, but I definitely need
to read more around this area. Thanks again. Cheers.

Glenn

On Feb 22, 6:08 am, Rouven Weßling <m...@rouvenwessling.de> wrote:
> Small hint: you don't need the & in PHP5 anymore so you save that bit. Also the advantage of JHtml is that templates can override those files, in case you don't need that functionality JDocument::addStylesheet() is just as good.
>
> Rouven
>
> On 21.02.2012, at 19:39, Sam Coult wrote:
>
>
>
>
>
>
>
> > The line: $doc =& JFactory::getDocument(); gets the global document object (your page) and the addStylesheet function adds a stylesheet to the head.
> > You can also use $doc to get and set metadata and to add scripts to the head amongst other things. For more info look here and here.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
> > To view this discussion on the web, visithttps://groups.google.com/d/msg/joomla-dev-general/-/UaHM8BXnk-QJ.
Reply all
Reply to author
Forward
0 new messages