BootPress Blog Component

平面文件博客和CMS,不吝啬功能,可以在任何网站上实现。(A flat file Blog and CMS that doesn't skimp on features, and can be implemented in any website.)

  • 所有者: Kylob/Blog
  • 平台: BSD, Linux, Mac, Windows
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

use BootPress\Blog\Component as Blog;

平面文件博客和CMS不会吝啬功能,并且可以在任何网站中使用。

安装

将以下内容添加到您的`composer.json 文件中。

{
    "require": {
        "bootpress/blog": "^1.0"
    }
}

在网站的公共根文件夹中创建一个.htaccess 文件,以将不存在的所有内容重定向到index.php 文件。

# Prevent directory browsing
Options All -Indexes
# Turn on URL re-writing (remove 'example.com/' if not on localhost)
RewriteEngine On
RewriteBase /example.com/
# If the file exists, then that's all folks
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .+ - [L]
# For everything else, there's BootPress
RewriteRule ^(.*)$ index.php [L]

您的 index.php 文件应该看起来像这样:

<?php
use BootPress\Page\Component as Page;
use BootPress\Blog\Component as Blog;
use BootPress\Asset\Component as Asset;
use BootPress\Sitemap\Component as Sitemap;
$autoloader = require '../vendor/autoload.php';
// Setup the page
$page = Page::html(array(
    'dir' => '../page', // a private (root) directory
    'base' => 'http://localhost/example.com',
    'suffix' => '.html',
));
$html = '';
// Deliver sitemap and assets first
if ($asset = Asset::cached('assets')) {
    $page->send($asset);
} elseif ($xml = Sitemap::page()) {
    $page->send($xml);
}
// Implement a blog
$blog = new Blog();
if (false !== $file = $blog->page()) {
    if (is_array($file)) { // An 'index.html.twig' file
        $html = $blog->theme->renderTwig($file);
    } else { // A 'txt', 'json', 'xml', 'rdf', 'rss', or 'atom' page
        $page->send(Asset::dispatch($page->url['format'], $file));
    }
} else {
    $page->send(404);
}
// Create the layout
$html = $page->display($blog->theme->layout($html));
// Send to user
$page->send(Asset::dispatch('html', $html));

设置博客

使用以下信息创建 ../page/blog/config.yml 文件:

 blog:
    name: Example # The name of your website
    image: logo.png # The main image relative to this directory
    listings: blog # The url base for all your listing pages - authors, archives, tags, etc.
    breadcrumb: Blog # How to reference the listings in your breadcrumbs array
    theme: default # The main theme for your site

您可以在Twig模板中访问其中任何一个,例如:{{blog.name}},包括您所在的{{blog.page}}。 最终,这个文件将包含您可以轻松管理的作者、类别和标签。 您可以通过以下方式创建Bootstrap列表组类别:

<ul class="list-group">
{% for category in blog.query('categories') %}
    <li class="list-group-item">
        <span class="badge">{{ category.count }}</span>
        <a href="{{ category.url }}">{{ category.name }}</a>
        {# if category.subs #}
    </li>
{% endfor %}
</ul>

其他{{ blog.query(...) }}包括'tags','authors','archives','recent','featured','similar','posts'和[...] 各种清单,也称为“环路”。

创建内容

BootPress博客是一个平面文件CMS,这意味着您不需要任何花哨的管理界面来管理所有的内容是通过数据库分散的。您只需创建文件。您所有的博客帖子和网页都将驻留在 ../page/blog/content/ 目录中,如果您查看网址,则可以直接关注文件夹到您的index.html.twig 文件。例如:

URL File
/ blog/content/index.html.twig
/feed.rss blog/content/feed.rss.twig
/about-me.html blog/content/about-me/index.html.twig
/category/post.html blog/content/category/post/index.html.twig
/category/subcategory/long-title.html blog/content/category/subcategory/long-title/index.html.twig

为什么不将' /about-me.html '网址文件放在' content/about-me.html.twig '而不是'内容/about-me/index.html.twig '来代替吧?这样你就可以拥有你想要使用的所有资产,就在你想使用它们的地方。链接到他们甚至更容易。在' content/about-me/'文件夹中放置' image.jpg ',并链接至`{{'image.jpg'| asset} '** index.html.twig **'文件中的} 。你想调整它吗?试试{{ 'image.jpg?w=300'|asset }}。要查看所有选项,请查看快速参考“滑翔”

根据上面的 /feed.rss URL示例访问非HTML文件。

Twig模板

每个 index.html.twig 文件都是一个Twig模板,它接收 BootPress页面组件,以便您可以与HTML页面进行交互。您可以使用的方法是:

  • {{page.set()}} - 设置HTML页面属性。 标题,关键字(标签),作者等等
  • {{page.url()}} - 创建一个url,或者操纵它的查询字符串和片段。
  • {{page.get()}} - 访问$_GET参数。
  • {{page.post()}} - 访问$_POST参数。
  • {{page.tag()}} - 以编程方式生成HTML标记。
  • {{page.meta()}} - 将标记插入页面的部分。
  • {{page.link()}} - 在您的页面中包含js,css,ico等链接。
  • {{page.style()}} - 将CSS <style>格式添加到页面的<head>
  • {{page.script()}} - 将Java <script>代码添加到页面底部。
  • {{ page.jquery() }} - 把一些jQuery放入你的$(document).ready(function(){...})
  • {{page.id()}} - 获取一个唯一的id以在您的CSS或JavaScript中引用。

您每次使用的主要是 {{page.set()}} 就像这样:

 {{ page.set({
    title: 'A Flowery Post',
    description: 'Aren\'t they beautiful?',
    keywords: 'flowers, nature',
    image: 'flowers.jpg',
    published: 'January 1, 2015'
}) }}

您可以设置(和检索)的Page属性是:

  • ' title ' - 页面<title>
  • ' description ' - 此网页的元描述。
  • '关键字' - 用于标记博客文章的关键字的逗号分隔列表。
  • '机器人' - 如果设为false,则我们不会将此网页映射到网站,并且机器人将被告知要离开。
  • '主题' - 要使用其他主题,则使用默认使用的主题。
  • '图像' - 此页面的主要图像(如果有)。
  • '作者' - 发布作者的姓名。
  • '精选' - 如果设置为true,则会在所有其他帖子之前显示。否则称为“粘性帖子”。
  • '已发布' - 日期(例如`'Jan 1,2015' )如果这是发布或 <代码> true 如果它是一个页面。如果false `(默认),那么我们认为它是未发布的,并且不会告诉任何人。如果日期在将来,那么我们将等到发布之前。
  • ...以及您想要稍后设置和检索的任何其他值。以上对我们具有特殊意义。

为了让事情更简单,您可以将所有信息放入YAML中格式在页面顶部的Twig注释中。例如:

{#
title: A Flowery Post
description: Aren't they beautiful?
keywords: flowers, nature
image: flowers.jpg
published: January 1, 2015
#}
{% markdown %}
These are my flowers:
<img src="{{ 'flowers.jpg'|asset }}">
Aren't they ***beautiful***?
{% endmarkdown %}

当您检查if ($file = $blog->page()) { ... } 时,我们会查找相应的Twig模板,如果它存在,你的$file将是一个字符串,如果 if $page->url['format'] != 'html',或者一个适合于使用以下按键传递给$blog->theme->renderTwig($file)

  • 'file' - 适用于处理' vars '的type'的适当的Twig模板。
  • 'type' - 您正在使用的博客页面。无论是'page''post''category''index''archives''authors''tags'
  • 'vars' - 用于要使用的树枝模板。
  • 'default'=&gt;替代的文件,以便在主题中缺失时使用。

我们不会自动$page->send()它,这样您就有机会事先记录或缓存输出。现在你有你的博客信息,你可以用它做任何你想做的事情。您可以在任何项目中实施BootPress博客。它和弹性一样灵活,但如果你喜欢我们迄今为止的做法,那我们就继续吧?

主题

BootPress主题位于您的../page/blog/themes/文件夹中。假设你已经选择了'default ',当你$html = $blog->theme->renderTwig($template) 时,它会将$template['vars']传递给在'../page/blog/themes/default/'文件夹中的 $template['file'],并返回你的 $html。如果 $template['file']不存在,那么将为您提供默认值。如果您在任何时候想知道您需要使用什么 $template['vars'] ,只需{{dump()}}他们,他们将全部拼出来。

当您$blog->theme->layout($html),它会将$html {{ content }} 传递给你 ../page/blog/themes/default/index.html.twig '文件,它可能看起来像这样:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{{ page.title }}</title>
    <link rel="stylesheet" href="{{ 'css/bootstrap.css'|asset }}">
    <!--[if lte IE 8]><script src="{{ 'js/html5shiv.js'|asset }}"></script><![endif]-->
</head>
<body>
    <!-- Content -->
    <div id="content">
        {{ content }}
    </div>
    <!-- Sidebar -->
    <div id="sidebar">
        {% include '@theme/sidebar.html.twig' %} 
    </div>
    <!-- Scripts -->
    <script src="{{ 'js/jquery.js'|asset }}"></script>
    <script src="{{ 'js/bootstrap.js'|asset }}"></script>
</body>
</html>

插件

插件是位于`../page/blog /目录中的 Twig宏。插件/ 文件夹,并且很容易通过{%import'@ plugin/name'作为名称%} 访问任何模板。我的建议是遵循'vendor/package'的packagist命名模式,其主文件是'macro.twig'。例如,如果您将以下内容放在../page/blog/plugins/kylob/mailto/macro.twig 中:

{% macro eval(string) %}
    {% set js = '' %}
    {% set string = 'document.write(' ~ json_encode(string) ~ ');' %}
    {% for i in range(0, string|length - 1) %}
        {% set js = js ~ '%' ~ bin2hex(string|slice(i, 1)) %}
    {% endfor %}
    <script type="text/javascript">eval(unescape('{{ js }}'))</script>
{% endmacro eval %}

然后,您可以像垃圾邮件机器人一样隐藏电子邮件地址:

{% import '@plugin/kylob/mailto/macro.twig' as mailto %}
{{ mailto.eval('<a href="mailto:me@example.com">Contact Me</a>') }}

这将导致:

 <script type="text/javascript">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%22%3c%61%20%68%72%65%66%3d%5c%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%5c%22%3e%43%6f%6e%74%61%63%74%20%4d%65%3c%5c%2f%61%3e%22%29%3b'))</script>

您可以通过将 {{ this(_self, 'key', 'value') }} 设置为一个名称空间中的宏(位于同一页)宏,并访问另一个{{ this(_self, 'key') }} 。这允许你创建“属性”,这样你的宏插件可以更像“类”。您也可以使用几乎所有原生的php功能,这些功能可以被认为是安全的。

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

概覽

名稱與所有者Kylob/Blog
主編程語言PHP
編程語言PHP (語言數: 2)
平台BSD, Linux, Mac, Windows
許可證MIT License
發布數5
最新版本名稱v1.4 (發布於 )
第一版名稱v1.0 (發布於 )
創建於2016-10-07 22:43:41
推送於2017-01-19 07:17:55
最后一次提交2017-01-18 22:17:54
星數16
關注者數4
派生數4
提交數52
已啟用問題?
問題數0
打開的問題數0
拉請求數6
打開的拉請求數0
關閉的拉請求數0
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

use BootPress\Blog\Component as Blog;

Packagist
License MIT
HHVM Tested
PHP 7 Supported
Build Status
Code Climate
Test Coverage

A flat file Blog and CMS that doesn't skimp on features, and can be utilized in any website.

Installation

Add the following to your composer.json file.

{
    "require": {
        "bootpress/blog": "^1.0"
    }
}

Create an .htaccess file in your website's public root folder to redirect everything that doesn't exist to an index.php file.

# Prevent directory browsing
Options All -Indexes

# Turn on URL re-writing (remove 'example.com/' if not on localhost)
RewriteEngine On
RewriteBase /example.com/

# If the file exists, then that's all folks
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .+ - [L]

# For everything else, there's BootPress
RewriteRule ^(.*)$ index.php [L]

Your index.php file should then look something like this:

<?php

use BootPress\Page\Component as Page;
use BootPress\Blog\Component as Blog;
use BootPress\Asset\Component as Asset;
use BootPress\Sitemap\Component as Sitemap;

$autoloader = require '../vendor/autoload.php';

// Setup the page
$page = Page::html(array(
    'dir' => '../page', // a private (root) directory
    'base' => 'http://localhost/example.com',
    'suffix' => '.html',
));
$html = '';

// Deliver sitemap and assets first
if ($asset = Asset::cached('assets')) {
    $page->send($asset);
} elseif ($xml = Sitemap::page()) {
    $page->send($xml);
}

// Implement a blog
$blog = new Blog();
if (false !== $file = $blog->page()) {
    if (is_array($file)) { // An 'index.html.twig' file
        $html = $blog->theme->renderTwig($file);
    } else { // A 'txt', 'json', 'xml', 'rdf', 'rss', or 'atom' page
        $page->send(Asset::dispatch($page->url['format'], $file));
    }
} else {
    $page->send(404);
}

// Create the layout
$html = $page->display($blog->theme->layout($html));

// Send to user
$page->send(Asset::dispatch('html', $html));

Setup Blog

Create a ../page/blog/config.yml file with the following information:

blog:
    name: Example # The name of your website
    image: logo.png # The main image relative to this directory
    listings: blog # The url base for all your listing pages - authors, archives, tags, etc.
    breadcrumb: Blog # How to reference the listings in your breadcrumbs array
    theme: default # The main theme for your site

You can access any of these in your Twig templates eg. {{ blog.name }}, including the {{ blog.page }} you are on. Eventually this file will be full of authors, categories, and tags that you can easily manage as well. You can create a Bootstrap list group of categories by:

<ul class="list-group">
{% for category in blog.query('categories') %}
    <li class="list-group-item">
        <span class="badge">{{ category.count }}</span>
        <a href="{{ category.url }}">{{ category.name }}</a>
        {# if category.subs #}
    </li>
{% endfor %}
</ul>

Other {{ blog.query(...) }}'s include 'tags', 'authors', 'archives', 'recent', 'featured', 'similar', 'posts', and [...] listings of every sort, otherwise known as "The Loop".

Create Content

A BootPress Blog is a flat-file CMS, which means you don't need any fancy admin interface to manage all of the content that is scattered througout a database. You simply create files. All of your blog's posts and pages will reside in the ../page/blog/content/ directory, and if you look at a URL, you will be able to follow the folders straight to your index.html.twig file. For example:, URL, File, -------------------------------------, ------------------------------------------------------------, /, blog/content/index.html.twig, /feed.rss, blog/content/feed.rss.twig, /about-me.html, blog/content/about-me/index.html.twig, /category/post.html, blog/content/category/post/index.html.twig, /category/subcategory/long-title.html, blog/content/category/subcategory/long-title/index.html.twig, Why not have the '/about-me.html' URL file at 'content/about-me.html.twig' instead of 'content/about-me/index.html.twig' instead, right? This is so you can have all of the assets that you want to use, right there where you want to use them. Linking to them is even easier. Place an 'image.jpg' in the 'content/about-me/' folder, and link to {{ 'image.jpg', asset }} in the 'index.html.twig' file. Would you like to resize that? Try an {{ 'image.jpg?w=300', asset }}. To see all the options, check out the Quick Reference "Glide".

Non-HTML files are accessed according to the '/feed.rss' URL example above.

Twig Templates

Every index.html.twig file is a Twig template that receives the BootPress Page Component, so that you can interact with your HTML Page. The methods available to you are:

  • {{ page.set() }} - Set HTML Page properties. Things like the title, keywords (tags), author, etc.
  • {{ page.url() }} - Either create a url, or manipulate it's query string and fragment.
  • {{ page.get() }} - Access $_GET parameters.
  • {{ page.post() }} - Access $_POST parameters.
  • {{ page.tag() }} - Generate an HTML tag programatically.
  • {{ page.meta() }} - Insert <meta> tag(s) into the <head> section of your page.
  • {{ page.link() }} - Include js, css, ico, etc links in your page.
  • {{ page.style() }} - Add CSS <style> formatting to the <head> of your page.
  • {{ page.script() }} - Add Java<script> code to the bottom of your page.
  • {{ page.jquery() }} - Put some jQuery into your $(document).ready(function(){...}).
  • {{ page.id() }} - Get a unique id to reference in your CSS or JavaScript.

The main one you will use everytime is {{ page.set() }} like so:

{{ page.set({
    title: 'A Flowery Post',
    description: 'Aren\'t they beautiful?',
    keywords: 'flowers, nature',
    image: 'flowers.jpg',
    published: 'January 1, 2015'
}) }}

The Page properties you can set (and retrieve) are:

  • 'title' - The page <title>.
  • 'description' - The meta description of this page.
  • 'keywords' - A comma-separated list of keywords for tagging your blog posts.
  • 'robots' - If set to false then we will not sitemap this page, and the robots will be told to go away.
  • 'theme' - To use a different theme then the one used by default.
  • 'image' - The main image for this page (if any).
  • 'author' - The post author's name.
  • 'featured' - If set to true then it will be displayed before all other posts. Otherwise known as a "sticky post".
  • 'published' - A date (eg. 'Jan 1, 2015') if this is a post, or true if it is a page. If false (the default) then we consider it unpublished and won't tell anyone. If a date is in the future then we will wait until then before publishing.
  • ... and any other value that you want to set and retrieve later on. The above just have special meanings to us.

To make things even easier, you can put all that information in YAML format within a Twig comment at the top of the page. For example:

{#
title: A Flowery Post
description: Aren't they beautiful?
keywords: flowers, nature
image: flowers.jpg
published: January 1, 2015
#}

{% markdown %}

These are my flowers:

<img src="{{ 'flowers.jpg', asset }}">

Aren't they ***beautiful***?

{% endmarkdown %}

When you check if ($file = $blog->page()) { ... } we will look for the corresponding Twig template, and if it is there, your $file will either be a string if $page->url['format'] != 'html', or an array suitable for passing to $blog->theme->renderTwig($file) with the following keys:

  • 'file' - The appropriate Twig template that is equipped to deal with these 'type' of 'vars'.
  • 'type' - The kind of Blog page you are working with. Either 'page', 'post', 'category', 'index', 'archives', 'authors', or 'tags'.
  • 'vars' - For the Twig template to utilize.
  • 'default' => An alternate 'file' to use if it's missing in your theme.

We don't automatically $page->send() it, so that you can have the opportunity to log or cache the output beforehand. Now you have your blog info, and you can do anything you want with it. You can implement a BootPress Blog into any project. It is as flexible as flexible can be, but if you like the way we do things so far, then let's continue shall we?

Themes

BootPress Themes live in your ../page/blog/themes/ folder. Assuming you have selected the 'default', when you $html = $blog->theme->renderTwig($template), it will pass the $template['vars'] to the $template['file'] in the '../page/blog/themes/default/' folder, and return your $html. If the $template['file'] does not exist, then a default one will be provided for you. If at any time you are wondering what $template['vars'] you have to work with, just {{ dump() }} them, and they will be all spelled out for you.

When you $blog->theme->layout($html), it will pass the $html {{ content }} to your '../page/blog/themes/default/index.html.twig' file which could look something like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    
    <title>{{ page.title }}</title>
    
    <link rel="stylesheet" href="{{ 'css/bootstrap.css', asset }}">
    <!--[if lte IE 8]><script src="{{ 'js/html5shiv.js', asset }}"></script><![endif]-->
</head>
<body>

    <!-- Content -->
    <div id="content">
        {{ content }}
    </div>

    <!-- Sidebar -->
    <div id="sidebar">
        {% include '@theme/sidebar.html.twig' %} 
    </div>

    <!-- Scripts -->
    <script src="{{ 'js/jquery.js', asset }}"></script>
    <script src="{{ 'js/bootstrap.js', asset }}"></script>
</body>
</html>

Plugins

Plugins are Twig macros that reside in your ../page/blog/plugins/ folder, and are easily accessed in any template via {% import '@plugin/name' as name %}. My recommendation is to follow the packagist naming schema of 'vendor/package' with the main file being 'macro.twig'. For example, if you put the following at ../page/blog/plugins/kylob/mailto/macro.twig:

{% macro eval(string) %}

    {% set js = '' %}
    {% set string = 'document.write(' ~ json_encode(string) ~ ');' %}
    {% for i in range(0, string, length - 1) %}
        {% set js = js ~ '%' ~ bin2hex(string, slice(i, 1)) %}
    {% endfor %}
    <script type="text/javascript">eval(unescape('{{ js }}'))</script>
    
{% endmacro eval %}

You could then hide an email address from spam bots like so:

{% import '@plugin/kylob/mailto/macro.twig' as mailto %}

{{ mailto.eval('<a href="mailto:me@example.com">Contact Me</a>') }}

Which would result in:

<script type="text/javascript">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%22%3c%61%20%68%72%65%66%3d%5c%22%6d%61%69%6c%74%6f%3a%6d%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%5c%22%3e%43%6f%6e%74%61%63%74%20%4d%65%3c%5c%2f%61%3e%22%29%3b'))</script>

You can pass variables among macros in the same namespace (on the same page) by setting {{ this(_self, 'key', 'value') }} in one macro, and accessing {{ this(_self, 'key') }} in another. This allows you to create "properties" so that your macro plugins can behave a little more like "classes". You can also use nearly every native php function that would be considered safe to use.

License

The MIT License (MIT). Please see License File for more information.

去到頂部