Although the advantages of XSL compared to PHP are obvious (at least for me), I still hear sometimes that PHP was better. On a closer look, I found out that many users do not know the advantages of XSL over PHP, so I want to compare both of them.
The output style
The output style is in my opinion the most important advantage of XSL. SobiPro presents the plain data in XML. XML does NOT describe how the data are displayed. These information are within the XSL template files where the layout can be freely changed.
The XML data cannot only be presented on a web site (HTML), but can be used to create any kind of output using a different XSL template.
To print the content of a page, only a new XSL template needs to be created. Add only those XML data to this template which should be printed, and style them accordingly. Such a print example can be found on our demo site in the Restaurants Guide; just click on the print button.
Another example are the feeds SobiPro creates. In fact, the feeds are created by an XSL template from the XML data SobiPro provides. The default feed template, that comes with SobiPro, creates feeds in Atom format. To change it to RSS 2.0 or add additional information, only the XSL template needs to be adapted.
In Sobi2, where we used PHP to present tha data, Sobi2 itself determined how the data should look like, and you had only the possibility to use CSS to change the visual view, or to re-arrange them in the template. The output was always only a HTML page. The possibilities to change the fix-coded layout were limited compared to XSL.
PHP has functions and XSL?
To manipulate data or even retrieve different data from the database, PHP could be used in Sobi2 templates. And what is with SobiPro and its XSL templates?
XSL provides specific XSL commands to manipulate data. They let you add/remove elements and attributes to or from the visible output, rearrange and sort
elements, perform tests and make decisions about which elements to hide and display, and a lot more.
As only XSLT 1.0 can be used (XSLT 2.0 is not available for PHP driven web servers), the available functionality is more limited than with PHP functions. But SobiPro provides also a solution for that. For those who need to use PHP methods to achieve a specific functionality not available within XSL, PHP still can be used.
Yes, you can use PHP in SobiPro templates!
More information can be read in PHP in SobiPro Templates.
The data information
You are ready to write your own XSL template, but need to know which XML data are available. Of course you can now subscribe to the SobiPro club and read the documents available, or ask for support in our ticket system.
But you can also just let SobiPro show you which XML data are available in a specific view, adapted to your specific configuration. Just add
/?xml=1 to the URL of a specific view, and check the source code of the shown page. It will show you all available data. For security reason
this feature is off by default and needs to be switched on first.
In PHP driven templates, you need to know how the specific elements are called and which elements are available, otherwise you are not able to use them at all.
Take a deeper look to how the XML data are presented using XSL by reading the article The Template Language XSL.
Complex syntax or only not used to it?
Another argument I often hear against XSL, is the so called 'complex syntax' comparing with the PHP syntax. But is that really true?
XSL commands need more letters, they are longer, perhaps that's why it looks complicated at first glance. Let us compare; for the PHP commands I use the commands we invented for Sobi2.
Output of the data of a field (the name of the street in this example), formatted according to the field settings.
To output the formatted data of the field a PHP function ($fieldsFormatted) has to be called.
To output the field formatted, a predefined template (showfield) can be called in XSL.
Output of the data of a field (the name of the street in this example), un-formatted.
To output the un-formatted data of the field a PHP function ($fieldsObjects) has to be called.
To output a field un-formatted, the plain XSL command for simple data can be used.
Our last example is an if clause to show the data of a field conditionally. If the value of the color field is 'blue', the formatted content of the sky field should be shown.
Another argument for PHP I hear is, that they can use HTML in the PHP templates.
Of course HTML can used in the same way in XSL. Even better, XSL commands and HTML commands can be mixed without the need of taking care of being within a PHP area or not.
Showing the title of an entry as link in a heading element.
If you take a closer look to a XSL command, you will see that the syntax is similar to HTML. It has an opening tag and a closing tag.
While you use
<tag> ... </tag> in HTML, you use
<xsl:command> ... </xsl:command> in XSL.
There are three types of possible errors, syntax errors, calling non-existent data and not well-formed commands or tags (HTML and XSL).
In case of non-existent data, I mean here e.g. calling a field which does not longer exist or is unpublished, PHP will throw a fatal error. XSL simply ignores such calls.
In case of syntactical errors, PHP again throws a fatal error. XSL returns a page with error code 500. On contrary to the real error 500, the 'Internal Server Error', this error 500 is generated by SobiPro by design. It shows you in most cases the reason why you got this page. As the error cannot always be shown on the page, they are logged in the SobiPro error log. The error log (accessible via SobiPro back-end) shows the reason for the problem including the file name and line where the problem is located.
To generate the error shown below, I called a XSL template 'blabla' what does not exist.
not well-formed commands or tags
As a design oriented language, XSL is more strict in using well-formed commands or tags. A command or tag is not well-formed, if there is only the opening tag/command and the closing tag/command is missing. In case of not well-formed commands (HTML and XSL), XSL returns a page with error code 500, indicating the file which cannot be parsed. E.g. 500 Cannot parse template file at /usr/templates/b3-default3/entry/details.xsl. PHP ignores such errors, but often the visual result is not like you want.
And finally - Books
As XSL templates are not written in a proprietary language like all PHP templates are (each developer uses its own syntax), there are several books and websites available to explain all aspects of XSLT. The description of XSLT in Wikipedia has a lot of additional links concerning this topic.Dedicated to my friend Ufuk
Sigrid lives in a small village near Frankfurt on the Main in Germany.
She studied electrotechnics before she started to work as a software engineer for industrial products using Microsoft C++/MFC.
Since 2009, Sigrid works full time for the Sobi projects.
Her programming experiences started with the programming languages Pascal and C, followed by C++ which is a good basis for developing software for Joomla!, especially since the well known to her object oriented developing techniques found their way into PHP.
Besides her development work for the Sobi project, she is mainly responsible for design solutions, quality management and for all business affairs and public relations. Sigrid is also in charge of the translations and documentations for SobiPro, as well as of the Sigsiu.NET company and demo websites.
Sigrid is member of the German J and Beyond e.V.. She is involved in Joomla! for many years. Just now she is helping in building the Joomla Template Directory. Her former positions include the assistant team leader of the Joomla! Events Team and team leader of the Joomla! Social Media team.
Sigrid enjoys cuisine, bee-keeping and the outdoors in her leisure time, including spending time working in the garden. She also enjoys taking photos, although she doesn't have as much time for photography lately.