Extension:变量
![]() 發佈狀態: 穩定版本 |
|
---|---|
实现 | 解析器函数 |
描述 | 介绍页面作用的变量函数。 |
作者 |
|
维护者 | MGChecker |
最新版本 | 2.5.1 (2019-07-11) |
MediaWiki | 1.29+ |
PHP | 5.5+ |
数据库更改 | 否 |
Composer | mediawiki/variables |
许可协议 | ISC授權條款 |
下載 | README RELEASE-NOTES |
|
|
季度下載量 | 160 (Ranked 64th) |
公開的wiki使用 | 1,808 (Ranked 201st) |
翻譯Variables的扩展,若在translatewiki.net可用 | |
Vagrant角色 | variables |
問題 | 尚未完成的工作 · 报告錯誤 |
变量(Variables)扩展允许您在一个页面定义变量,然后在同样的页面或包括的模板变化它的值,可能是通过表达式在旧值中赋予的值等。
它很像模板,只是占用内存非常少并且作用只能在1个页面,所以你可以在1个页面使用很多个变量,不会出现因使用大量的模板导致维基运行缓慢的问题。结合解析器函數 扩展以达到最好的结果。
#var_final
parser function is shown as deprecated in MediaWiki 1.35 or above, due to changes in the parser. However, it will continue to work in this versions. See 任务T276627 and 任务T236809 for further information.
给变量赋值
#vardefine
{{#vardefine:变量名称|指定值}}
将值指定值分配给(已存在或由此引入的)变量变量名。
- 示例:
{{#vardefine:iconwidth|25}}
使iconwidth = 25
#vardefineecho
{{#vardefineecho:变量名称|指定值}}
与#vardefine
完全相同,但会打印受影响的值。
- 例如: making
iconwidth = {{#vardefineecho:iconwidth|25}}
检索变量的值
#var
变量“变量名称“的值的产生:
{{#var:变量名称}}
如果未定义,会产生空字符串,不会抛出错误。
变量未定义或为空时可以定义值:
{{#var:变量名称 | 默认值 }}
相当于:
{{#if: {{#var:变量名称}} | {{#var:变量名称 }} | 默认值 }}
但这样更加简短。在版本2.0之前,默认值会自动展开。从版本2.0开始,默认值只会在真正用到时展开。
值可用于解析器函数。
#varexists
{{#varexists:变量名称 }}
如果变量已经定义返回1。如果没有定义则返回空。
它支持第二个和第三个参数来替换这些值。
{{#varexists:变量名称 | if值 | else-value }}
这相当于:
{{#if: {{#varexists: 变量名称 }} | if值 | else-value }}
但它更短,更好地安排。在2.5版之前,两者都是扩展的。 从2.5以后的if和else值只有在实际输入案例时才会展开。
#var_final
变量2.0中引入了实验新功能。 此函数将输出变量在页面渲染结束时具有的最后一个最后一个值。 当然,在解析器遍历整个wiki标记之后将插入该值,因此该函数不能用于其他函数,期望正在使用正确的值。 例:
{{#var_final:变量名称 | 默认值}}
如果变量在最终页面呈现阶段不存在或者其值为空字符串,则将使用默认值。 默认值将在使用函数的位置展开,因此即使不需要参数,也会扩展参数。
示例
解析器函數 扩展必须已安装以使用#expr
。
计算x = 2*a + b
:
{{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}}
令变量n加1:
{{#vardefine:n|{{#expr:{{#var:n}}+1}}}}
安裝
- 下载文件,并将其放置在您
extensions/
文件夹中的Variables
目录内。 - 将下列代码放置在您的
LocalSettings.php
的底部:wfLoadExtension( 'Variables' );
- Configure as required
完成 – 在您的wiki上导航至Special:Version,以验证已成功安装扩展。
致使用MediaWiki 1.30或更早版本的用户:
上面的说明介绍的是安装此扩展的新方法,它使用wfLoadExtension()
。
如果您需要在早期版本(MediaWiki 1.30和更早版本)中安装此扩展,而不是wfLoadExtension( 'Variables' );
,您需要使用:
require_once "$IP/extensions/Variables/Variables.php";
配置
該擴充功能提供了兩個配置參數:
$egVariablesAreVolatile
- Allows to disable the parser frame being marked as volatile by the extension, i.e. disable template caching.
- 默认:
true;
$egVariablesDisabledFunctions
- 允許指定停用此擴充功能提供的解析器功能。
- 默认:
[];
- 示例:
[ 'var_final', 'vardefineecho' ];
兼容性
下面列出了最近MediaWiki版本的Variables扩展的推荐版本。较旧版本的扩展可能也可以正常工作,但未针对新的MediaWiki版本进行测试。
1.19 | 1.20 | 1.21 | 1.22 | 1.23 | 1.24 | 1.25 | 1.26 | 1.27 | 1.28 | 1.29 | 1.30 | 1.31 | 1.32 | 1.33 | 1.34 | master | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2.x | 2.1.x | 2.2.x | 2.5.x, master |
替代選項
由于这个扩展不被维基媒体基金会(WMF)启用,以下是一些替代选择:[1][2][3][4]
- If you use Variables as cache for expensive operations, you can transform the section where you need them into a template and pass the required information as template parameters instead. This will work until you reach the expansion depth limit, which can be increased if necessary.
- If you use Variables to do more complex template operations than possible with simple Extension:解析器函數 , and if you have sufficient permissions on the server, you can use Scribunto's Lua functionality instead. You may not be able to install Scribunto on shared hosting. Note that this does not add support for global Variables. However, Extension:VariablesLua adds a Scribunto interface for the Variables store.
- Don't use variables, instead duplicate the information you need as a variable. If that information is acquired by an expensive template call, performance may suffer. This will work until you reach the node count limit.
- If you require variables just for autonumbering, you could look into Extension:NumerAlpha .
参见
- 扩展:Php标签
- Extension:MyVariables - 在MediaWiki创建新变量
- Extension:迴圈 — 提供执行循环的解析器函数
- Extension:数组 — Create an array and provide array functions(比如搜索、分离、排序)and set operations (such as intersect, union and diff)
- Extension:HashTables — 用于处理MediaWiki的hash table的新解析器函数
参考
- ↑ 1.0 1.1 phab:T9865
- ↑ 2.0 2.1 phab:T65324
- ↑ 3.0 3.1 phab:T113859
- ↑ 4.0 4.1 phab:T151192
![]() | 此擴充功能包含在以下包或/和wiki農場: 這不是一份權威名單。即使一些wiki農場/託管者沒有被列在這裡,他們也可能包含這個extension。請以各wiki農場/託管者或包的狀態為準。 |
- Stable extensions/zh
- Parser function extensions/zh
- Extensions supporting Composer/zh
- ISC licensed extensions/zh
- Extensions in Wikimedia version control/zh
- InternalParseBeforeSanitize extensions/zh
- ParserClearState extensions/zh
- ParserFirstCallInit extensions/zh
- All extensions/zh
- Extensions included in BlueSpice/zh
- Extensions included in Canasta/zh
- Extensions included in Fandom/zh
- Extensions included in Miraheze/zh
- Extensions included in ProWiki/zh
- Extensions included in semantic::core/zh
- Extensions included in wiki.gg/zh
- Extensions included in WikiForge/zh
- Modifiable variables extensions/zh