EnovelCms变量函数列表

教程文章 2026年10月03日 EnovelCms官网

一、所有前端主题模板共用变量与函数

1. 共用变量

这些变量由 includes/Router.php 统一注入,几乎所有前端模板都可用。

变量说明
$current_lang当前语言代码,如 zh-cn、en-us
$siteLogo站点 Logo 地址
$userGold当前登录用户金币;未登录为 0
$seoSEO 数组:title、keywords、description
$lang语言对象,调用 $lang->get('key')
$db数据库对象,调用 $db->query()、$db->fetch()、$db->fetchAll()
$pageTitle当前页面标题,各 handler 返回
$seoPage当前 SEO 页面标识,如 home、library、read
$seoVariables当前页面 SEO 替换变量数组
$viewData所有注入变量的原始数组,一般不用

header.php、footer.php 还常用:

变量/常量说明
THEME_PATH当前主题绝对路径
THEME_URL当前主题 URL
ENOVELCMS_VERSION当前程序版本
$_SESSION['username']当前登录用户名
isLoggedIn()是否登录
isAuthor()当前用户是否作者

2. 共用函数

以下函数在主题模板中可直接调用,来自 includes/functions.php、includes/templates_functions.php 等。

2.1 常用基础函数

函数说明
h($str)HTML 转义输出
getSetting($key, $db)读取系统设置
getPointName()获取积分名称,如“金币”“书币”
isLoggedIn()是否登录
isAuthor($userId = null)是否作者
getActiveTheme()当前主题名
getThemeUrl()当前主题 URL
getThemePath()当前主题路径
input($key, $default, $method, $htmlspecialchars)安全接收请求参数
redirect($url)跳转
csrf_field()输出 CSRF 隐藏域
csrf_token()获取 CSRF token
verify_csrf_token($token)验证 CSRF token
verify_admin_csrf()后台 CSRF 验证
verify_captcha($input)验证验证码
clear_captcha()清除验证码
smartWordCount($content)智能统计字数
formatNumberShort($num)数字简写:千/万/亿
cleanPlainText($text)去除 HTML,清理纯文本
getSeoMeta($page, $variables)获取 SEO 元信息
getAvailableLanguages()获取可用语言包
applyAdLinkReplacements($content)章节内容关键词自动链接替换
safe_nl2br($str)安全换行处理
batch_latest_chapters($novelIds)批量获取多本小说最新章节
is_search_frequency_exceeded($limit, $window)搜索频率是否超限
record_search_request()记录搜索请求
reset_search_frequency()重置搜索频率
isSpider()是否蜘蛛
recordVisit()记录访问统计

2.2 DIY 榜单 / 广告

函数说明
DiyString($blockName)获取自定义榜单数据
DiyStrTitle($blockName)获取自定义榜单标题
generateBlockData($block)生成自定义榜单数据
showAd($position)输出指定位置广告

可用广告位:
home_banner、home_sidebar_top、home_sidebar_bottom、novel_detail_top、novel_detail_bottom、read_top、read_bottom、library_top、rank_top、author_top

2.3 小说 / 章节 / 阅读

函数说明
getChapterFilePath($novelId)生成章节文件路径
getChapterFullPath($filePath)获取章节完整路径
updateNovelTotalWords($novelId)更新小说总字数
getChapterPrice($chapterId)获取章节价格
checkChapterReadPermission($userId, $chapterId)检查章节阅读权限
purchaseChapter($userId, $chapterId)购买章节
isChapterPurchased($userId, $chapterId)是否已购买章节
deductGoldAndAddIncome($userId, $chapterId, $price)扣金币并给作者加收入
logVipReading($userId, $chapterId)记录 VIP 阅读

2.4 金币 / VIP / 签到

函数说明
addGold($userId, $goldChange, $type, $remark, $novel_id, $chapter_id)增减金币并写日志
getSignReward($userId)获取签到奖励预览
doSign($userId)执行签到
exchangeVipWithGold($userId, $months)金币兑换 VIP
getRechargeRate()获取充值汇率

2.5 作者收入 / 提现

函数说明
checkAuthorPermission($novelId, $userId = null)检查作者作品权限
get_user_withdraw_info($user_id)获取用户提现信息
save_user_withdraw_info($user_id, $data)保存用户提现信息
apply_withdraw($user_id, $amount, $withdraw_info)提交提现申请
get_author_income_stats($user_id)作者收入统计
get_author_income_by_novel($user_id)按作品统计收入
get_author_income_by_chapter($user_id, $novel_id = null)按章节统计收入

2.6 邮件 / 验证码 / 找回密码

函数说明
sendMail($to, $subject, $body)发送邮件
sendVerifyCode($email, $type)发送邮箱验证码
verifyCode($email, $code)验证邮箱验证码
resetPassword($email)重置密码并邮件通知

2.7 支付 / 订单

函数说明
generateOutTradeNo()生成订单号
getRechargeRate()充值汇率

二、各前端页面独有变量与函数

以下“独有变量”指对应 handler 返回、并通过 extract($viewData) 注入到模板的变量。


1. 首页 templates/default/index/home.php

对应 handler:home_handler()

变量说明
$banners首页轮播广告
$latestUpdates最新更新小说
$topViewsWeek周点击榜
$topUpdate最近更新榜
$pageTitle页面标题
$seoPagehome
$seoVariables首页 SEO 变量

模板中常用函数/调用:

调用说明
showAd('home_banner')首页轮播广告
showAd('home_sidebar_top')首页侧栏顶部广告
showAd('home_sidebar_bottom')首页侧栏底部广告
DiyString('featured_novels')精选小说
DiyString('latest_updates')最新更新
DiyString('rank_views')点击榜
DiyString('rank_favorites')收藏榜
DiyString('rank_words')字数榜
DiyString('random_picks')随机推荐
DiyStrTitle('xxx')获取对应 DIY 榜单标题

2. 书库 templates/default/index/library.php

对应 handler:library_handler()

变量说明
$categories所有分类
$novels当前页小说列表
$categoryId当前分类 ID
$status连载状态:all、ongoing、finished
$orderBy排序:id、updated_at、views、favorites
$minWords最小字数
$maxWords最大字数
$vipFilter作品类型:all、free、vip
$page当前页码
$totalPages总页数
$limit每页数量
$total总小说数
$pageTitle页面标题
$seoPagelibrary
$seoVariables包含 {page}、{total_pages}、{category_id}、{status}、{order_by}、{vip_filter}

常用调用:

调用说明
showAd('library_top')书库页顶部广告

3. 排行榜 templates/default/index/rank.php

对应 handler:rank_handler()

变量说明
$rankType榜单类型:views、words、favorites
$novels榜单小说列表
$pageTitle页面标题
$seoPagerank
$seoVariables包含 {rank_type}

常用调用:

调用说明
showAd('rank_top')排行榜顶部广告

4. 作者列表 templates/default/index/author.php

对应 handler:author_handler()

变量说明
$authors作者列表,含 author、novel_count
$pageTitle页面标题
$seoPageauthor
$seoVariables作者页 SEO 变量

常用调用:

调用说明
showAd('author_top')作者页顶部广告

5. 小说详情 templates/default/index/novel_detail.php

对应 handler:novel_detail_handler()

变量说明
$novel小说信息,含 id、title、author、category_name、cover、description、status、views、favorites、total_words、is_vip 等
$chaptersByVolume按分卷组织的章节:[volume_id => ['volume' => [...], 'chapters' => [...]]]
$totalWords总字数
$pageTitle页面标题
$seoPagenovel_detail
$seoVariables包含 {novel_title}、{author}、{category_name}、{status}、{novel_description}、{novel_keywords}、{total_words}、{total_chapters}、{views}、{favorites}、{description_preview}

常用调用:

调用说明
showAd('novel_detail_top')详情页顶部广告
showAd('novel_detail_bottom')详情页底部广告
DiyString('related_novels')相关推荐

6. 阅读页 templates/default/index/read.php

对应 handler:read_handler()

变量说明
$novel小说信息
$chapter当前章节信息
$content章节内容,可能为全文或预览
$prev上一章,含 id、title
$next下一章,含 id、title
$novelId小说 ID
$needPay是否需要付费
$needLogin是否需要登录
$price章节价格
$userGold当前用户金币
$error购买错误信息
$pageTitle页面标题
$seoPageread
$seoVariables包含 {novel_title}、{chapter_title}、{novel_keywords}、{chapter_word_count}、{chapter_intro}、{chapter_views}、{total_chapters}

