我如何在 joomla 3 中添加内联脚本?

How can i add inline script in joomla 3?

我正在尝试在我的模板中添加脚本,但它经常会出错:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in Z:\home\localhost\www\RealEstate\templates\real_estate\index.php on line 21

这是我的代码:

<?php

defined('_JEXEC') or die;

$app             = JFactory::getApplication();
$doc             = JFactory::getDocument();
$user            = JFactory::getUser();
$this->language  = $doc->language;
$this->direction = $doc->direction;

$params = $app->getTemplate(true)->params;


$doc->addStyleSheet('templates/' . $this->template . '/css/swiper.min.css');
$doc->addStyleSheet('templates/' . $this->template . '/css/style.css');

JHtml::_('jquery.framework');
$doc->addScript('templates/' . $this->template . '/js/swiper.min.js');
$doc->addScriptDeclaration('
  jQuery(document).ready(function() {
   var swiper = new Swiper('.swiper-container', {
   nextButton: '.swiper-button-next',
   prevButton: '.swiper-button-prev',
   pagination: '.swiper-pagination',
   slidesPerView: 3,
   slidesPerColumn: 2,
   paginationClickable: true,
   spaceBetween: -15,
   });
});
');

?>

我也在尝试这个:

JHtml::_('jquery.framework', false);
$doc->addScript('templates/' . $this->template . '/js/swiper.min.js');
$doc->addScriptDeclaration('
  $(document).ready(function() {
   var swiper = new Swiper('.swiper-container', {
   nextButton: '.swiper-button-next',
   prevButton: '.swiper-button-prev',
   pagination: '.swiper-pagination',
   slidesPerView: 3,
   slidesPerColumn: 2,
   paginationClickable: true,
   spaceBetween: -15,
   });
});
');

两者都不起作用,但会出错。 请大家指出我做错了什么。

您以错误的方式连接字符串

将该脚本放在双引号内

$doc->addScriptDeclaration("
  jQuery(document).ready(function() {
   var swiper = new Swiper('.swiper-container', {
   nextButton: '.swiper-button-next',
   prevButton: '.swiper-button-prev',
   pagination: '.swiper-pagination',
   slidesPerView: 3,
   slidesPerColumn: 2,
   paginationClickable: true,
   spaceBetween: -15,
   });
});
");
Try:
$doc->addScriptDeclaration('
  jQuery(document).ready(function() {
   var swiper = new Swiper(\'.swiper-container\', {
   nextButton: \'.swiper-button-next\',
   prevButton: \'.swiper-button-prev\',
   pagination: \'.swiper-pagination\',
   slidesPerView: 3,
   slidesPerColumn: 2,
   paginationClickable: true,
   spaceBetween: -15,
   });
});
');

无需转义引号即可分隔字符串的另一种方法是 Heredoc 语法。

$doc->addScriptDeclaration(<<<JS_SCRIPT
  $(document).ready(function() {
   var swiper = new Swiper('.swiper-container', {
       nextButton: '.swiper-button-next',
       prevButton: '.swiper-button-prev',
       pagination: '.swiper-pagination',
       slidesPerView: 3,
       slidesPerColumn: 2,
       paginationClickable: true,
       spaceBetween: -15,
   });
  });
JS_SCRIPT
);

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings. The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

参考:Heredoc