Tools 是非官方社区Wiki。社区文档正在编写中,欢迎参与。 Wiki编辑答疑群:717421103
版本250722.2
全站通知:

帮助:解析函数/fornumargs

来自WIKI实验室WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

fornumargs是一个解析函数。帮助:解析函数页列出了所有解析函数的说明。

fornumargs

遍历模板编号参数,以指定格式输出。出自扩展 Loops

仅在模板中使用。遍历模板调用时传入的编号参数(显式或隐式编号),按照指定的格式输出。

语法

{{#fornumargs: 键变量名 | 值变量名 | 代码块}}

  • 键变量名(可选):存储参数位置的变量名(如"1"、"2"等)
  • 值变量名:存储参数值的变量名
  • 代码块:每次循环执行的wikitext代码
  • 参数处理特性:
    • 自动过滤非数字参数(如"key=value"中key不是纯数字时会被忽略)
    • 支持显式编号参数(如1=值)和隐式参数(如{{模板|值1|2=值2|a=不包括a}}
    • 参数键会按数字升序排列处理

示例

  • 基础用法
{{#fornumargs: pos | val |<nowiki/>
* {{#var:pos}}={{#var:val}}
}}
 调用:{{模板|苹果|B=香蕉|梨|5=芒果}}
 输出:
 * 1=苹果
 * 2=梨
 * 5=芒果


底层代码

来自MediaWiki及其扩展的源代码,运行在服务端。此处仅供快速查阅,便于更充分的挖掘其“特性”。

/** mediawiki-extensions-Loops-REL1_37\ExtLoops.php
	* #fornumargs: keyVarName | valVarName | code
	* or (since 0.4 for more consistency)
	* #fornumargs: | keyVarName | valVarName | code
	* @param Parser $parser
	* @param PPFrame $frame
	* @param array $args
	* @return string
	*/
public static function pfObj_fornumargs( Parser $parser, PPFrame $frame, array $args ) {
	/**
		* get numeric arguments, don't use PPFrame::getNumberedArguments because it would
		* return explicitely numbered arguments only.
		*/
	$tNumArgs = $frame->getArguments();
	foreach ( $tNumArgs as $argKey => $argVal ) {
		// allow all numeric, including negative values!
		if ( is_string( $argKey ) ) {
			unset( $tNumArgs[ $argKey ] );
		}
	}
	// sort from lowest to highest
	ksort( $tNumArgs );

	if ( count( $args ) > 3 ) {
		/**
			* compatbility to pre 0.4 but consistency with other Loop functions.
			* this way the first argument can be ommitted like '#fornumargs: |varKey |varVal |code'
			*/
		array_shift( $args );
	}

	return self::perform_forargs( $parser, $frame, $args, $tNumArgs, '' );
}

/**
	* Generic function handling '#forargs' and '#fornumargs' as one
	* @param Parser $parser
	* @param PPFrame $frame
	* @param array $funcArgs
	* @param array $templateArgs
	* @param string $prefix
	* @return string
	*/
protected static function perform_forargs(
		Parser $parser,
		PPFrame $frame,
		array $funcArgs,
		array $templateArgs,
		$prefix = ''
) {
	// if not called within template instance:
	if ( !( $frame->isTemplate() ) ) {
		return '';
	}

	// name of the variable to store the argument name:
	$keyVar  = array_shift( $funcArgs );
	$keyVar  = $keyVar !== null ? trim( $frame->expand( $keyVar ) ) : '';
	// name of the variable to store the argument value:
	$valVar  = array_shift( $funcArgs );
	$valVar  = $valVar !== null ? trim( $frame->expand( $valVar ) ) : '';
	// unexpanded code:
	$rawCode = array_shift( $funcArgs );
	$rawCode = $rawCode !== null ? $rawCode : '';

	$output = '';

	// if prefix contains numbers only or isn't set, get all arguments, otherwise just non-numeric
	$tArgs = preg_match( '/^([1-9][0-9]*)?$/', $prefix ) > 0
			? $frame->getArguments() : $frame->getNamedArguments();

	foreach ( $templateArgs as $argName => $argVal ) {
		// if no filter or prefix in argument name:
		if ( $prefix !== '' && strpos( $argName, $prefix ) !== 0 ) {
			continue;
		}
		if ( $keyVar !== $valVar ) {
			// variable with the argument name without prefix as value:
			self::setVariable( $parser, $keyVar, substr( $argName, strlen( $prefix ) ) );
		}
		// variable with the arguments value:
		self::setVariable( $parser, $valVar, $argVal );

		// expand current run:
		$output .= trim( $frame->expand( $rawCode ) );
	}

	return $output;
}
代码逻辑:
  • 处理参数,临时存储
    • 过滤:自动忽略命名参数(如color=red),包含显式数字参数(如1=苹果)和隐式位置参数(如{{模板|苹果|栗子}}
    • 排序:参数按键值数字升序排列处理,比如{{模板|5=红|2=蓝|绿}}将按参数1,2,5顺序处理
    • 覆盖:后定义的显式参数会覆盖隐式位置。比如{{模板|苹果|1=橘子}}最终只处理1=橘子
    • 空参数:允许键变量名为空(如{{#fornumargs: ||val|code}})但值变量名必须定义,否则无法存储参数值
  • 每个参数都将按照代码块指定的格式输出。

实际用例

一些Wiki使用了相关特性,如下所示这个静态列表可能在下列页面更改后过时仅供批判性参考