簡體   English   中英

Joomla-如何使用用戶在模板參數中提供的信息從頭部取消JS文件

[英]Joomla - how to Unset JS file from head using info given by user in template parameters

我已經使用下面的常用方法成功地從Joomla模板HTML頭手動取消了JS和CSS文件的設置。

unset($this->_scripts['/media/jui/js/bootstrap.min.js']);
unset($this->_stylesheets['/media/jui/js/bootstrap.min.css']);

我的問題是這個。 是否可以將文件和路徑信息存儲到變量中,然后在上述示例中使用變量代替路徑/文件? 如果是這樣,什么是實現此目的的好方法?

原因? 我想讓我的模板用戶能夠在模板參數中輸入多個用逗號分隔的文件/路徑,將這些各種文件/路徑存儲到一個數組中,然后遍歷該數組(從HTML頭中取消設置/刪除每個文件/路徑)模板)。

目的? 有時候,您需要從html頭和頁面底部刪除js,以減少頁面加載時間,頁面過渡閃爍/閃爍白色等。在這種情況下,我不想使用插件。 如果可能的話,我寧願給我的用戶一些細化的控件。 當然,我將在模板參數中放置一個位置,以供用戶添加從標題到底部刪除的所有js。

我正在使用的代碼?

// grab the js file paths user enters into template parameters and add them to an array i can loop through

$unsetJSFiles = $this->params->get( 'customFilesUnsetJS' );
$unsetJSFilesArray = explode(',', $unsetJSFiles);

// loop through each js file path user entered and unset each one from joomla html head  

foreach ($unsetJSFilesArray as $valueJS) {
    $doc = JFactory::getDocument();
    unset($doc->_scripts[$valueJS]);
    }

它似乎不起作用。 我已經成功測試了這些值通過print_r,echo等輸出了我想要的東西。但是循環遍歷不起作用。 這是我對stackoverflow提出的第一個真正的問題。 研究了很多線程並用谷歌搜索,但是我找不到這個答案。 非常感謝您的任何建議!

嘗試在循環外(循環以上)聲明$ doc!

您應該使用THIS取消設置它並聲明根!

unset($this->_scripts[JURI::root(true) . '/media/jui/js/jquery.min.js']);

因此,請嘗試(未測試!):

// grab the js file paths user enters into template parameters and add them to an array i can loop through

$doc = JFactory::getDocument();
$unsetJSFiles = $this->params->get( 'customFilesUnsetJS' );
$unsetJSFilesArray = explode(',', $unsetJSFiles);

// loop through each js file path user entered and unset each one from joomla html head  

foreach ($unsetJSFilesArray as $valueJS) {        
    unset($this->_scripts[JURI::root(true) . $valueJS]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM