code craft

tech2023-05-27  26

code craft

We’ve recently featured articles on two popular content management systems – Perch and Statamic. In this article, Ryan Irelan introduces Craft, another CMS that has received considerable acclaim in recent years.

我们最近在两个流行的内容管理系统Perch和Statamic上发表了文章。 在本文中,Ryan Irelan介绍了Craft(Craft),这是另一个近年来广受赞誉的CMS。

Craft is a database-driven CMS that makes it easy to build and manage content-heavy websites. It was originally released in 2013 by Pixel & Tonic, a web development company that got its start building commercial add-ons for the ExpressionEngine CMS.

Craft是数据库驱动的CMS,可轻松构建和管理内容繁多的网站。 它最初由网络开发公司Pixel&Tonic于2013年发布,该公司开始为ExpressionEngine CMS构建商业附加组件。

Craft isn’t a website builder – you still need to be familiar with HTML and CSS to build the front-end layer of your website – but it is geared toward making both the development and authoring experience as great as possible.


You can see the features of Craft on the features section of the Craft website. There are videos for each main feature along with descriptions and screenshots. It’ll give you a good overview of the capability of Craft.

您可以在Craft网站的功能部分中查看Craft的功能 。 每个主要功能都有视频以及描述和屏幕截图。 它会为您很好地概述Craft的功能。

Craft.io商业模式 (Craft Business Model)

Craft is a commercial CMS, but there is a free option (called Personal License) so you can try out the software without any risk. Upgrading to one of the two paid licenses is simple via an in-app purchase right inside of Craft.

Craft是商业CMS,但是有一个免费选项(称为“个人许可证”),因此您可以免费试用该软件。 通过Craft内部的应用内购买,可以轻松升级到两个付费许可证之一。

You can run the paid versions of Craft locally (e.g. or without purchasing, so you can experiment with the commercial features or work on a client site without needing to deal with licensing until the project launches (at which time you’ll need to purchase the appropriate license).

您可以在本地购买Craft的付费版本(例如craft.dev或 )而无需购买,因此您可以试用商业功能或在客户站点上工作而无需在项目启动之前处理许可(此时)您需要购买适当的许可证)。

The licensing is as follows:


Personal: Free

个人: 免费

Client: $199 per site

客户: 每个站点$ 199

Pro: $299 per site

专业版: 每个网站299美元

The Personal license limits you to one user account and doesn’t include some other features. The Client license adds an additional client user, and Pro is unlimited users. All licenses include unlimited content sections, fields, etc. Read the pricing page for all the details on what is included with each license type.

个人许可证将您限制为一个用户帐户,并且不包含其他一些功能。 客户端许可证增加了一个额外的客户端用户,而Pro是无限的用户。 所有许可证都包括无限的内容部分,字段等。 有关每种许可证类型所含内容的所有详细信息 ,请阅读定价页面 。

Craft.io如何运作 (How Craft Works)

Like many content management systems, Craft allows you to customize your entry forms and organize your content and data into content buckets.


The Craft way is similar to some other CMSs with a few conveniences (like the ability to reuse a field multiple times across different content sections).


Let’s take a look at the different parts of Craft and how they work.


栏目 (Sections)

In Craft, you store content in Sections. Each section can be one of three Section Types:

在Craft中,您将内容存储在Sections中。 每个部分可以是以下三种类型之一:

Channel – used for a collection of related content like a blog, news articles, press releases, or products.

频道–用于收集相关内容,例如博客,新闻文章,新闻稿或产品。 Structure – also used for a collection of similar content but which requires a hierarchy of some sort. The Craft documentation is a good example of a Structure.

结构–也用于相似内容的集合,但需要某种层次结构。 Craft文档是一个很好的结构示例。 Single – used for one-off pages. A common use of the Single is for the homepage

单页–用于一次性页面。 主页的常见用法是

字段,字段布局和条目类型 (Fields, Field Layouts, and Entry Types)

