[英]How can i add inline script in joomla 3?
我正在嘗試在teplate中添加腳本,但它通常會出現錯誤:
解析錯誤:語法錯誤,第21行的Z:\\ home \\ localhost \\ www \\ RealEstate \\ templates \\ real_estate \\ index.php中出現意外的T_CONSTANT_ENCAPSED_STRING
這是我的代碼:
<?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文本的行為就像帶雙引號的字符串,沒有雙引號。 這意味着heredoc中的引號不需要轉義,但是上面列出的轉義代碼仍然可以使用。 變量被擴展,但是在heredoc中使用字符串表示復雜變量時必須同樣小心。 結束標識符必須在該行的第一列中開始。 另外,標識符必須遵循與PHP中其他任何標簽相同的命名規則:它必須僅包含字母數字字符和下划線,並且必須以非數字字符或下划線開頭。
警告請務必注意,帶有結束標識符的行除分號(;)外,不得包含其他任何字符。 特別是這意味着標識符可能不會縮進,並且在分號之前或之后可能沒有任何空格或制表符。 同樣重要的是要認識到,結束標識符之前的第一個字符必須是本地操作系統定義的換行符。 在包括Mac OS X在內的UNIX系統上,這是\\ n。結束定界符也必須后跟換行符。
如果該規則被破壞,並且結束標識符不是“干凈的”,它將不會被視為結束標識符,PHP將繼續尋找一個。 如果在當前文件末尾之前找不到合適的結束標識符,則將在最后一行產生解析錯誤。
Heredocs不能用於初始化類屬性。 從PHP 5.3開始,此限制僅對包含變量的heredocs有效。
參考: Heredoc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.