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)。有关更多信息,请参阅许可证文件。