十月CMS |如何从后端文件上传器获取组件中图像的路径?

时间:2019-04-22 16:59:57

标签: plugins octobercms

所以我有我的插件设置(或我相信),但是由于某种原因,在default.htm文件中,我无法使用“文件上传”小部件获取通过后端上传的图像的路径。

例如:<img src="{{ plugin.upload.path }}">不会向我显示图像的路径,但是如果我执行<img src="{{ plugin.upload.first.path }}"><img src="{{ plugin.upload.[0].path }}">,我确实会得到图像的路径,但这并不理想我想显示多张图片。

我感觉自己缺少一些非常简单的东西,但是由于我十月份刚来,所以请原谅我的无知。

谢谢。

components / gallerys / default.htm:

{% set gallerys = __SELF__.gallery %}

<ul>
{% for gallery in gallerys %}

<li>{{ gallery.uploads.path }}</li>

{% endfor %}
</ul>

components / Gallerys.php:

<?php namespace MartinSmith\Gallerys\Components;

use Cms\Classes\ComponentBase;
use MartinSmith\Gallerys\Models\Gallery;

class gallerys extends ComponentBase
{

public $gallery;

public function componentDetails(){
    return [
        'name' => 'Frontend Gallery',
        'description' => 'A gallery for you webpage'
    ];
}

public function onRun(){
    $this->gallery = $this->loadGallerys();
}

protected function loadGallerys(){
    return Gallery::all();
}

}

models / Gallery.php:

<?php namespace MartinSmith\Gallerys\Models;

use Model;

/**
* Model
*/
class Gallery extends Model
{
use \October\Rain\Database\Traits\Validation;

/*
 * Disable timestamps by default.
 * Remove this line if timestamps are defined in the database table.
 */
public $timestamps = false;


/**
 * @var string The database table used by the model.
 */
public $table = 'martinsmith_gallerys_';

/**
 * @var array Validation rules
 */
public $rules = [
];

public $attachMany = [
    'uploads' => 'System\Models\File'
];

}

models / columns.yaml:

columns:
name:
    label: name
    type: text
    sortable: true
uploads:
    type: partial
    path: ~/plugins/martinsmith/gallerys/models/gallery/_image.htm

models / fields.yaml:

fields:
name:
label: Name
span: auto
type: text
uploads:
    label: Upload
    span: full
    mode: image
    useCaption: true
    thumbOptions:
        mode: crop
        extension: auto
    imageWidth: '200'
    imageHeight: '200'
    type: fileupload

1 个答案:

答案 0 :(得分:0)

您真的很接近。这是我认为正在发生的事情。您正在从模型({% for gallery in gallerys %})中检索项目的数组。现在,由于您正在使用$attachMany{% for image in gallery.uploads %},因此模型中的每个项目都可以接受图像数组。因此,当您调用{{ plugin.upload.first.path }}{{ plugin.upload.[0].path }}时,您将获取数组的第一张图像并获取路径。

所以您要做的就是:

{% set gallerys = __SELF__.gallery %}

<ul>
{% for gallery in gallerys %}

    {% for image in gallery.uploads %}

    <li>{{ image.path }}</li>

    {% endfor %}

{% endfor %}
</ul>

Twig Dump +是一个可以帮助您调试OctoberCMS的出色插件(还有另一个Twig Dump也可以使用,但我喜欢Twig Dump +)。这使您可以编写{{ d(gallery.uploads) }}来查看对象的转储。