Typecho 模板制作之 index.php

yibin 2015-04-24 Typecho 859

制作index.php

模板作者信息

在index.php的开头,可以使用注释方式添加模板作者信息。

/**
 * 这是一段描述,写在前面
 * @package nhstu-style
 * @author nhstu
 * @version 1.0
 * @link http://www.nhstu.com
 */

那么在typecho后台,那么可以看到详细的作者信息

引用页面片

直接使用$this->need()函数引用,譬如:

<?php $this->need('inc/header.php');?>

那么将会在index.php中插入inc/header.php的内容,相当于require。

/**
 * 获取主题文件
 *
 * @access public
 * @param string $fileName 主题文件
 * @return void
 */
public function need($fileName)
{
    require $this->_themeDir . $fileName;
}

文章内容

先来个全景:

<div class="main">
    <?php if($this->have()):?>
        <?php while($this->next()): ?>
        <article>
            <p class="title"><a href="<?php $this->permalink() ?>" target="_blank"><?php $this->title() ?></a></p>
            <span class="date">发布于:<?php $this->date('y-m-d'); ?></span>
            <span class="author">来自<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a></span>
            <div class="category"><?php $this->category(','); ?></div>
            <div class="content"><?php $this->content('阅读全文 >>'); ?></div>
        </article>
        <?php endwhile; ?>
    <?php else:?>
        <div>暂无文章</div>
    <?php endif?>
    <?php $this->pageNav('<< 上一页', '下一页 >>'); ?>
</div>
  • $this->have() 判断是否有文章输出
  • $this->next() 迭代到下一篇文章
  • $this->permalink() 输出文章的链接
  • $this->title() 输出文章标题
  • $this->category() 输出分类信息。参数表示分隔符。
  • $this->content() 输出文章内容
  • $this->expect(200) 输出文章摘要,参数200表示输出文章的前200字符
  • $this->author() 输出作者名称
  • $this->author->permalink() 输出作者的链接
如需要更加详细的字段说明,请参阅Typecho模板中的Archive.php》

分页信息

使用$this->pageNav()输出标准的分页html。

pageNav的定义为:

/**
 * 输出分页
 *
 * @access public
 * @param string $prev 上一页文字
 * @param string $next 下一页文字
 * @param int $splitPage 分割范围
 * @param string $splitWord 分割字符
 * @param string $template 展现配置信息
 * @return void
 */
public function pageNav($prev = '&laquo;', $next = '&raquo;', $splitPage = 3, $splitWord = '...', $template = '')
{
... ...
}
很多人怀疑,使用标准的分页输出,不利于个性化。但实际上,使用标准的html输出,对模板制作非常有利,减少学习成本,提高模板效率。

附上完整的例子参考:

<?php
    /**
     * 这是一段描述,写在前面
     * @package nhstu-style
     * @author nhstu
     * @version 1.0
     * @link http://www.nhstu.com
     */
$this->need('inc/header.php');
?>
    <div class="main">
        <?php if($this->have()):?>
            <?php while($this->next()): ?>
            <article>
                <p class="title"><a href="<?php $this->permalink() ?>" target="_blank"><?php $this->title() ?></a></p>
                <span class="date">发布于:<?php $this->date('y-m-d'); ?></span>
                <span class="author">来自<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a></span>
                <div class="category"><?php $this->category(','); ?></div>
                <div class="content"><?php $this->content('阅读全文 >>'); ?></div>
            </article>
            <?php endwhile; ?>
        <?php else:?>
            <div>暂无文章</div>
        <?php endif?>
        <?php $this->pageNav('<< 上一页', '下一页 >>'); ?>
    </div>
<?php
$this->need('inc/footer.php');

扫码添加微信

13013082126 扫描微信 建站咨询 优化咨询