joe codes

x-cart guru & custom programmer

  • About
  • Archives
  • Contact

Powered by Genesis

Smarty Strip

October 30, 2008 by joecodes Leave a Comment

If you are a Smarty expert then this will be very dumb. I knew about the cool Smarty tag, strip, but hadn’t found a really good use for it. When you are using Smarty you are changing the how the HTML is created when the page is loaded. In some cases, how the code is formatted affects how the browser displays it, especially for IE. One of the most common areas I’ve seen this is when formatting using an indented structure and having an image at the end of a block. By having the closing block tag (/div, /td, etc.) on a new line an extra bit of space is added behind or below the image. This extra space is usually noticeable and causes page rendering differences among browsers. Here is an example of this piece of code:

    <img src="myimage.gif" alt="" />
</div>

A workaround is to manually put the closing block tag on the previously line with no space like so:

<img src="myimage.gif" alt="" /></div>

This works fine but what if you don’t want to do that or you want to keep your Smarty template file looking nice and tidy with all of your extra lines and indents? This is where the Smarty strip tag comes in. Just add strip around the block of code and all those unwanted spaces are stripped out:

{strip}
    <img src="myimage.gif" alt="" />
</div>
{/strip}

This will build the page with the following source in that area:

<img src="myimage.gif" alt="" /></div>

This can also be useful if you are running through a loop building rows of a table. You might want to see the structure in a nice easy-to-read format but want the generated lines to be nice and tight. Just wrap it in strip:

<table ...>
{foreach ...}
{strip}
<tr>
    <td>...</td>
    <td>...</td>
    <td>...</td>
    <td>...</td>
</tr>
{/strip}
{/foreach}
</table>

This is nice to read and work with in the template but will generated the following tight source code when the page is loaded:

<table ...>
<tr><td>...</td><td>...</td><td>...</td><td>...</td></tr>
<tr><td>...</td><td>...</td><td>...</td><td>...</td></tr>
<tr><td>...</td><td>...</td><td>...</td><td>...</td></tr>
</table>

Yeah, you probably already knew this but I thought this was really useful. I used to build web apps using Mason and it had a simple way to suppress new lines so using strip is a nice substitute for that feature I used in Mason. Hope you find this useful! :)

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to email this to a friend (Opens in new window)

Related

Filed Under: Programming Tagged With: Mason, Smarty

Leave a Reply Cancel reply

You must be logged in to post a comment.

Quick Thoughts

  • I was surprised to learn that foreach in JavaScript does not have a traditional break. The loop will run to completion.

  • Who knew that combination sums across all permutation lengths of an array would be so difficult? It was a challenge but the final product looks good and takes a lot of resources. Limiting the max length for basic memory limits. Would only do something like this for occasional reporting.

  • Working on a new project that can have hundreds of forms on a page. The browser was spending way too much time in Parse HTML. Wasted a bunch of time before learning this is a long-standing bug in Chrome when there are many forms or inputs. Other browsers are fine.

Recent Posts

  • Progressive Enhancement
  • Keychain Password Search
  • Smarty preg_match
  • iPhone Plus Experiment
  • Time Machine Speed-Up Tip

Tag Cloud

Apple JavaScript Mason Perl PHP security simple Smarty speed stability Tax WWDC X-Cart

Search

Subscribe

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Comments

  • Bad App Alert on Startup Item Help
  • iPhone Pre-Order Needs to Change on iPhone Pre-Order Warning
  • Apple vs Pro on My Personal Three S’s of Development
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.