Each Section in Craft has one or more Entry Types. These Entry Types allow you to create Field Layouts (a collection of fields) and assign them to the Section Entry Type. This means one Section can have multiple content variations.

Craft的每个部分都有一个或多个输入类型 。 这些输入类型使您可以创建字段布局(字段的集合)并将其分配给“节输入类型”。 这意味着一个版块可以具有多个内容变体。

A Field Layout is made up of individual fields. Craft ships with multiple custom field types, including the following (but there are more than this!):

字段布局由各个字段组成。 Craft附带了多种自定义字段类型,包括以下内容(但还有更多!):

Plain Text

纯文本 Rich Text (think WYSIWYG)

富文本(认为所见即所得) Table

表 Assets (for file uploading and association)

资产(用于文件上传和关联) Entries (for relating one or more Entries)


Matrix (for creating customizable content structures made up of multiple fields – see more on Matrix in the feature video)

矩阵(用于创建由多个字段组成的可自定义的内容结构, 请参见功能视频中有关矩阵的更多信息 )

Fields can be used across Field Layouts and Sections. It pays in Craft to create your fields as generic as possible so you can reuse them across multiple sections.

可以在“字段布局”和“节”之间使用字段。 使用Craft可以尽可能多地创建您的字段,因此您可以在多个部分中重复使用它们。

使用树枝的模板 (Templates Using Twig)

Like any good CMS, Craft lets you use your own HTML/CSS/JS and use it as-is without having to create a theme or otherwise compromise a handsomely-coded set of templates.

像任何优秀的CMS一样,Craft允许您使用自己HTML / CSS / JS并按原样使用它,而无需创建主题或以其他方式破坏一组精美编码的模板。

To achieve this, Craft uses the Twig template engine to parse and display your website templates. In addition to allowing you to use your own HTML & CSS, the advantage of Twig is that it’s fast and has a syntax that a non-developer can grok while also being flexible (and elegant) for a developer to use for advanced functionality.

为此,Craft使用了Twig模板引擎来解析和显示您的网站模板。 除了允许您使用自己HTML和CSS外,Twig的优点还在于它速度快,并且具有非开发人员可以使用的语法,同时还灵活(优雅)地供开发人员使用以实现高级功能。

Another benefit of using Twig is that it is maintained by a third party (Sensio Labs) so Pixel & Tonic doesn’t have to spend valuable time improving and supporting a rendering engine, instead using their resources to improve the core product instead.

使用Twig的另一个好处是它是由第三方( Sensio Labs )维护的,因此Pixel&Tonic不必花费宝贵的时间来改进和支持渲染引擎,而不必使用他们的资源来改进核心产品。

To learn more about Twig, review the Twig for Template Designers guide from Sensio Labs or take my Twig and Craft video course.

要了解有关Twig的更多信息,请查看Sensio Labs的Twig for Template Designers指南,或参加我的Twig and Craft视频课程 。

在Craft中使用Twig (Using Twig in Craft)

There’s quite a bit to using Craft in Twig, but here are a couple common examples to give you an idea of what the template code looks like.


You will frequently want to output the Entries stored in a Section. To do that in Craft and Twig, you would do the following:

您将经常需要输出存储在分区中的条目。 要在Craft和Twig中做到这一点,您需要执行以下操作:

{% for entry in craft.entries.section("news") %} <h1>{{ entry.title }}</h1> {{ entry.body }} {% end for %}

This example uses a for-loop to to iterate through whichever entires are returned by craft.entries.section("news").

本示例使用for -loop遍历craft.entries.section("news").返回的craft.entries.section("news").

One of the nicest features of Twig (in this humble author’s opinion) is support for layouts, so you can smartly reuse template code.


You do this by inheriting a base template (the layout) and then overriding parts of it using blocks:


{% extends "layouts/_base" %} {% block content %} {# override the same block in the _base.html layout #} {% end block %}

Twig also allows you to store data to variables right in the template. Using the same example as before, let’s store the output of craft.entries.section("news") to a variable called newsEntries:

Twig还​​允许您将数据直接存储到模板中的变量中。 使用与之前相同的示例,让我们将craft.entries.section("news")的输出存储到一个名为newsEntries的变量中:

{% set newEntries = craft.entries.section("news") %}

We can then iterate over the array assigned to the variable and output the entries:


{% for entry in newsEntries %} ... {% end for %}

Twig also has the standard stuff like conditionals, and handy stuff like filters. Filters in Twig allow you to manipulate the data assigned to variables:

Twig还​​具有标准条件(如条件条件)和方便的条件(如过滤器)。 Twig中的过滤器允许您操纵分配给变量的数据:

{{ entry.title | upper }}

This would uppercase the title of the entry. Craft comes with its own Filters that allow you to do things like format currency or even group together items (like entries) based on common properties (like the year they were created).

这会将条目标题大写。 Craft带有自己的过滤器 ,可让您执行诸如设置货币格式或什至基于共同属性(如创建年份)将项目(如条目)分组在一起的操作。

路线 (Routes)

I recently worked on a Craft implementation that required a custom URL structure that Craft doesn’t support out-of-the-box. Well, it does with Routes.

我最近在一个Craft实施中进行工作,该实施需要一个Craft不支持即用的自定义URL结构。 好吧,它与路线有关。

Routes saved me because they’re nearly infinitely flexible for re-routing URIs to render the templates you desire.


With Routes you can tell Craft to handle a specified URI (exact or based on wild cards and regular expressions to account for dynamic URIs) and render a specified template.


You might not use Craft’s Routes on your first or second project, but when you need it you will be thankful for it!


Craft.io控制面板 (The Craft Control Panel)

Pixel & Tonic excels at simple yet elegant user interface design, and their Control Panel shows that off.


The Craft Control Panel is fully responsive, meaning you can reliably (not ham-fistedly as it is with so many CMSs) edit and create content on your Craft site from any device.


实时预览 (Live Preview)

Craft has a Live Preview feature for Section Entries. While editing an entry, you can toggle Live Preview and see what the content will look like when it’s published. Live Preview uses the same template as the front-end of the site and responds to content changes.

Craft具有“节条目”的实时预览功能。 在编辑条目时,您可以切换“实时预览”并查看内容在发布时的外观。 实时预览使用与网站前端相同的模板,并响应内容更改。

To see Live Preview in action, watch the Live Preview video on the Craft website.

要观看实时预览,请在Craft网站上观看实时预览视频 。

一键更新 (One-click Updating)

Craft provides one-click updating right from inside the Control Panel. You’ll be notified when there’s a new version and you can install in-place with a single click. Craft backs up your files and database first before updating and will even roll back automatically if it encounters an update failure.

Craft可从控制面板内部提供一键式更新。 当有新版本时,您会收到通知,只需单击即可就地安装。 Craft在更新之前先备份您的文件和数据库,如果遇到更新失败,它甚至会自动回滚。

为什么要使用手Craft.io品? (Why Use Craft?)

Everyone has their own reasons for using Craft.


Developers like the flexibility and performance of the Twig templates and the extensibility of the CMS via plugins.


Designers like that their designs can be implemented without compromise and disrespect.


Clients like that they have an elegant and simple authoring experience.


尝试手 (Try Craft)

If you want to use Craft, you can spin up a free sandbox site and take a look for yourself.


社区与资源 (Community & Resources)

I’ve created a series of video courses that help you quickly and efficiently learn Craft. You can find more about them over at my training site, Mijingo.

我创建了一系列视频课程,可以帮助您快速有效地学习Craft。 您可以在我的培训网站Mijingo上找到有关它们的更多信息。

Additionally, there are a handful of Craft resources for you to use:


Straight-up Craft


Craft Stack Exchange


Craft Cookbook


Craft Slack


#craftcms hashtag on Twitter


If you have any questions about Craft, or experiences to share, please leave a comment below!



code craft