如何更改首页&来自后端的组件图像?如何在 php 中获取 HTML 标签属性?

我的模型 Settings.php

my model Settings.php

class Settings extends Model
{
    public $implement = ['System.Behaviors.SettingsModel'];
    // A unique code
    public $settingsCode = 'dca_plugins_settings';
    // Reference to field configuration
    public $settingsFields = 'fields.yaml';
    /**
     * @var array Relations
     */
    public $attachOne = [ 'avatar' => ['SystemModelsFile'] ];
}

我的 Fields.yaml

my Fields.yaml

fields:
  id:
    label: ID
    disabled: true

  avatar:
    label: Avatar
    type: fileupload
    mode: image
    imageHeight: 150
    imageWidth: 250

我的组件 comp.php

my Component comp.php

public $avatar_id = 1;

public function getAvatarImage($avatar_id)
{
    $var = SystemModelsFile::select('disk_name')->where('attachment_id', $avatar_id)->first();           
    if (count($var) == 0) return "";

    return $var->path;
} 

function setMyAvatarId($id)
{
    $this->avatar_id = $id;
} 

我的html default.htm

my html default.htm

{% set avatar_id= __SELF__.property("avatar_id") %}

{% if avatar_id is not empty %}
{% do __SELF__.setMyAvatarId(avatar_id) %}
{% endif %}

<img id="avatar-image" alt="Virtual agent avatar" src="{{ __SELF__.getavatarImage(avatar_id) }}">
<script>
var avatar_id = {{ avatar_id }};
</script> 

我设法获得了一个随机链接,但图片未显示

I manage to get a random link but the image is not shown

后端 - 模型设置

有谁知道如何定义页面属性?如何将我的页面链接到模型设置?

Does anyone know how to define page properties? How do I link my page to the model settings?

如何让它发挥作用?有人请帮助我~~我很迷茫:(

How do I make it work? Someone pls help me~~ I'm so lost :(

推荐答案

要更改图像,您可以将其添加到模型中:

To change the image you can add this to your model:

public $attachOne = ['avatarImage'];

而且您不必在数据库中创建名为 avatarImage 的列,因为十月会自动将您的图像存储在另一个名为system_files"或类似的表中.因此,在您的 fields.yaml 中,您必须添加以下内容:

and you don't have to create a column in the database called avatarImage because October will automatically store your image in another table called 'system_files' or something like that. So in your fields.yaml you'll have to add this:

avatarImage:
     type: mediafinder
     mode: image
     label: My Avatar

然后,October 将在控制面板的设置控制器中创建一个 mediafinder 表单小部件,您可以对其进行更改.要在前端页面中显示图像,您必须在页面的 php 代码部分添加一些代码

Then October will create a mediafinder form widget in your settings controller in the control panel and you can change it. And to show the image in your frontend page you will have to add some code in the php code section of the page

$this['settings'] = //The Code to Get the Model;

您可以轻松地使用 getPath() 方法显示图像.

And easily you can display the image using getPath() method.

<img src="{{ settings.getPath() }}" />

如果上面的代码不起作用,您可以将其替换为:

If the above code didn't work you can replace it with:

<img src="{{ settings.getPath()|media" />

我认为第二个会正常工作,我有一段时间没有使用 october :D

I think the second one will work correctly, I didn't use october for a while :D

希望对你有帮助.

相关文章