Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little
Posted: Tue Dec 15, 2009 9:58 pm
It always helps to read and understand the syntax of the programming and templating languages that you use.
According to Joel Spoelsky, string operations are generally the most resource intensive functions.
Searching a string is comparable to a painter who is told to paint a yellow line on the road, but he leaves the paint bucket at the starting point. Each time he walks back to the bucket to re-dip his brush, the trip gets longer. If the line is only 20' long, this won't take too long. But if the line is 300' long, the rate of progress slows linearly with the distance.
Parsing strings unnecessarily slows down the rendering of your web page by the server. The longer the string, the more cpu time it takes to parse it.
In SMARTY tags, double quotes are parsed for variables, while single quotes are taken as a literal string.
So this:
{content block="Block Number 1" oneline="true" assign="block_1"}
requires three parse operations.
But this:
{content block='Block Number 1' oneline='true' assign='block_1'}
requires none.
The only time that you want to use double quotes in a smarty tag is when the string contains a variable reference such as this:
{assign var='pagetitle' value="This is the $page_alias page."}
Assuming the page alias is "home" the tag above will produce the following value in $pagetitle:
"This is the home page."
The CMSMS default templates that come with {cms_module module="something" template="somethingelse" etc.} will run faster if you convert them to single quotes.
According to Joel Spoelsky, string operations are generally the most resource intensive functions.
Searching a string is comparable to a painter who is told to paint a yellow line on the road, but he leaves the paint bucket at the starting point. Each time he walks back to the bucket to re-dip his brush, the trip gets longer. If the line is only 20' long, this won't take too long. But if the line is 300' long, the rate of progress slows linearly with the distance.
Parsing strings unnecessarily slows down the rendering of your web page by the server. The longer the string, the more cpu time it takes to parse it.
In SMARTY tags, double quotes are parsed for variables, while single quotes are taken as a literal string.
So this:
{content block="Block Number 1" oneline="true" assign="block_1"}
requires three parse operations.
But this:
{content block='Block Number 1' oneline='true' assign='block_1'}
requires none.
The only time that you want to use double quotes in a smarty tag is when the string contains a variable reference such as this:
{assign var='pagetitle' value="This is the $page_alias page."}
Assuming the page alias is "home" the tag above will produce the following value in $pagetitle:
"This is the home page."
The CMSMS default templates that come with {cms_module module="something" template="somethingelse" etc.} will run faster if you convert them to single quotes.