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

Craft CMS站点的搭建可以参考这篇:使用Docker镜像服务来搭建 craft cms 站点

Template功能简介

使用Template,可以自定义网站的呈现样式。

Template的保存位置在:craft/templates,可以在此目录下任意建立template,或者文件夹+Template,假如现在有Template:craft/templates/news/index.html,则此Template对应的url为:http://ip/news/。

要注意的是,Craft 的Template使用的是twig,且不允许直接使用php代码,关于twig,可以参考:Twig Primer

创建field

blog需要的栏位:Title, Body, Tag, Published, PublishDate:

创建Section

创建名为Blog的section,并将如下选项去除: Entries in this section have their own URLs

添加field到section

编辑section的Entry Types,将之前添加的栏位全部添加进来。

添加template

在template目录下,新建blog目录,建立文件:

index.html,用于呈现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。

最终的呈现效果如下:

参考: