要做到這一點,你需要使你的數據granular足以能夠選擇個人傳記領域。在MediaWiki中有幾種方法可以做到這一點。
分析器功能
您可以使用#switch語句來創建一個模板,將做的工作,而無需添加任何擴展。例如,你可以在Template:Biography/data
有一個數據模塊:
{{#switch: {{{character|}}}
| Steven = {{#switch: {{{trait|}}}
| Personality = Outgoing
| Age = 25
}}
| Dennis = {{#switch: {{{trait|}}}
| Personality = Shy
| Age = 34
}}
}}
然後,您可以調用從Template:Biography
這樣的:
{{#if: {{{2|}}}
| {{Biography/data|character={{{1|}}}|trait={{{2|}}}}}
|
* Name: {{{1|}}}
* Age: {{Biography/data|character={{{1|}}}|trait=Age}}
* Personality: {{Biography/data|character={{{1|}}}|trait=Personality}}
}}
您可以用多種不同的方式組織這次#switch聲明。例如,您可以將每個字符的數據存儲在Template:Biography
的不同子模板中,如Template:Biography/data/Steven
,Template:Biography/data/Dennis
等。
模板:傳記:
{{#if: {{{2|}}}
| {{Biography/data|{{{1|}}}|{{{2|}}}}}
|
* Name: {{{1|}}}
* Age: {{Biography/data|{{{1|}}}|Age}}
* Personality: {{Biography/data|{{{1|}}}|Personality}}
}}
模板:傳記/數據
{{Biography/data/{{{1|}}}|trait={{{2|}}}}}
模板:傳記/數據/史蒂芬:
{{#switch: {{{trait|}}}
| Personality = Outgoing
| Age = 25
}}
這將是比我們的第一次嘗試更好,因爲MediaWiki中的開關語句長時間地慢。但是,您仍需要兩個子模板級別才能使其工作,並且如果不指定特徵,則必須多次調用子模板。所以它仍然會很慢。
Scribunto
您可以通過寫在Lua模板,通過Scribunto擴展加快速度。你可以寫這樣的數據模塊(假設它被稱爲Module:Biography/data
):
return {
['Steven'] = {
Personality = 'Outgoing',
Age = 25,
},
['Dennis'] = {
Personality = 'Shy',
Age = 34,
},
}
然後你就可以加載,而不是每次使用時使用mw.loadData數據,這將每頁一次加載整個表, {{Biography}}
模板。例如:
local data = mw.loadData('Module:Biography/data')
local p = {}
local BIO_TEMPLATE = [[
* Name: %s
* Age: %d
* Personality: %s]]
-- Trim whitespace from args, and treat blank args as nil
local function preprocessArg(s)
if not s then
return nil
end
s = s:match('^%s*(.-)%s*$') -- trim whitespace
if s == '' then
return nil
else
return s
end
end
function p.main(frame)
local args = frame.args
local character = preprocessArg(args[1])
local trait = preprocessArg(args[2])
-- Check for blank character arguments
if not character then
return ''
end
-- Get the data for the specified character
local characterData = data[character]
if not characterData then
return ''
end
if trait then
-- User specified a trait, so return it
return characterData[trait] or ''
else
-- Return the biography template with all the traits in it
return string.format(
BIO_TEMPLATE,
character,
characterData.Age,
characterData.Personality
)
end
end
return p
您還可以通過文字或特質了拆分數據頁,在大致相同的方式,你可以用#switch。
使用Lua的缺點是您的編輯將需要學習Lua語法以及wikitext語法來向數據模塊添加條目。 Lua語法通常比wikitext更難掌握,因此這可能會使人們失去貢獻。
擴展
wikitext的數據是非結構化的問題是一個長期存在的一個,所以就出現了很多嘗試來解決它多年來的。在解決純粹的解析器函數或純Scribunto之前,你最好調查一些已經做出嘗試和解決這個問題的擴展。 (請記住,您可以通過模板訪問這些擴展名,通常也可以通過Scribunto訪問這些擴展名。)
例如,labeled section transclusion擴展名將允許您爲自己的角色編寫普通傳記,劃定不同部分,然後跨越這些部分分別到其他頁面上。
也有更多的數據爲中心的擴展,可以幫助你做出更細緻的模板,而無需在交換機的功能或數據模塊陷入困境:
這些將分別存儲在數據庫中的數據字段(通常與MediaWiki安裝相同),因此性能特徵將取決於您需要多少數據庫訪問來生成頁面,以及如何設置緩存。
如果我是你,我會首先看看可用的擴展名,看看是否有適合你的情況,然後使用模板或Scribunto進行微調。
'Biography'模板通常會在同一頁面上多次使用嗎?或者它通常每頁只能使用一次? –
大概一次,但可能更多 – o0o0o0o0o