使用 Craft CMS 搭建blog模型

原文連接:http://www.supperxin.com/Coding/Details/create-blog-using-craft-cmsphp

Craft CMS站點的搭建能夠參考這篇:使用Docker鏡像服務來搭建 craft cms 站點html

Template功能簡介

使用Template,能夠自定義網站的呈現樣式。docker

Template的保存位置在:craft/templates,能夠在此目錄下任意創建template,或者文件夾+Template,假如如今有Template:craft/templates/news/index.html,則此Template對應的url爲:http://ip/news/網站

要注意的是,Craft 的Template使用的是twig,且不容許直接使用php代碼,關於twig,能夠參考:Twig Primerthis

建立field

blog須要的欄位:Title, Body, Tag, Published, PublishDate:url

建立Section

建立名爲Blog的section,並將以下選項去除: Entries in this section have their own URLs3d

添加field到section

編輯section的Entry Types,將以前添加的欄位所有添加進來。code

添加template

在template目錄下,新建blog目錄,創建文件:htm

index.html,用於呈現blog列表,內容以下:blog

{% set blogs = craft.entries.section('blog') %}

{% extends "_layout" %}

{% block content %}
<main role="main">

  <section class="content">
    <ul class="download-links">
        {% for blog in blogs %}
            <li><a href="/blog/{{blog.slug}}">{{blog.title}}</a></li>
        {% endfor %}
    </ul>
  </section>
</main>
{% endblock %}

details.html,用於呈現blog內容,內容以下:

{% set blog = craft.entries.section('blog').slug(craft.request.lastSegment).first %}

{% extends "_layout" %}

{% block content %}
<main role="main">
  <section class="content">
    <h1>{{blog.title}}</h1>
    <p>{{blog.body}}</p>
  </section>
</main>
{% endblock %}

爲blog配置route

在route中添加:blog/slug --> blog/details的映射關係,這樣就能夠經過/blog/slug來訪問一篇具體的blog。

最終的呈現效果以下:

參考:

相關文章
相關標籤/搜索