CMS的MVC?主题怎么样?

时间:2023-01-14 09:22:01

I understand what is MVC and CMS. I understand MVC pattern and how CMS should working. But I have problem with theming and the pattern.

我明白什么是MVC和CMS。我理解MVC模式以及CMS应该如何工作。但我对主题和模式有问题。

Example:

When CMS is installed on serwer I want to change my homepage. I want to display some additional data. I change my homepage template and add a function call to pull data from DB. My new data will be displayed on my homepage. :)

当CMS安装在服务器上时,我想更改我的主页。我想显示一些额外的数据。我更改了我的主页模板并添加了一个函数调用来从DB中提取数据。我的新数据将显示在我的主页上。 :)

But when I do this I broke MVC pattern because in this situation View decide which data should be read from DB to display on my homepage.

但是当我这样做时,我打破了MVC模式,因为在这种情况下,View决定应该从DB中读取哪些数据以显示在我的主页上。

So... is it MVC for a CMS? Or maybe it shouldn't be a clear MVC pattern to work with situations like this? Maybe I should forget about patterns? I'm confusing...

所以...它是CMS的MVC吗?或者也许它不应该是一个明确的MVC模式来处理这样的情况?也许我应该忘记模式?我很困惑......

PS Wordpress it's not build on MVC pattern, I guess?

PS Wordpress它不是建立在MVC模式上的,我猜?

1 个解决方案

#1


Design patterns like MVC are meant to be about separating your display code from the business logic, etc. This makes it easier to change any part without affecting any of the other parts (eg. change the templates without having to worry about accidentally changing any business logic).

像MVC这样的设计模式意味着将显示代码与业务逻辑等分开。这样可以更容易地更改任何部分而不影响任何其他部分(例如,更改模板而不必担心意外更改任何业务逻辑)。

You situation sounds like the function you are adding is simple view logic? It might be best to put the function into a Helper and have the helper call for the data through a Model. Then, in your view, just call the Helper.

您的情况听起来像您添加的功能是简单的视图逻辑?最好将函数放入Helper并通过Model对数据进行帮助调用。然后,在您的视图中,只需致电助手。

In pseudocode:

Helper

function get_whatever_data () {
    // get the actual data from the model
    return SomeModel.get_the_data_thats_needed_here()
}

View

<div id="some-id">
    <? print get_whatever_data() ?>
</div>

#1


Design patterns like MVC are meant to be about separating your display code from the business logic, etc. This makes it easier to change any part without affecting any of the other parts (eg. change the templates without having to worry about accidentally changing any business logic).

像MVC这样的设计模式意味着将显示代码与业务逻辑等分开。这样可以更容易地更改任何部分而不影响任何其他部分(例如,更改模板而不必担心意外更改任何业务逻辑)。

You situation sounds like the function you are adding is simple view logic? It might be best to put the function into a Helper and have the helper call for the data through a Model. Then, in your view, just call the Helper.

您的情况听起来像您添加的功能是简单的视图逻辑?最好将函数放入Helper并通过Model对数据进行帮助调用。然后,在您的视图中,只需致电助手。

In pseudocode:

Helper

function get_whatever_data () {
    // get the actual data from the model
    return SomeModel.get_the_data_thats_needed_here()
}

View

<div id="some-id">
    <? print get_whatever_data() ?>
</div>