Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

Модуль:Tabs: различия между версиями

Материал из YastreWiki
Новая страница: «local p = {} function p.render(frame) local args = frame:getParent().args local root = mw.html.create('div') root:addClass('tab-container') local tabs = {} local contentBlocks = {} local activeTabId = nil local i = 1 while args['tab' .. i .. '_title'] do local title = clean(args['tab' .. i .. '_title']) local content = clean(args['tab' .. i .. '_content']) local id = 'tab-' .. i if t...»
 
Нет описания правки
 
Строка 1: Строка 1:
local p = {}
local p = {}
local function clean(val)
    if val == '' or val == nil then return nil end
    return mw.text.trim(val)
end


function p.render(frame)
function p.render(frame)
    local styles = frame:extensionTag('templatestyles', '', { src = 'Шаблон:Tabs/styles.css' })
   
     local args = frame:getParent().args
     local args = frame:getParent().args
     local root = mw.html.create('div')
     local root = mw.html.create('div')
Строка 8: Строка 15:
     local tabs = {}
     local tabs = {}
     local contentBlocks = {}
     local contentBlocks = {}
     local activeTabId = nil
   
     local uniqueId = tostring(math.random(1000, 9999))


     local i = 1
     local i = 1
Строка 14: Строка 22:
         local title = clean(args['tab' .. i .. '_title'])
         local title = clean(args['tab' .. i .. '_title'])
         local content = clean(args['tab' .. i .. '_content'])
         local content = clean(args['tab' .. i .. '_content'])
         local id = 'tab-' .. i
         local id = 'yastre-tab-' .. uniqueId .. '-' .. i
          
          
         if title and content then
         if title and content then
             table.insert(tabs, { id = id, title = title })
             table.insert(tabs, { id = id, title = title })
             table.insert(contentBlocks, { id = id, content = content })
             table.insert(contentBlocks, { id = id, content = content })
            if i == 1 then
                activeTabId = id
            end
         end
         end
         i = i + 1
         i = i + 1
     end
     end
    if #tabs == 0 then return "Ошибка: Вкладки не заполнены!" end


     local nav = root:tag('div'):addClass('tabs-nav')
     local nav = root:tag('div'):addClass('tabs-nav')
     for _, tab in ipairs(tabs) do
     for index, tab in ipairs(tabs) do
         nav:tag('a')
         nav:tag('div')
             :addClass('tab-link')
             :addClass('tab-link')
             :attr('href', '#' .. tab.id)
             :addClass(index == 1 and 'active' or '')
             :addClass(tab.id == activeTabId and 'active' or '')
             :attr('data-target', tab.id)
             :wikitext(tab.title)
             :wikitext(tab.title)
     end
     end


     local content = root:tag('div'):addClass('tab-content')
     local contentWrapper = root:tag('div'):addClass('tab-content')
     for _, block in ipairs(contentBlocks) do
     for index, block in ipairs(contentBlocks) do
         content:tag('div')
         contentWrapper:tag('div')
             :addClass('tab-pane')
             :addClass('tab-pane')
             :attr('id', block.id)
             :attr('id', block.id)
             :addClass(block.id == activeTabId and 'active' or '')
             :addClass(index == 1 and 'active' or '')
             :wikitext(block.content)
             :wikitext(block.content)
     end
     end


     return tostring(root)
     return styles .. tostring(root)
end
end


return p
return p

Текущая версия от 00:40, 13 апреля 2026

Для документации этого модуля может быть создана страница Модуль:Tabs/doc

local p = {}

local function clean(val)
    if val == '' or val == nil then return nil end
    return mw.text.trim(val)
end

function p.render(frame)
    local styles = frame:extensionTag('templatestyles', '', { src = 'Шаблон:Tabs/styles.css' })
    
    local args = frame:getParent().args
    local root = mw.html.create('div')
    root:addClass('tab-container')

    local tabs = {}
    local contentBlocks = {}
    
    local uniqueId = tostring(math.random(1000, 9999))

    local i = 1
    while args['tab' .. i .. '_title'] do
        local title = clean(args['tab' .. i .. '_title'])
        local content = clean(args['tab' .. i .. '_content'])
        local id = 'yastre-tab-' .. uniqueId .. '-' .. i
        
        if title and content then
            table.insert(tabs, { id = id, title = title })
            table.insert(contentBlocks, { id = id, content = content })
        end
        i = i + 1
    end

    if #tabs == 0 then return "Ошибка: Вкладки не заполнены!" end

    local nav = root:tag('div'):addClass('tabs-nav')
    for index, tab in ipairs(tabs) do
        nav:tag('div')
            :addClass('tab-link')
            :addClass(index == 1 and 'active' or '')
            :attr('data-target', tab.id)
            :wikitext(tab.title)
    end

    local contentWrapper = root:tag('div'):addClass('tab-content')
    for index, block in ipairs(contentBlocks) do
        contentWrapper:tag('div')
            :addClass('tab-pane')
            :attr('id', block.id)
            :addClass(index == 1 and 'active' or '')
            :wikitext(block.content)
    end

    return styles .. tostring(root)
end

return p