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

How to change the front page amp; component image from the backend? how to get HTML tag attributes in php?(如何更改首页amp;来自后端的组件图像?如何在 php 中获取 HTML 标签属性?)
本文介绍了如何更改首页&来自后端的组件图像?如何在 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

希望对你有帮助.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)