37个常用Halo Java轻量博客页面模板标签调用(快速调用页面数据)

这篇文章应该算是Halo Java轻量博客程序的最后一篇主题开发的最后篇文章。在前面老蒋有整理到Halo页面调用、公共模板、全局变量的调用。这里我们是不是有需要页面模板标签的调用,比如最新文章、菜单、图片组的调用,这篇文章是比较全的37个Halo页面模板标签调用整理。

37个常用Halo Java轻量博客页面模板标签调用(快速调用页面数据) - 第1张

模板标签可以运用在页面的任何地方。

第一、文章(postTag)

1、获取最新文章(latest)

<@postTag method="latest" top="3">
    <#list posts as post>
        <a href="${post.fullPath!}">${post.title!}</a>
    </#list>
</@postTag>

2、获取所有文章的数量(count)

<@postTag method="count">
<span>文章数量:${count!0}</span>
</@postTag>

3、根据年份归档(archiveYear)

<@postTag method="archiveYear">
  <#list archives as archive>
      <h1>年份: ${archive.year?c}</h1>
      <ul>
          <#list archive.posts?sort_by("createTime")?reverse as post>
            <li>
              <a href="${post.fullPath!}">${post.title!}</a>
            </li>
          </#list>
      </ul>
  </#list>
</@postTag>

4、根据年月归档(archiveMonth)

<@postTag method="archiveMonth">
  <#list archives as archive>
      <h1>${archive.year?c}-${archive.month?c}</h1>
      <ul>
          <#list archive.posts?sort_by("createTime")?reverse as post>
            <li>
              <a href="${post.fullPath!}">${post.title!}</a>
            </li>
          </#list>
      </ul>
  </#list>
</@postTag>

5、归档(archive)

<@postTag method="archive" type="month">
  <#list archives as archive>
      <h1>${archive.year?c}-${archive.month?c}</h1>
      <ul>
          <#list archive.posts?sort_by("createTime")?reverse as post>
            <li>
              <a href="${post.fullPath!}">${post.title!}</a>
            </li>
          </#list>
      </ul>
  </#list>
</@postTag>

6、根据分类 id 获取文章(listByCategoryId)

<@postTag method="listByCategoryId" top="${category.id?c}">
    <span>分类 ${category.name!} 下的文章:</span>
    <#list posts as post>
        <a href="${post.fullPath!}">${post.title!}</a>
    </#list>
</@postTag>

7、根据分类 slug 获取文章(listByCategorySlug)

<@postTag method="listByCategorySlug" categorySlug="${category.slug!}">
    <span>分类 ${category.name!} 下的文章:</span>
    <#list posts as post>
        <a href="${post.fullPath!}">${post.title!}</a>
    </#list>
</@postTag>

8、根据标签 id 获取文章(listByTagId)

<@postTag method="listByTagId" tagId="${tag.id?c}">
    <span>标签 ${tag.name!} 下的文章:</span>
    <#list posts as post>
        <a href="${post.fullPath!}">${post.title!}</a>
    </#list>
</@postTag>

9、根据标签 slug 获取文章(listByTagSlug)

<@postTag method="listByTagSlug" tagSlug="${tag.slug!}">
    <span>标签 ${tag.name!} 下的文章:</span>
    <#list posts as post>
        <a href="${post.fullPath!}">${post.title!}</a>
    </#list>
</@postTag>

第二、评论(commentTag)

1、获取最新评论(latest)

<@commentTag method="latest" top="获取条数">
    <ul>
        <#list comments.content as comment>
            <li>${comment.author!}:${comment.content!}</li>
        </#list>
    </ul>
</@commentTag>

2、获取所有评论的数量(count)

<@commentTag method="count">
<span>评论数量:${count!0}</span>
</@commentTag>

第三、分类目录(categoryTag)

1、获取所有分类目录(list)

<@categoryTag method="list">
  <#list categories as category>
    <a href="${category.fullPath!}">${category.name!}(${category.postCount!})</a>
  </#list>
</@categoryTag>

2、获取文章的所有分类(listByPostId)

<@categoryTag method="listByPostId" postId="${post.id?c}">
  <#list categories as category>
    <a href="${category.fullPath!}">${category.name}</a>
  </#list>
</@categoryTag>

3、获取所有分类的数量(count)

<@categoryTag method="count">
<span>分类数量:${count!0}</span>
</@categoryTag>

第四、标签(tagTag)

1、获取所有标签(list)

<@tagTag method="list">
  <#list tags as tag>
    <a href="${tag.fullPath!}">${tag.name!}(${tag.postCount!})</a>
  </#list>
</@tagTag>

2、获取文章的所有标签(listByPostId)

<@tagTag method="listByPostId" postId="${post.id?c}">
  <#list tags as tag>
    <a href="${tag.fullPath!}">${tag.name}</a>
  </#list>
</@tagTag>

3、获取所有标签的数量(count)

<@tagTag method="count">
<span>标签数量:${count!0}</span>
</@tagTag>

第五、菜单(menuTag)

1、获取所有菜单(list)

