优雅地自定义遍历输出文章
背景介绍
看了不下十个主题的代码,一个通病,就是控制代码和模板混写在一起。如下图
这么写的问题是什么?
当输出结构比较复杂的时候,html会变得非常难以修改。
typecho自己是如何做的
作为一个typecho新手,很好奇为什么系统内置的文章输出就可以如此优雅。一个while($this->next())实现文章遍历,模板随意修改,数据控制代码也随意修改,互不干涉。
以下是所有模板中都用到的,输出文章列表的代码。
<?php while($this->next()): ?>
<article class="post_summary">
<h3 class="title"><a target="_blank" itemtype="url" href="<?php $this->permalink() ?>"><?php $this->title() ?></a></h3>
<div class="row">
<div class="col-lg-3">
<a href="<?php $this->permalink() ...