常用调用/局部变量:

调用说明
getPointName()积分名称
applyAdLinkReplacements($content)关键词自动链接
safe_nl2br($content)安全换行
showAd('read_top')阅读页顶部广告
showAd('read_bottom')阅读页底部广告

7. 搜索页 templates/default/index/search.php

对应 handler:search_handler()

变量说明
$categories分类列表
$novels搜索结果
$keyword搜索关键词
$author作者筛选
$categoryId分类筛选
$status状态筛选
$orderBy排序
$minWords最小字数
$maxWords最大字数
$page当前页码
$totalPages总页数
$limit每页数量
$total结果总数
$needCaptcha是否需要验证码
$captchaError验证码错误信息
$searchExecuted是否已执行搜索
$pageTitle页面标题
$seoPagesearch
$seoVariables包含 {keyword}、{site_name}、{page}、{total_results}

常用调用:

调用说明
showAd('rank_top')搜索页底部广告,代码中实际调用了该位置

8. 404 / 500

404 templates/default/404.php

变量说明
$lang语言对象

500 templates/default/500.php

变量说明
$lang语言对象
$error_detail_for_view调试模式下错误详情

三、用户中心页面独有变量

用户中心所有页面都会包含 templates/default/user/sidebar.php,因此都需要:

变量说明
$user当前用户完整信息
$vipActiveVIP 是否有效
$lang语言对象
getPointName()积分名称

1. 用户中心首页 user/index.php

对应 handler:user_index_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$bookshelfCount书架数量
$historyCount阅读历史数量
$todaySigned今日是否已签到
$pageTitle页面标题

2. 个人资料 user/profile.php

对应 handler:user_profile_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$message操作提示
$pageTitle页面标题

3. 书架 user/bookshelf.php

对应 handler:user_bookshelf_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$books书架列表
$pageTitle页面标题

4. 阅读历史 user/history.php

对应 handler:user_history_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$history历史记录
$page当前页码
$totalPages总页数
$total总记录数
$limit每页数量
$pageTitle页面标题

5. 金币明细 user/gold.php

对应 handler:user_gold_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$logs金币日志
$pageTitle页面标题

6. 签到 user/sign.php

对应 handler:user_sign_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$message签到结果
$todaySigned今日是否已签到
$rewardPreview签到奖励预览:continuous_days、base_gold、bonus_percent、incremental、total、bonus
$pageTitle页面标题

7. VIP 中心 user/vip.php

对应 handler:user_vip_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$message操作提示
$pricePerMonth人民币月价
$goldPerMonth金币月价
$pageTitle页面标题

8. 登录 user/login.php

对应 handler:user_login_handler()

变量说明
$error登录错误
$pageTitle页面标题

9. 注册 user/register.php

对应 handler:user_register_handler()

变量说明
$error注册错误
$pageTitle页面标题

模板中还会读取:

变量/设置说明
getSetting('register_mode', $db)注册模式:normal 或 email

10. 忘记密码 user/forgot.php

对应 handler:user_forgot_handler()

变量说明
$step当前步骤
$message成功提示
$error错误提示
$pageTitle页面标题

11. 积分充值 user/recharge.php

对应 handler:user_recharge_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$message成功提示
$error错误提示
$rate充值汇率
$pointName积分名称
$rechargeLogs最近充值记录
$pageTitle页面标题

四、作者中心页面独有变量

作者中心页面同样包含 user/sidebar.php,所以都需要 $user、$vipActive。

1. 申请作者 author/apply_author.php

对应 handler:apply_author_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$message提示
$error错误
$applied是否已提交申请
$pageTitle页面标题

2. 我的作品 author/author_works.php

对应 handler:author_works_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novels当前作者作品列表
$pageTitle页面标题

3. 发布作品 author/novel_add.php

对应 handler:novel_add_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$categories分类列表
$penName作者笔名
$isAuthor是否作者
$pageTitle页面标题

4. 编辑作品 author/novel_edit.php

对应 handler:novel_edit_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novel小说信息
$categories分类列表
$pageTitle页面标题

5. 章节列表 author/chapter_list.php

对应 handler:chapter_list_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novel小说信息
$novelId小说 ID
$chapters章节列表
$page当前页码
$totalPages总页数
$pageTitle页面标题

