问题描述
在对普通的HTML/CSS/Javascript/PHP进行编程之后,由于Laravel框架和OctoberCMS看起来结构良好且易于使用/维护,因此我刚刚开始使用称为OctoberCMS的CMS.但是我对如何处理单个详细信息页面或概述页面有些困惑.
After programming plain HTML/CSS/Javascript/PHP I've just started using the CMS called OctoberCMS since both the Laravel framework and OctoberCMS look very well structured and easy to use/maintain. But I'm a little confused on how to process a single detail page or a overview page.
让我们以一个新闻页面为例.到目前为止,我已经创建了此页面:
Let's take a news page for example. So far I've made this page:
title = "News"
url = "/news/:news_id?|^[0-9]+$"
layout = "default"
description = "This is the news page."
is_hidden = "0"
meta_title = "News"
meta_description = "News page meta description"
==
<?php
function onStart()
{
$news_id = $this->param('news_id');
if(isset($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'] = $news;
}
}
?>
==
<div class="container">
<h1 class="block-title">
News
</h1>
{% if news_article is defined %}
Article
{% else %}
Overview
{% endif %}
</div>
但是,我实际上在哪里可以为新闻文章建立某种类型的库?我已经阅读了有关在新插件中创建新类的内容,但是找不到有关此问题的任何教程或文档,或者我在搜索时使用了错误的术语.有人可以举一个小例子(也许带有新闻文章)或张贴一个链接,在其中我可以找到教程/文档吗?
But where do I actually manage to make some sort of library for my news articles? I've read something about creating a new class in a new plug-in but I can't find any tutorials or documentation for this problem, or I'm just using the wrong terms while searching. Can someone make a small example (maybe with news articles) or post a link where I can find a tutorial/documentation?
推荐答案
使用构建器( 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>
这篇关于OctoberCMS新闻列表/详细信息页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!