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

问题描述 投票:1回答:1

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

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

我有一种感觉,我错过了一些非常简单的事情,但请原谅我的无知,因为我对十月很新。

先感谢您。

组件/ gallerys / Default.htm的:

{% set gallerys = __SELF__.gallery %}

<ul>
{% for gallery in gallerys %}

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

{% endfor %}
</ul>

组件/ 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();
}

}

车型/ 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'
];

}

车型/ columns.yaml:

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

车型/ 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
plugins octobercms
1个回答
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>

一个可以帮助你调试OctoberCMS的很棒的插件是Twig Dump +(还有一个是Twig Dump,它也可以,但我喜欢Twig Dump +)。这允许您编写{{ d(gallery.uploads) }}以查看对象的转储。

© www.soinside.com 2019 - 2024. All rights reserved.