且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

OctoberCMS新闻列表/详细信息页面

更新时间:2023-02-11 19:29:41

使用构建器( https://octobercms.com/plugin/rainlab-builder )插件可以非常轻松地管理CRUD.

Use Builder (https://octobercms.com/plugin/rainlab-builder) plugin to manage CRUD very easily.

假设您有一个名为NewsModel的模型,并且想要在前端显示新闻列表或单个新闻,则可以通过以下方式修改代码.

Suppose you have a model named NewsModel and you want to show the news listing or a single news in the frontend then you can modify your code by the following..

N.B:无需在php部分中编写php开始和结束标记,只需编写

N.B: No need to write php opening and closing tag in the php section, just write

use Namespace\Plugin\Models\NewsModel; //needed to get data through model
function onStart()
{
    $news_id = $this->param('news_id');
    if($news_id) {
        $news_article = []; //get the news article by id
        $this['news_article'] = $news_article;
    } else {
        $news = []; //get an array of news articles (last ... articles ordered by datetime desc)
        $this['news_list'] = $news;
    }
}
==
<div class="container">    
    {% if news_article %}
        <h1 class="block-title"> News Details</h1>
        <div>{{ news_article.details }}</div> <!--Suppose you have a field named 'details' in the news table -->
    {% elseif news_list %}
        <h1 class="block-title"> News List</h1>
        <ul>
        {% for news in news_list %}
           <li> {{ news.title }}</li><!--Suppose you have a field named 'title' in the news table -->
        {% endfor %}
        </ul>
    {% else %}
        No news found !
    {% endif %}
</div>