简体   繁体   English

Joomla模板覆盖PHP初始化变量

[英]Joomla template override PHP initializing variable

I'm trying to create an override for my template to customize the way an extension should display it's fields in the article. 我正在尝试为模板创建替代,以自定义扩展在文章中显示其字段的方式。

The extension I use is DPfields and I'm using this reference guide from the developer: https://joomla.digital-peak.com/documentation/162-dpfields/2750-rendering-fields 我使用的扩展名是DPfields,我正在使用开发人员的参考指南: https ://joomla.digital-peak.com/documentation/162-dpfields/2750-rendering-fields

Expecially I'm referencing to the paragraph: Accessing the fields in the layout 特别是我要参考以下段落:访问布局中的字段

I've created a new php file started from the default.php file for article view and inside this new file (newfile.php) I'm trying to display a gallery field type from the component DPField. 我已经从用于文章查看的default.php文件创建了一个新的php文件,并且在此新文件(newfile.php)内,我试图显示来自组件DPField的Gallery字段类型。

I've succesfully inserted this code in the newfile.php: 我已成功将此代码插入到newfile.php中:

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

so it correctly shows in the output the gallery. 因此它可以在输出中正确显示图库。

My question is: how could I improve that code? 我的问题是:如何改善该代码? Is there a better way to let it work instead of using a foreach? 有没有更好的方法让它工作而不是使用foreach?

Thanks in advance. 提前致谢。

You can try to filter the array. 您可以尝试过滤数组。 It'll not change the amount of code you need I guess. 我猜这不会改变您需要的代码量。

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