<@menuTag method="list">
  <ul>
    <#list menus as menu>
      <li>
        <a href="${menu.url!}" target="${menu.target!}">${menu.name!}</a>
      </li>
    </#list>
  </ul>
</@menuTag>

2、获取多级菜单(tree)

<@menuTag method="tree">
  <ul>
    <#list menus as menu>
      <li>
        <a href="${menu.url!}" target="${menu.target!}">${menu.name!}</a>
        <#if menu.children?? && menu.children?size gt 0>
            <ul>
              <#list menu.children as child>
                <li>
                  <a href="${child.url!}" target="${menu.target!}">${child.name!}</a>
                </li>
              </#list>
            </ul>
        </#if>
      </li>
    </#list>
  </ul>
</@menuTag>

3、根据分组获取菜单(listByTeam)

<@menuTag method="listByTeam" team="main">
  <ul>
    <#list menus as menu>
      <li>
        <a href="${menu.url!}" target="${menu.target!}">${menu.name!}</a>
      </li>
    </#list>
  </ul>
</@menuTag>

4、根据分组获取多级菜单(treeByTeam)

<@menuTag method="treeByTeam" team="main">
  <ul>
    <#list menus as menu>
      <li>
        <a href="${menu.url!}" target="${menu.target!}">${menu.name!}</a>
        <#if menu.children?? && menu.children?size gt 0>
            <ul>
              <#list menu.children as child>
                <li>
                  <a href="${child.url!}" target="${menu.target!}">${child.name!}</a>
                </li>
              </#list>
            </ul>
        </#if>
      </li>
    </#list>
  </ul>
</@menuTag>

第六、友情链接(linkTag)

1、获取所有友情链接(list)

<ul>
  <@linkTag method="list">
    <#list links as link>
        <li>
            <a href="${link.url!}" target="_blank">
                ${link.name!}
            </a>
        </li>
    </#list>
  </@linkTag>
</ul>

2、乱序获取所有友情链接(listByRandom)

<ul>
  <@linkTag method="list">
    <#list links as link>
        <li>
            <a href="${link.url!}" target="_blank">
                ${link.name!}
            </a>
        </li>
    </#list>
  </@linkTag>
</ul>

3、获取分组友情链接(listTeams)

<@linkTag method="listTeams">
  <#list teams as team>
    <h1>${team.team}</h1>
    <ul>
      <#list links as link>
        <li>
          <a href="${link.url!}" target="_blank">
          ${link.name!}
          </a>
        </li>
      </#list>
    </ul>
  </#list>
</@linkTag>

4、乱序获取分组友情链接(listTeamsByRandom)

<@linkTag method="listTeamsByRandom">
  <#list teams as team>
    <h1>${team.team}</h1>
    <ul>
      <#list links as link>
        <li>
          <a href="${link.url!}" target="_blank">
          ${link.name!}
          </a>
        </li>
      </#list>
    </ul>
  </#list>
</@linkTag>

5、获取所有友情链接的数量(count)

<@linkTag method="count">
<span>友情链接数量:${count!0}</span>
</@linkTag>

第七、图库(photoTag)

1、获取所有图片(list)

<@photoTag method="list">
    <#list photos as photo>
        <img alt="${photo.description}" src="${photo.url}"/>
    </#list>
</@photoTag>

2、获取所有分组图片(listTeams)

<@photoTag method="listTeams">
    <#list teams as team>
        <h1>${team.team}</h1>
    <#list team.photos as photo>
        <img alt="${photo.description}" src="${photo.url}"/>
    </#list>
    </#list>
</@photoTag>

3、根据分组获取图片(listByTeam)

<@photoTag method="listTeams" team="风景">
  <#list team.photos as photo>
    <img alt="${photo.description}" src="${photo.url}"/>
  </#list>
</@photoTag>

4、获取所有图片的数量(count)

<@linkTag method="count">
<span>图片数量:${count!0}</span>
</@linkTag>

第八、分页(paginationTag)

1、获取首页文章列表的分页数据(index)

<ul class="pagination">
    <@paginationTag method="index" page="${posts.number}" total="${posts.totalPages}" display="3">
        <#if pagination.hasPrev>
            <li>
                <a href="${pagination.prevPageFullPath!}">上一页</a>
            </li>
        </#if>
        <#list pagination.rainbowPages as number>
            <#if number.isCurrent>
                <li>
                    <span class="current">${number.page!}</span>
                </li>
            <#else>
                <li>
                    <a href="${number.fullPath!}">${number.page!}</a>
                </li>
            </#if>
        </#list>
        <#if pagination.hasNext>
            <li>
                <a href="${pagination.nextPageFullPath!}">下一页</a>
            </li>
        </#if>
    </@paginationTag>
</ul>

2、获取文章归档列表的分页数据(archives)

