Animated forum with XSLT and AJAX. Today is a special day. Today our XSLT lessons will show some really interesting (attractive) results which you can apply in practice. We will make a forum. Initially we will display only the forum topics, and load posts dynamically (by mouse clicking on the topics) using ajax technology. I think you may have seen similar forums on the Internet, so I will tell you how to do it your own.
Here are samples and downloadable package:
[sociallocker]
[/sociallocker]
Ok, download the source files and lets start coding !
Step 1. Source XML
Firstly lets define our structure of forum:
forum.xml
01 | <?xml version="1.0" encoding="UTF-8"?> |
03 | <topic title="XSLT forum demonstration" date="15.04.2011" description="description can be here"> |
04 | <post date="15.04.2011" author="Diego"> |
05 | <description><![CDATA[ |
06 | This forum able to display any HTML entities |
09 | <post date="15.04.2011" author="Andrew"> |
10 | <description><![CDATA[ |
11 | As example <b>bold text</b>, <i>italic</i>, <u>underline</u>, <a href="http://www.google.com">links</a> etc |
14 | <post date="15.04.2011" author="James"> |
15 | <description><![CDATA[ |
19 | <post date="15.04.2011" author="John"> |
20 | <description><![CDATA[ |
24 | <post date="15.04.2011" author="Robert"> |
25 | <description><![CDATA[ |
26 | Wonderful forum, isn`t it? |
30 | <topic title="sample forum topic 2" date="16.04.2011" description="description of topic 2"> |
31 | <post date="16.04.2011" author="Michael"> |
32 | <description><![CDATA[ |
36 | <post date="16.04.2011" author="William"> |
37 | <description><![CDATA[ |
41 | <post date="16.04.2011" author="David"> |
42 | <description><![CDATA[ |
47 | <topic title="sample forum topic 3" date="17.04.2011" description="description of topic 3"> |
48 | <post date="17.04.2011" author="Charles"> |
49 | <description><![CDATA[ |
53 | <post date="17.04.2011" author="Joseph"> |
54 | <description><![CDATA[ |
Forum contain several topics, each topic can contain variable amount of posts. Each post have name of Author, date of posting, and, text itself (Description) can contain HTML tags.
Step 2. PHP
Here are easy PHP file which we using for xsl transformation:
index.php
02 | if (version_compare(phpversion(), "5.3.0", ">=") == 1) |
03 | error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); |
05 | error_reporting(E_ALL & ~E_NOTICE); |
07 | $xml = new DOMDocument; |
08 | $xml->load('forum.xml'); |
09 | $xsl = new DOMDocument; |
10 | switch ($_POST['action']) { |
12 | $xsl->load('xslt/posts.xslt'); |
13 | $proc = new XSLTProcessor; |
14 | $proc->importStyleSheet($xsl); |
15 | $proc->setParameter('', 'topic_id', (int)$_POST['i']); |
18 | $xsl->load('xslt/forum.xslt'); |
19 | $proc = new XSLTProcessor; |
20 | $proc->importStyleSheet($xsl); |
23 | echo $proc->transformToXML($xml); |
Make attention to switch-case. For ajax response (when we clicking to our topics) I using first switch case (when action = posts). To determine which messages we will display (filter) I will pass ID of topic (topic_id) to our XSL script.
Step 3. CSS
Here are used CSS file:
css/styles.css
01 | body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0} |
02 | .main{background:#FFF;width:698px;min-height:500px;overflow:hidden;border:1px #000 solid;margin:3.5em auto 2em;padding:1em 2em 2em} |
04 | .forum{border:1px solid #B0BBCD} |
05 | .forum .header{color:#fff;background-color:#545F6F;padding:5px} |
06 | .forum .topic{border-bottom:2px solid #B0BBCD;background-color:#F3F2EF;padding:5px} |
07 | .forum .topic .desc{margin-left:30px;font-size:12px;color:#444} |
08 | .forum .topic .posts{border-top:1px solid #B0BBCD;display:none;margin-top:10px;padding:10px} |
09 | .forum .topic .posts .post{border-bottom:1px solid #B0BBCD;margin:5px} |
10 | .forum .post .date{font-size:10px;color:#444;text-align:right} |
Here are several styles of our forum – styles for topics, posts etc.
Step 4. JS
Here are used JS files:
js/jquery.min.js
This is just jQuery library, available in package.
js/main.js
01 | function loadPosts(obj, i) { |
02 | $('.topic#'+i+' .posts').load('index.php', |
03 | {action: 'posts', i: i}, |
05 | $(this).fadeIn('slow'); |
07 | obj.click(function(e) { |
08 | $('.topic#'+i+' .posts').slideToggle('slow'); |
14 | $('.topic').click(function() { |
15 | loadPosts( $(this), $(this).attr('id') ); |
When page loaded – I linking onClick events to our forum topics. After, when we clicking to Topic element, I using $.load to load posts of this Topic. And, unbind old onClick functional (we don`t will need to load this content twice), and adding new onClick event, which will toggle slide of these posts.
Step 5. XSLT
And, the delicacy – used XSLT rules:
xslt/forum.xslt
01 | <?xml version="1.0" encoding="ISO-8859-1"?> |
04 | <xsl:template match="/"> |
07 | <script src="js/jquery.min.js" type="text/javascript">></script> |
08 | <script src="js/main.js" type="text/javascript"></script> |
09 | <link media="all" href="css/styles.css" type="text/css" rel="stylesheet"/> |
10 | <title>Our XSLT-based ajaxy forum</title> |
14 | <h2>Our XSLT-based ajaxy forum</h2> |
16 | <div class="header">Forum topics</div> |
17 | <xsl:for-each select="forum/topic"> |
19 | <xsl:attribute name="id"> |
20 | <xsl:value-of select="position()"/> |
22 | <a class="top_link" href="javascript:void(0)"> |
23 | <xsl:value-of select="@title"/> (<xsl:value-of select="count(child::*)"/>) |
26 | <xsl:value-of select="@description"/> |
28 | <div class="posts"></div> |
33 | <xsl:comment>Copyright: AndrewP</xsl:comment> |
As you can see, here we generating our main forum, and drawing only topics now. We will load posts ajaxy fr each Topic using jQuery, nice, isn`t is?
xslt/posts.xslt
01 | <?xml version="1.0" encoding="ISO-8859-1"?> |
03 | <xsl:template match="/"> |
04 | <xsl:for-each select="forum/topic[position() = $topic_id]/*"> |
06 | <xsl:attribute name="id"> |
07 | <xsl:value-of select="position()"/> |
09 | <xsl:value-of select="description" disable-output-escaping="yes"/> |
11 | posted at <xsl:value-of select="@date"/> by <xsl:value-of select="@author"/> |
Second XSL file we will use to generate list of posts for requested Topic. Hope that you don`t forget that we will request it ajaxy? We already passed $topic_id variable into this XSL above (in PHP code). So, we just should display all child ‘posts’ of our Topic.
Conclusion
I hope that today’s article was very interesting, and I hope that interest to XSL language has not dried up 🙂 Good luck!