Friday, 8 April 2011

Using javascript and CSS styles with Smarty

Placing CSS styles and some javascript code containing braces can cause some troubles when Smarty is used as template engine. This template engine shows the error like "Fatal error: unrecognized tag" in this situation.

There are some methods that allow to avoid Smarty errors like described bellow.

First method: the CSS styles or javascript code with braces are needed to be enclosed in Smarty tag {literal}...{/literal}. Template engine doesn't try to parse the code inside these tags and the code and styles will be shown in the HTML document successfully.

There is another way to avoid the errors. Another method is that if after opening braces white space or the end of the line symbol follows so these code is not needed to be placed inside the tags literal and it will work too.

Example:
<style>
   .some_class {color: red;}
</style>
This code will cause the Smarty error, but next example will work without any troubles:
{literal}
<style>
.some_class {color: red;}
</style>
{/literal}
But I'm not sure that this method works in all versions of Smarty. I checked it using Smarty engine version 3.0.7.

No comments:

Post a Comment