如何更改首页&后端的组件图像?如何在php中获取HTML标记属性? [英] How to change the front page & component image from the backend? how to get HTML tag attributes in php?

查看:111
本文介绍了如何更改首页&后端的组件图像?如何在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' => ['System\Models\File'] ];
}

我的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 = \System\Models\File::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中创建一列,因为October会自动将图像存储在另一个名为"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

然后,十月将在控制面板的设置控制器中创建一个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

希望对您有帮助.

这篇关于如何更改首页&amp;后端的组件图像?如何在php中获取HTML标记属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