创建密码保护的日志

FarBox本身并不提供单日志的密码保护,但是,基于模板API,我们可以实现这个功能!

来源Oyiadin, 他提供的模板源码地址。

step1, 在显示文章列表里,对输出进行控制,如果有设定密码的(通过post.metadata.password判定),则使用如下的代码(Oyiadin将其放在include/password_form.jade这个文件中调用):

form(method="post", action="{{ post.url }}")
    p
        | 这是一篇受密码保护的文章,请提供
        if post.metadata.question
            | 问题的答案
        else
            | 密码
        | 以继续查看。

    label(for="password")
        {{ post.metadata.question or "密码:" }}
    input#password(name="password")
    //- 如果设置 type=password 的话,无法输入中文。
    input(type="submit", value="提交")

step2, 在文章的详细页中,对输入密码是否正确进行判断。

if not post.metadata.password or request.form.password|string == post.metadata.password|string
    post.content
 else
    include include/password_form

    if request.form.password|string
        p
            strong 输入错误,请重试。

注意事项

收密码保护的文章,在其它模板中,如果没有对应的逻辑,则是非密保的。

另外,文章的status不能为public, 因为可以通过?action=show_json来查看public的日志各种属性。

比如Oyiadin是用这种方式获取posts变量的:get_data(type='post', status=['public', 'secret'])

最终效果如图

Post Password

  • d2014-03-15 08:44

    Emz, looks nice!!!

  • Hey Ⅰ am so thгilled I fοund your webpage, I
    really found you by mistake, while I was researchіng on Dіgg for something else, Anyways I am herᥱ now and would just
    like to say kudos for a tremendous post and a all round entᥱrtaining blog (I also love the
    theme/design), I don’t have time to read it all at the minute but I haᴠe
    book-maгkeԁ it and also added youг RSS feeds, so when I
    have time I will be bаck to read much more, Please do keep up the grᥱat work.

  • derm exclusive tv offer2017-03-30 18:27

    Ꮲretty nice post. I simply stumbled upon your weblog and wanteԁ to mentiоn that I've trulү loved browsing your weblog
    posts. After aⅼl I wilⅼ be subscribing for
    your feed and I'm hoping you write once more very soon!