Reflexecute
Reflexecute

Reputation: 291

OctoberCMS news list / detail page

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?

Upvotes: 1

Views: 794

Answers (3)

Tauhed
Tauhed

Reputation: 109

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

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: 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>

Upvotes: 1

onedayiwillchangethis
onedayiwillchangethis

Reputation: 143

Documentation: https://octobercms.com/docs/plugin/registration

If you want to generate some code in command line here are some useful commands:

Generate plugin registration file and folders

php artisan create:plugin AuthorName.PluginName

Generate model

php artisan create:model AuthorName.PluginName ModelName

Generate controller

php artisan create:controller AuthorName.PluginName ModelNames

Refresh (reinstall) plugin

php artisan plugin:refresh AuthorName.PluginName

This should get you going, docs will be helpful after that.

Upvotes: 1

Pawel
Pawel

Reputation: 21

That is more comfortable to use plugin instead for write all code yourself.

Rain lab plugin allow create, manage, categorize, edit all kinds articles (include news).

You can get admin part from that plugin and use your visitor view.

Upvotes: 1

Related Questions