Skip to content

Instantly share code, notes, and snippets.

@schmidt1024
Last active December 14, 2021 19:52
Show Gist options
  • Save schmidt1024/2360208 to your computer and use it in GitHub Desktop.
Save schmidt1024/2360208 to your computer and use it in GitHub Desktop.
Template Snippets for Joomla!
# all php files :: disallow direct access of file
# between <?php and ?>
defined('_JEXEC') or die;
# index.php :: define variable with application
# between <?php and ?>
$app = JFactory::getApplication();
# index.php :: define variable with document
# between <?php and ?>
$doc = JFactory::getDocument();
# index.php :: define variable with template path
# between <?php and ?>
$tpath = $this->baseurl.'/templates/'.$this->template;
# index.php :: load cascading style sheet throught API
# between <?php and ?>
$doc->addStyleSheet($tpath.'/css/template.css');
# index.php :: unset css file from head
# between <?php and ?>
unset($doc->_styleSheets[$this->baseurl.'/media/zoo/assets/css/reset.css']);
# index.php :: add custom css
# between <?php and ?>
$doc->addStyleDeclaration('body{background-color:red;}');
# index.php :: load javascript file
# between <?php and ?>
$doc->addScript($tpath.'/js/modernizr.js');
# index.php :: unset javascript file from head
# between <?php and ?>
unset($doc->_scripts[$this->baseurl.'/media/jui/js/bootstrap.min.js']);
# index.php :: quickest way to unset all javascript file from head
# between <?php and ?>
$doc->_scripts = array();
# index.php :: add custom javascript
# between <?php and ?>
$doc->addScriptDeclaration('alert(\'Hello Joomla!\')');
# index.php :: quickest way to unset plain javascript from head
# between <?php and ?>
$doc->_script = array();
# index.php :: add custom tag
# between <?php and ?>
$doc->addCustomTag('<!-- What a beautiful comment! -->');
# index.php :: load the global joomla header
# between <head> and </head>
<jdoc:include type="head" />
# index.php :: system messages output incl. prevention message box (div tag)
# needs variable $app
# between <body> and </body>
<?php if (!empty($app->getMessageQueue)) : ?>
<jdoc:include type="message" />
<?php endif ?>
# index.php :: article and component output
# between <body> and </body>
<jdoc:include type="component" />
# index.php :: module output of "menu", which have to be mentioned in templateDetails.xml
# between <body> and </body>
<jdoc:include type="modules" name="menu" style="xhtml" />
# templateDetails.xml :: creation of a new module position called "menu";
# between <positions> and </positions>
<position>menu</position>
# index.php :: insert debug module; important for developers
# before </body>
<jdoc:include type="modules" name="debug" />
# index.php :: output of the base url
# between <?php and ?>
echo $this->baseurl;
# index.php :: output of sitename
# need variable $app
# between <?php and ?>
echo $app->getCfg('sitename');
# index.php :: if construct; true, if module is published on position menu
# between <?php and ?>
if ($this->countModules('menu')) :
# index.php :: if construct; true, if http user agent is mobile device
# between <?php and ?>
if(preg_match('/iphone|ipod|opera mini|blackberry|mobile|iemobile/i', $_SERVER['HTTP_USER_AGENT'])) :
# index.php :: extend the if statement from above to execute another statement
# between <?php and ?>
else :
# index.php :: closing the if statement
# between <?php and ?>
endif;
# index.php :: short write if, then, else for echo a class
# between <?php and ?>
echo ($this->countModules('menu')) ? ('class') : ('no-class');
# index.php :: disappear the generator tag
# between <?php and ?>
$this->setGenerator(null);
# index.php :: get params (line 1) and
# get the page class from the current menu entry (line 2)
# needs variable $app
# between <?php and ?>
$params = $app->getParams();
$pageclass = $params->get('pageclass_sfx');
# index.php :: load the modal behavior
# between <?php and ?>
JHTML::_('behavior.modal');
# index.php :: activate modal window
# between <html> and </html> or in articles and modules
<a class="modal"></a>
# index.php :: insert translatable text
# between <?php and ?>
echo JText::_('EXAMPLE_TEXT');
# en-GB.tpl_templatename.ini
# new row
EXAMPLE_TEXT="What a beautiful text."
# index.php :: display short system language tag; e.g. en-gb
# between <?php and ?>
echo $this->language;
@christianhent
Copy link

hi. eregi() ist seit PHP 5.3.0 depraced, nimm preg_match

@schmidt1024
Copy link
Author

Dankeschön. erregi wurde durch preg_match mit der flag i ersetzt.

@betweenbrain
Copy link

Have you considered wrapping <jdoc:include type="message" /> in a PHP conditional so that <div id="system-message-container"> isn't always displayed?

@schmidt1024
Copy link
Author

I don't know, if I understand it right, but I mention in the comments.

@betweenbrain
Copy link

Sorry. I just realized that part of my comment was not being displayed due the way markdown syntax is rendered. I have revised it.

@betweenbrain
Copy link

For example, with Construct I define $messageQueue = $app->getMessageQueue(); and then display the message with
<?php if (!empty($messageQueue)) : ?>
<jdoc:include type="message" />
<?php endif ?>

You can see it at https://github.com/construct-framework/construct5/blob/master/elements/logic.php#L20 and at https://github.com/construct-framework/construct5/blob/master/index.php#L270

@schmidt1024
Copy link
Author

Great! Thank you. I will implement your code, if it is okay for you.

@betweenbrain
Copy link

Yes. By all means, feel free to implement it. I also need to mention that I have defined $app = JFactory::getApplication(); since $messageQueue uses $app.

Hope that helps!

@JorisLange
Copy link

The conditional system message div
getMessageQueue)) : ?>

has to be
getMessageQueue())) : ?>
I think.

Or
getMessageQueue())) : ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment