Kirby CMS简介

Kirby is a lightweight CMS that probably fits somewhere between feature-rich platforms such as Drupal, and static site generators such as Jekyll.

Kirby是一种轻量级CMS,可能适合功能丰富的平台(如Drupal)和静态站点生成器(如Jekyll)之间的某个地方。

What makes Kirby quite different to most CMS’s – and closer to Jekyll in the process – is that it has no database. Instead, everything is stored as files; some for configuration, some for content – all in addition to the usual template files, partials and plugins.

使Kirby与大多数CMS完全不同的地方-并在此过程中更接近Jekyll-是因为它没有数据库。 相反,所有内容都存储为文件。 一些用于配置,一些用于内容–除了常用的模板文件,部分文件和插件之外,还包括其他所有内容。

In this article I’m going to take a look at Kirby, demonstrate how to use it, and assess some of its strengths and weaknesses.

在本文中,我将介绍Kirby,演示如何使用它,并评估其一些优点和缺点。

许可,下载和安装 (Licensing, Downloading and Installing)

Kirby is provided on the basis of a per-site license of (at time of writing) $39 USD. However, you can try it out on your local machine for free – it’s only when you get into production that the license fee becomes payable.

Kirby是基于(在撰写本文时)39美元的每站点许可提供的。 但是,您可以在本地计算机上免费试用-只有当您投入生产后,才需要支付许可费。

The package is available from the Kirby Downloads page as a .zip file, or from Github.

可从Kirby下载页面.zip文件Github的形式获得该软件包。

To install, unzip / copy the archive to your web root and point your virtual host at Kirby’s root directory. And that’s pretty much it – right away, you should see some dummy content and Kirby’s default theme.

要安装,请将压缩文件解压缩/复制到您的Web根目录,然后将虚拟主机指向Kirby的根目录。 差不多就可以了–立即,您应该看到一些虚拟内容和Kirby的默认主题。

The Default Kirby theme

柯比的网站结构 (Kirby’s Site Structure)

Let’s suppose we plan to work with the basic site structure provided by default, but add a new page at the top-level called “Our Work”. We want it to appear before “Contact” in the menu.

假设我们计划使用默认提供的基本站点结构,但在顶层添加一个名为“我们的工作”的新页面。 我们希望它出现在菜单中的“联系人”之前。

Start by renaming the directory which holds the contact page from content/03-contact to content/05-contact.

首先将包含联系人页面的目录从content/03-contact重命名为content/05-contact

By default, the menu system uses the directory name to determine the order pages should appear in the navigation. We’ll create a new page for the fourth position later.

默认情况下,菜单系统使用目录名称来确定订单页面应出现在导航中。 稍后,我们将为第四个位置创建一个新页面。

Now, create a new folder called content/03-our-work, and in that a file called our-work.txt

现在,创建一个名为content/03-our-work的新文件夹,并在其中创建一个名为our-work.txt的文件。

Content files should always have a .txt extension by default, despite their content being a mixture of YAML and Markdown. To modify this behaviour, change the following line in site/config/config.php:

内容文件默认情况下应始终具有.txt扩展名,尽管它们的内容是YAML和Markdown的混合。 要修改此行为,请在site/config/config.php更改以下行:

c::set('content.file.extension', 'txt');

The file should have the following structure:

该文件应具有以下结构:

Title: Our Work
----
Text: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.

Now, if you browse to the site you should see a new menu item in between Projects and Contact, with the content we just defined in 04-our-work/our-work.txt.

现在,如果浏览到该站点,您应该在Projects和Contact之间看到一个新的菜单项,其中包含我们刚刚在04-our-work/our-work.txt定义的内容。

Obviously, you can edit existing pages in the same way. You’ll find the contents of the homepage in content/home/home.txt.

显然,您可以用相同的方式编辑现有页面。 您可以在content/home/home.txt找到主页的content/home/home.txt

Let’s take a look at how the menu is built to get an insight into what’s going on. The menu is implemented as a snippet, which is basically a view partial:

让我们看一下菜单的构建方式,以了解正在发生的事情。 菜单以代码段的形式实现,基本上是部分视图:

// site/snippets/menu.php

<nav class="menu">
<ul>
    <?php foreach($pages->visible() AS $p): ?>
    <li><a<?php echo ($p->isOpen()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo html($p->title()) ?></a></li>
    <?php endforeach ?>
</ul>
</nav>

Notice that it calls a method called visible(). Content prefixed with a number – 01, 02 etc – is visible; that is, it’s designed to appear in the navigation. Content without – such as home and error – is hidden. Try creating a folder called content/test, with a corresponding content file called test.txt – you’ll find that it doesn’t appear on the menu. If you want to hide additional pages, change the following line in site/config/config.php:

注意,它调用了一个名为visible()的方法。 与一些前缀内容- 0102等等-可见; 也就是说,它旨在显示在导航中。 隐藏没有内容的内容(例如homeerror )。 尝试创建一个名为content/test的文件夹,以及一个名为test.txt的相应内容文件-您会发现它没有出现在菜单上。 如果要隐藏其他页面,请在site/config/config.php更改以下行:

c::set('content.file.ignore', array());

$pages->visible() returns pages ordered by directory name, hence the numeric prefixes. You can override that behaviour; suppose you wanted a list of pages alphabetically ordered by title:

$pages->visible()返回按目录名称排序的页面,因此返回数字前缀。 您可以覆盖该行为; 假设您想要一个按标题字母顺序排列的页面列表:

$a_to_z = $pages->visible()->sortBy('title', 'asc');

To list pages beneath the currently open menu item (i.e., a sub-menu) you can do this:

要在当前打开的菜单项(即子菜单)下面列出页面,您可以执行以下操作:

if($root = $pages->findOpen()) {
$items = $root->children()->visible();    
}

There’s lots more information on managing menus on the Kirby website.

Kirby网站上有许多有关管理菜单的信息

主题化 (Theming)

In a new installation, pages are rendered using site/templates/default.php.

在新安装中,页面是使用site/templates/default.php呈现的。

Let’s take a look at the contents:

让我们看一下内容:

<?php snippet('header') ?>
<?php snippet('menu') ?>
<?php snippet('submenu') ?>

<section class="content">

<article>
    <h1><?php echo html($page->title()) ?></h1>
    <h2><?php echo html($page->subtitle()) ?></h2>
    <?php echo kirbytext($page->text()) ?>
</article>

</section>

<?php snippet('footer') ?>

snippet() includes a file from site/snippets – we’ll look at those shortly.

snippet()包含来自site/snippets的文件-我们将在稍后对此进行介绍。

You’ll notice that the page content is available as a variable called $page, which reads the properties defined in the corresponding text file. The html() function is used to escape values, and kirbytext() applies some additional interpretation, as we’ll see shortly.

您会注意到页面内容可以作为名为$page的变量使用,该变量读取相应文本文件中定义的属性。 html()函数用于转义值,而kirbytext()应用了一些附加的解释,稍后我们将看到。

To create a new template for a specific page, simply create a new file in site/templates whose name matches the filename of the content for the page in question. So, for example, if you want a separate page template for the homepage, you’d create a file named site/templates/home.php. For the “Our Work” page we created earlier, you’d call it our-work.php.

要为特定页面创建新模板,只需在site/templates创建一个新文件,其名称应与所讨论页面的内容文件名相匹配。 因此,例如,如果您要为主页使用单独的页面模板,则可以创建一个名为site/templates/home.php的文件。 对于我们之前创建的“我们的工作”页面,您将其称为our-work.php

By default, with Kirby comes an assets folder, containing images and styles sub-folders, but you’re free to structure your theme’s assets as you wish.

默认情况下,Kirby带有一个assets文件夹,其中包含imagesstyles子文件夹,但是您可以根据需要随意构建主题资产。

网站属性和摘要 (Site Properties, and Snippets)

If you open up the file content/site.txt, you’ll see a bunch of properties of the site itself such as the title, author and copyright notice. You’re free to add additional properties. For example, if you add the following:

如果打开文件content/site.txt ,您将看到网站本身的许多属性,例如标题,作者和版权声明。 您可以随意添加其他属性。 例如,如果添加以下内容:

----

Twitter: myusername

You can now print this out in your templates or snippets, using a magic method of the $site variable. So to put it in the footer, open up site/snippets/footer.php and add the following:

现在,您可以使用$site变量的魔术方法在模板或摘要中将其print出来。 因此,要将其放在页脚中,请打开site/snippets/footer.php并添加以下内容:

<a href="http://twitter.com/<?php print $site->twitter() ?>">Twitter</a>

创建一个简单的博客 (Creating a Simple Blog)

We’ve seen how to create content, create a page-specific template and generate a list of pages. Armed with this knowledge, creating a simple blog is pretty straightforward.

我们已经看到了如何创建内容,创建特定于页面的模板以及如何生成页面列表。 有了这些知识,创建一个简单的博客就非常简单了。

Create a folder called content/04-blog and in it, a file called blog.txt:

创建一个名为content/04-blog的文件夹,并在其中创建一个名为blog.txt的文件:

Title: Blog
----

Within that folder, create a bunch of blog posts (using the same format as the existing pages), e.g.

在该文件夹中,创建一堆博客帖子(使用与现有页面相同的格式),例如

content/04-blog/01-first-post/first-post.txt
content/04-blog/02-first-post/second-post.txt
content/04-blog/03-first-post/third-post.txt

Now, create a template for the Blog:

现在,为博客创建一个模板:

// site/templates/blog.php

<?php snippet('header') ?>
<?php snippet('menu') ?>

<section class="content">

    <h1><?php echo html($page->title()) ?></h1>

    <?php foreach ($pages->findOpen()->children()->visible()->flip() as $post): ?>
<article>
    <h2><a href="<?php print $post->url() ?>"><?php echo html($post->title()) ?></a></h2>
    <p><?php print excerpt($post->text()) ?></p>
    <p><a href="<?php print $post->url() ?>">Read more &raquo;</a></p>
</article>
    <?php endforeach ?>

</section>

<?php snippet('footer') ?>

Notice how we’re calling flip() to reverse the order of the pages, all of whom are children of the current page – i.e., pages within the content/04-blog folder.

注意,我们如何调用flip()来反转页面的顺序,这些页面都是当前页面的子级,即content/04-blog文件夹中的页面。

This template also introduces the url() function, which returns a URL to the page in question, and excerpt(), which returns a summary of the text content. To change the number of characters returned, simply provide it as a second parameter:

该模板还引入了url()函数和excerpt() url()函数,该函数返回所涉及页面的URL,而excerpt() url()函数返回文本内容的摘要。 要更改返回的字符数,只需将其作为第二个参数提供即可:

print excerpt($post->text(), 250); // prints the first 250 characters

柯比文字 (Kirbytext)

The actual content in Kirby is written in “Kirbytext”. It’s stored as the text property in a YAML file, and is essentially an extension of Markdown but with some additional macros.

Kirby中的实际内容以“ Kirbytext”编写。 它作为text属性存储在YAML文件中,本质上是Markdown的扩展,但带有一些其他宏。

This is best illustrated with an example:

最好用一个例子说明:

File: page.txt:

文件: page.txt

Title: About
 ----
Somefield: Something
 ----
Body: This is some sample content. It's based on Markdown, so we can use **bold** or *emphasis*, and [write links](http://getkirby.com).

Kirbytext also has macros, that look a little like this:

(youtube: http://www.youtube.com/watch?v=ZZ5LpwO-An4 width: 300 height: 200)

The Kirby-specific macro / tag here is for embedding a Youtube video, but you can extend Kirbytext to write your own. To do so, start by creating a file called kirbytext.extended.php in site/plugins:

特定于Kirby的宏/标签用于嵌入YouTube视频,但是您可以扩展Kirbytext编写自己的视频。 为此,首先在site/plugins创建一个名为kirbytext.extended.php的文件:

class kirbytextExtended extends kirbytext {

function __construct($text, $markdown=true) {

    parent::__construct($text, $markdown);

}  

}

Now, in the constructor you can define new tags, as well as new attributes. Suppose we wanted to add Gravatar support:

现在,在构造函数中,您可以定义新标签以及新属性。 假设我们要添加Gravatar支持:

// define custom tags
    $this->addTags('gravatar');

    // define custom attributes
    $this->addAttributes('size');

Once you’ve defined a new tag through addTags(), you simply implement a method with the same name, which will be called in order to render the tag:

通过addTags()定义新标签后,您只需实现一个具有相同名称的方法,就会调用该方法以呈现该标签:

/**
*  Render the gravatar tag, e.g. (gravatar: joe.blogs@example.com)
*/
function gravatar($params) {

    $email = $params['gravatar'];

    $size = (isset($params['size'])) ? $params['size'] : 50;

    $url = sprintf('http://www.gravatar.com/avatar/%s?size=%d', md5($email), $size);

    return sprintf('<img src="%s" width="%d" height="%d" />', $url, $size, $size);

}

You’d use this as follows:

您将按以下方式使用它:

(gravatar: joe.bloggs@example.com size: 64)

Note that the email attribute here is named gravatar; i.e., the same as the tag.

请注意,此处的电子邮件属性名为gravatar; 即与标签相同。

外挂程式 (Plugins)

There are a number of plugins available on Github including support for Twitter, Instagram and Github, as well as things like contact forms, estimating reading time and displaying related pages.

Github上有许多插件可用,包括对TwitterInstagramGithub的支持 ,以及联系方式 ,估计阅读时间和显示相关页面之类的东西

You can create your own plugins with little to no knowledge of Kirby’s inner workings – simply drop a class into site/plugins and use it from your templates as required.

您几乎不了解Kirby的内部工作原理就可以创建自己的插件-只需将类放到site/plugins ,然后根据需要从模板中使用它即可。

柯比小组 (Kirby Panel)

Kirby doesn’t actually provide any sort of administrative interface out-the-box – you simply edit the files directly. However, there is a simple interface available as a separate package, called Kirby Panel.

Kirby实际上并没有提供任何现成的管理界面,您只需直接编辑文件即可。 但是,有一个简单的界面作为单独的软件包提供,称为Kirby Panel。

The Kirby Panel

To install Kirby Panel, clone it from Github.

要安装Kirby Panel,请从Github克隆它

Then, copy the folder to the site folder of your Kirby site, naming it panel, e.g.:

然后,将该文件夹复制到您的Kirby站点的site文件夹,命名为panel ,例如:

cp -R kirbycms-panel/ /var/www/yoursite/site/panel

Now copy the panel\defaults folder and place it in the site root:

现在,复制panel\defaults文件夹并将其放置在站点根目录中:

cp -R /var/www/yoursite/panel/defaults /var/www/yoursite/panel

To specify some login credentials, rename the file site/panel/accounts/admin.php to [username].php, e.g.:

要指定一些登录凭据,请将文件site/panel/accounts/admin.php重命名为[username].php ,例如:

// site/panel/accounts/john.php

<?php if(!defined('KIRBY')) exit ?>
username: john
password: 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
encrypt: sha1
language: en

You can enter a plaintext password (a very, very bad idea), an MD5 hash (a very bad idea) or an SHA1 hash (marginally better) – you just need to explicitly specify which you’re using, or leave it out for plaintext.

您可以输入纯文本密码(一个非常非常糟糕的主意),一个MD5哈希(一个非常糟糕的主意)或一个SHA1哈希(稍微更好的主意)–您只需要明确指定要使用的密码,或将其保留纯文本。

Now, if you open http://yoursite.local/panel in your web browser, you’ll have access to a simple administrative interface. This allows you to modify site properties, as well as add / edit / delete content.

现在,如果您在网络浏览器中打开http://yoursite.local/panel ,则可以访问简单的管理界面。 这使您可以修改站点属性,以及添加/编辑/删除内容。

利弊 (Pros and Cons)

Let’s look briefly at some of Kirby’s pros and cons.

让我们简要地看一下柯比的一些优缺点。

优点 (Pros)
  • Extremely simple to install

    安装极其简单
  • Fast

    快速
  • Results in clean markup

    产生清晰的标记
  • Very shallow learning curve

    学习曲线很浅
  • Very little bloat

    很少肿胀
缺点 (Cons)
  • Some of the code tries to reinvent the wheel, for example the device detection

    一些代码尝试重新发明轮子,例如设备检测
  • Everything sits in the public root, which isn’t particularly logical

    一切都位于公共根,这不是特别合乎逻辑的
  • Dynamic elements such as search are limited at best

    诸如搜索之类的动态元素充其量是有限的

摘要 (Summary)

I’ve given you a brief introduction of Kirby, but there’s plenty more to explore – check out the documentation and tutorials, find support in the forums, or delve into the source-code. In particular, take a look at kirby/lib/kirby.php, along with the other files in kirby/lib.

我为您简要介绍了Kirby,但还有更多值得探索的地方-查看文档教程在论坛中找到支持,或深入研究源代码。 特别要看一下kirby/lib/kirby.php以及kirby/lib的其他文件。

What do you think of Kirby, and what might you use it for? Have you already used it in production? Let me know in the comments.

您如何看待柯比,您可能会用它做什么? 您已经在生产中使用它了吗? 在评论中让我知道。

翻译自: https://www.sitepoint.com/introduction-kirby-cms/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值