You pretty much have the framework down for transferring your template into Lua, All you really need to do is put in the code for Lua to create the objects within the loop that the template would've done.
Lua Code:
local item=CreateFrame("Button","BagBuddy_Item"..idx,frame,"SecureActionButtonTemplate");
item:SetSize(37,37);
item:SetAttribute("type2","item");
item:SetScript("OnEnter",BagBuddy_Button_OnEnter);
item:SetScript("OnLeave",BagBuddy_Button_OnLeave);
item:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2");
item:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress");
item:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square","ADD");
do local tex=item:GetNormalTexture();-- Hack to modify the existing texture object
tex:ClearAllPoints();
tex:SetPoint("CENTER",0,-1);
tex:SetSize(64,64);
end
item.icon=item:CreateTexture("$parentIconTexture","BORDER");
item.count=item:CreateFontString("$parentCount","BORDER","NumberFontNormal");
item.count:SetPoint("BOTTOMRIGHT",-5,2);
item.count:SetJustifyH("RIGHT");
item.count:Hide();
item.glow=item:CreateTexture("$parentGlow","OVERLAY");
item.glow:SetPoint("CENTER");
item.glow:SetSize(70,70);
item.glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border");
item.glow:SetBlendMode("ADD");
item.glow:SetAlpha(0.6);
If there aren't any errors I missed, that should create the exact frame your template is making. Just replace the line where you create the item button with this and as Haleth suggested, move the contents of the OnLoad function to the main chunk and replace all references of
self with
frame.