Macro thêm vào nơi cần dùng (Twig,shoutbox history, event):
{%macro bbcode(content,tag,template,template2)%}{%if template2 is empty%}{%set template2=template|replace({'$1':'$2'})|raw%}{%endif%}{%set msg=content|split('[/'~tag~']')%}{%set msgx%}{%for i in 0..msg|length-1%}{%if '['~tag~']' in msg[i]%}{%set sp=msg[i]|split('['~tag~']')%}{%set l=msg[i]|split('['~tag~']')|length-1%}{%set v0=msg[i]|split(('['~tag~']'~sp[l]))[0]|raw%}{%set v1=sp[l]%}{{template|replace({'$0':(v0),'$1':(v1)})|raw}}{%elseif '['~tag~'="' in msg[i]%}{%set sp2=msg[i]|split('['~tag~'="')%}{%set sp3=sp2[1]|split('"]')%}{{template2|replace({'$0':(sp2[0]),'$1':(sp3[0]),'$2':(sp3[1])})|raw}}{%else%}{{msg[i]|raw}}{%endif%}{%endfor%}{%endset%}{{msgx|raw}}{%endmacro%}{%import _self as thaian2009%}
Bbcode url, xcode, div có thể chế thêm
{%set message='xin chao [url]http://company.com[/url] haha [url]buồn nhờ[url]link 2[/url] test[url="link"]name[/url]fff[url="http://google.com"]vào google[/url]Hộp văn bản[xcode]hihi[/xcode]hộp 2[xcode="blue"]haha[/xcode]sdsd[div]Topic cần dùng[/div]ffsdfsd'%} {%set url=thaian2009.bbcode(message,'url','$0<a href="$1" style="color:#f00">$1</a>','$0<a href="$1" style="color:#00f">$2</a>')%} {%set xcode=thaian2009.bbcode(url,'xcode','$0<textarea style="border:1px solid #f40">$1</textarea>','$0<textarea style="color:$1">$2</textarea>')%} {{thaian2009.bbcode(xcode,'div','$0<div class="title">$1</div>','')}}
+ Hướng dẫn:
{{thaian2009.bbcode(data,tag_name,template1,template2)}}
Trong đó:
-String data là dữ liệu vào.
-String tag_name là tên tag bbcode muốn tạo. ví dụ: xcode, imgs, hide...
-String template1 là mẫu loại 1. Kiểu $0 [tag]Nội dung bbcode $1[/tag].
-String template2 là mẫu loại 2. Kiểu $0 [tag="variable $1"]Nội dung bbcode $2[/tag]
Nếu không dùng template2 có thể bỏ trống khi đó nó sẽ lấy template1 làm template2 có nghĩa là:
{{thaian2009.bbcode(data,tag_name,tamplate1)}}
Demo: http://shoutboxgenerator.wap.m ... 62057
Nguồn: http://cobethoitiet.wap.mu
![[OFF]](/assets/images/off.gif)
![[ON]](/assets/images/on.gif)