יחידה:תבנית חוזרת

מתוך ויקיספר, אוסף הספרים והמדריכים החופשי

יחידה זו מיועדת לשימוש בתבניות מרובות פרמטרים, שתומכות במספר בלתי מוגבל של פרמטרים.

שימוש[עריכה]

יש להעביר ליחידה פרמטר יחיד בשם תבנית שיכיל את התבנית החוזרת:

  • כל פרמטר חוזר חייב להסתיים ב-X (ה-X מוחלף במספר). למשל כדי לתמוך ב"שם1" ו"שם2" וכו' נגדיר {{{שםX}}}
  • (אופציונלי) הפרמטר {{{?X}}} מציין את מספר הפרמטר. למשל הגדרת: {{{?X}}} {{{שםX}}} תגדיר 1. שם ראשון 2. שם שני

מגבלות[עריכה]

התבנית מניחה שכל הפרמטרים הם פרמטרי חובה. למשל אם התבנית היא {{{שםX}}} {{{גילX}}} חייבים למלא גם שם1 וגם גיל1 כדי שיופיעו (אם לא צוין אחד מהם, שניהם לא יופיעו).

דוגמה[עריכה]

{{#invoke:תבנית חוזרת|חזרה
|תבנית=
* '''{{{שם X}}}''': {{{גיל X}}}
}}

local p = {}

function p.parse( frame )
	local template = frame.args['תבנית']
	local i = 0
	local req_params = {}
	-- extract parameters from templat
	while i~=nil do
		i = mw.ustring.find( template, '\{\{\{', i )
		if i ~= nil then
			start_ind = i+3
			i = mw.ustring.find( template, '\}\}\}', i )
			param_name = mw.ustring.sub( template, start_ind, i+2 )
			req_params[param_name] = 1
		end
	end
	
	-- expand argumetns using the template
	i=1
	local found = true
	local segments = {}
	local pFrame = frame:getParent()
	while found do
		local curr_template = template
		found = true
		for k,v in pairs(req_params) do
			cur_k = mw.ustring.gsub( k, 'X}}}', tostring(i), 1 )
			if pFrame.args[cur_k]  then 
				local val = pFrame.args[cur_k]
				local param_name = '\{\{\{'..k
				local start_ind = mw.ustring.find( curr_template, param_name )
				curr_template = mw.ustring.sub( curr_template, 0, start_ind-1) .. val .. mw.ustring.sub( curr_template, start_ind+mw.ustring.len( param_name ))
			elseif k=='?X}}}' then
					curr_template = mw.ustring.gsub( curr_template, '{{{%?X}}}', tostring(i)  )
			else
				found = false
				break
			end
		end
		if  found then
			segments[i] = curr_template
		end
      	i=i+1
    end
    return table.concat( segments, '\n' )
end

p['חזרה'] = p.parse
return p