簡體   English   中英

Joomla模板覆蓋PHP初始化變量

[英]Joomla template override PHP initializing variable

我正在嘗試為模板創建替代,以自定義擴展在文章中顯示其字段的方式。

我使用的擴展名是DPfields,我正在使用開發人員的參考指南: https ://joomla.digital-peak.com/documentation/162-dpfields/2750-rendering-fields

特別是我要參考以下段落:訪問布局中的字段

我已經從用於文章查看的default.php文件創建了一個新的php文件,並且在此新文件(newfile.php)內,我試圖顯示來自組件DPField的Gallery字段類型。

我已成功將此代碼插入到newfile.php中:

<?php
    foreach ($this->item->dpfields as $field) {
        $gallery = (($field->type)=='gallery');
        if (!empty($gallery)) {
            echo '<div class="galleryfield">' .$field->value. '</div>'; 
        }
    }
?>

因此它可以在輸出中正確顯示圖庫。

我的問題是:如何改善該代碼? 有沒有更好的方法讓它工作而不是使用foreach?

提前致謝。

您可以嘗試過濾數組。 我猜這不會改變您需要的代碼量。

function filterForGalleryType($field) {
  return ($field->type) == 'gallery';
}

$galleryFields = array_filter($this->item->dpfields, "filterForGalleryType");

foreach ($galleryFields as $galleryfield) {
  echo '<div class="galleryfield">' .$galleryfield->value. '</div>'; 
}

暫無
暫無

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

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