<ul class="pagination">
    <@paginationTag method="archives" page="${posts.number}" total="${posts.totalPages}" display="3">
        <#if pagination.hasPrev>
            <li>
                <a href="${pagination.prevPageFullPath!}">上一页</a>
            </li>
        </#if>
        <#list pagination.rainbowPages as number>
            <#if number.isCurrent>
                <li>
                    <span class="current">${number.page!}</span>
                </li>
            <#else>
                <li>
                    <a href="${number.fullPath!}">${number.page!}</a>
                </li>
            </#if>
        </#list>
        <#if pagination.hasNext>
            <li>
                <a href="${pagination.nextPageFullPath!}">下一页</a>
            </li>
        </#if>
    </@paginationTag>
</ul>

3、获取搜索结果文章列表的分页数据(search)

<ul class="pagination">
    <@paginationTag method="search" page="${posts.number}" total="${posts.totalPages}" keyword="${keyword}" display="3">
        <#if pagination.hasPrev>
            <li>
                <a href="${pagination.prevPageFullPath!}">上一页</a>
            </li>
        </#if>
        <#list pagination.rainbowPages as number>
            <#if number.isCurrent>
                <li>
                    <span class="current">${number.page!}</span>
                </li>
            <#else>
                <li>
                    <a href="${number.fullPath!}">${number.page!}</a>
                </li>
            </#if>
        </#list>
        <#if pagination.hasNext>
            <li>
                <a href="${pagination.nextPageFullPath!}">下一页</a>
            </li>
        </#if>
    </@paginationTag>
</ul>

4、获取标签下文章列表的分页数据(tagPosts)

<ul class="pagination">
    <@paginationTag method="tagPosts" slug="${tag.slug!}" page="${posts.number}" total="${posts.totalPages}" display="3">
        <#if pagination.hasPrev>
            <li>
                <a href="${pagination.prevPageFullPath!}">上一页</a>
            </li>
        </#if>
        <#list pagination.rainbowPages as number>
            <#if number.isCurrent>
                <li>
                    <span class="current">${number.page!}</span>
                </li>
            <#else>
                <li>
                    <a href="${number.fullPath!}">${number.page!}</a>
                </li>
            </#if>
        </#list>
        <#if pagination.hasNext>
            <li>
                <a href="${pagination.nextPageFullPath!}">下一页</a>
            </li>
        </#if>
    </@paginationTag>
</ul>

5、获取分类下文章列表的分页数据(categoryPosts)

<ul class="pagination">
    <@paginationTag method="categoryPosts" slug="${category.slug!}" page="${posts.number}" total="${posts.totalPages}" display="3">
        <#if pagination.hasPrev>
            <li>
                <a href="${pagination.prevPageFullPath!}">上一页</a>
            </li>
        </#if>
        <#list pagination.rainbowPages as number>
            <#if number.isCurrent>
                <li>
                    <span class="current">${number.page!}</span>
                </li>
            <#else>
                <li>
                    <a href="${number.fullPath!}">${number.page!}</a>
                </li>
            </#if>
        </#list>
        <#if pagination.hasNext>
            <li>
                <a href="${pagination.nextPageFullPath!}">下一页</a>
            </li>
        </#if>
    </@paginationTag>
</ul>

6、获取图库页面图片列表的分页数据(photos)

<ul class="pagination">
    <@paginationTag method="photos" page="${photos.number}" total="${photos.totalPages}" display="3">
        <#if pagination.hasPrev>
            <li>
                <a href="${pagination.prevPageFullPath!}">上一页</a>
            </li>
        </#if>
        <#list pagination.rainbowPages as number>
            <#if number.isCurrent>
                <li>
                    <span class="current">${number.page!}</span>
                </li>
            <#else>
                <li>
                    <a href="${number.fullPath!}">${number.page!}</a>
                </li>
            </#if>
        </#list>
        <#if pagination.hasNext>
            <li>
                <a href="${pagination.nextPageFullPath!}">下一页</a>
            </li>
        </#if>
    </@paginationTag>
</ul>

7、获取日志页面日志列表的分页数据(journals)

<ul class="pagination">
    <@paginationTag method="journals" page="${journals.number}" total="${journals.totalPages}" display="3">
        <#if pagination.hasPrev>
            <li>
                <a href="${pagination.prevPageFullPath!}">上一页</a>
            </li>
        </#if>
        <#list pagination.rainbowPages as number>
            <#if number.isCurrent>
                <li>
                    <span class="current">${number.page!}</span>
                </li>
            <#else>
                <li>
                    <a href="${number.fullPath!}">${number.page!}</a>
                </li>
            </#if>
        </#list>
        <#if pagination.hasNext>
            <li>
                <a href="${pagination.nextPageFullPath!}">下一页</a>
            </li>
        </#if>
    </@paginationTag>
</ul>

这样,我们联合其他的Halo主题开发的页面调用标签、公共模板、函数等,基本上可以完成Halo主题开发需要的调用。

相关文章:

1、Halo博客主题开发之页面变量调用示范整理

2、Halo博客主题模板开发之公共宏模板代码规范

3、Halo博客主题开发之全局变量模板标签调用整理

 

投上你的一票

本文出处:老蒋部落 » 37个常用Halo Java轻量博客页面模板标签调用(快速调用页面数据) | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送