6. 添加章节 author/chapter_add.php

对应 handler:chapter_add_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novel小说信息
$novelId小说 ID
$volumes分卷列表
$pageTitle页面标题

7. 编辑章节 author/chapter_edit.php

对应 handler:chapter_edit_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novel小说信息
$novelId小说 ID
$chapter章节信息
$volumes分卷列表
$content章节正文
$pageTitle页面标题

8. 分卷列表 author/volume_list.php

对应 handler:volume_list_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novel小说信息
$novelId小说 ID
$volumes分卷列表
$pageTitle页面标题

9. 添加分卷 author/volume_add.php

对应 handler:volume_add_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novel小说信息
$novelId小说 ID
$pageTitle页面标题

10. 编辑分卷 author/volume_edit.php

对应 handler:volume_edit_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$novel小说信息
$novelId小说 ID
$volume分卷信息
$pageTitle页面标题

11. 收入统计 author/income.php

对应 handler:user_income_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$stats统计:total_sales、month_sales、today_sales、withdrawn、unwithdrawn
$novelList作品列表
$novelId当前筛选作品 ID
$timeRange时间范围
$chapterStats章节统计
$detailList收入明细
$page当前页码
$totalPages总页数
$total总记录数
$pageTitle页面标题

12. 提现申请 author/withdraw.php

对应 handler:user_withdraw_handler()

变量说明
$user用户信息
$vipActiveVIP 是否有效
$message成功提示
$error错误提示
$withdrawInfo提现信息:real_name、account_type、account、withdraw_qrcode
$withdraws提现记录
$page当前页码
$totalPages总页数
$pageTitle页面标题

五、快速索引:模板文件 → handler → 主要变量

模板文件handler主要独有变量
index/home.phphome_handler()$banners、$latestUpdates、$topViewsWeek、$topUpdate
index/library.phplibrary_handler()$categories、$novels、$categoryId、$status、$orderBy、$vipFilter、$page、$totalPages、$total
index/rank.phprank_handler()$rankType、$novels
index/author.phpauthor_handler()$authors
index/novel_detail.phpnovel_detail_handler()$novel、$chaptersByVolume、$totalWords
index/read.phpread_handler()$novel、$chapter、$content、$prev、$next、$needPay、$needLogin、$price、$userGold
index/search.phpsearch_handler()$categories、$novels、$keyword、$total、$needCaptcha、$captchaError、$searchExecuted
404.php无$lang
500.php无$lang、$error_detail_for_view
user/index.phpuser_index_handler()$user、$vipActive、$bookshelfCount、$historyCount、$todaySigned
user/profile.phpuser_profile_handler()$user、$vipActive、$message
user/bookshelf.phpuser_bookshelf_handler()$user、$vipActive、$books
user/history.phpuser_history_handler()$history、$page、$totalPages、$total
user/gold.phpuser_gold_handler()$logs
user/sign.phpuser_sign_handler()$todaySigned、$rewardPreview、$message
user/vip.phpuser_vip_handler()$pricePerMonth、$goldPerMonth、$message
user/recharge.phpuser_recharge_handler()$rate、$pointName、$rechargeLogs
author/apply_author.phpapply_author_handler()$applied、$message、$error
author/author_works.phpauthor_works_handler()$novels
author/novel_add.phpnovel_add_handler()$categories、$penName、$isAuthor
author/novel_edit.phpnovel_edit_handler()$novel、$categories
author/chapter_list.phpchapter_list_handler()$novel、$novelId、$chapters、$page、$totalPages
author/chapter_add.phpchapter_add_handler()$novel、$novelId、$volumes
author/chapter_edit.phpchapter_edit_handler()$novel、$novelId、$chapter、$volumes、$content
author/volume_list.phpvolume_list_handler()$novel、$novelId、$volumes
author/volume_add.phpvolume_add_handler()$novel、$novelId
author/volume_edit.phpvolume_edit_handler()$novel、$novelId、$volume
author/income.phpuser_income_handler()$stats、$novelList、$chapterStats、$detailList、$page、$totalPages
author/withdraw.phpuser_withdraw_handler()$withdrawInfo、$withdraws、$message、$error、$page、$totalPages

以上就是在 templates/default 前端主题里可直接使用的共用变量/函数,以及各页面独有变量整理。