<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Karoshi Ethos &#187; Smarty</title>
	<atom:link href="http://karoshiethos.com/tag/smarty/feed/" rel="self" type="application/rss+xml" />
	<link>http://karoshiethos.com</link>
	<description>Navigating the treacherous waters of interactive technology</description>
	<lastBuildDate>Mon, 09 Aug 2010 15:23:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Smarty base href Modifier</title>
		<link>http://karoshiethos.com/2010/08/07/smarty-base-href-modifier/</link>
		<comments>http://karoshiethos.com/2010/08/07/smarty-base-href-modifier/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 00:36:30 +0000</pubDate>
		<dc:creator>Rob Ruchte</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Things that are broken]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://karoshiethos.com/?p=536</guid>
		<description><![CDATA[It appears that Safari 5 will not respect the base href tag when used in a page served via https when the base href indicates a URL with the http protocol. There is probably more to this issue, but I ran across it while working on an Authorize.net integration using their SIM API. After a [...]]]></description>
			<content:encoded><![CDATA[<p>It appears that <a href="http://www.apple.com/safari/">Safari 5</a> will not respect the base href tag when used in a page served via https when the base href indicates a URL with the http protocol. There is probably more to this issue, but I ran across it while working on an <a href="http://authorize.net/">Authorize.net</a> integration using their <a href="http://developer.authorize.net/api/sim/">SIM API</a>. After a successful transaction, Authorize.net requests a URL of the developer's choosing, and proxies the response through their server to the user's browser. This allows developers to do callback processing of the successful transaction on their server, and display a custom "thank you" page. Brilliant. The problem is, if you have any included CSS, JavaScript, or images in the page, you either need to give them all fully qualified URLs, or use the base tag to specify a root URL to use for all relative link attributes in the page (href, src, action, etc.). I've done this same type of integration many times, going back about six years, and have never had a problem using the base href in this scenario. Now, Safari 5 will completely ignore the base href and display a big mess of un-styled markup when you reach the end of the Authorize.net transaction flow.</p>
<p>I do a lot of work with the <a href="http://www.smarty.net/">Smarty</a> template engine, and the site I'm working on now is large, complex, and has a fairly advanced layout with lots of CSS and <a href="http://jquery.com/">jQuery</a> goodies. Everything is so abstracted, it's just not practical to construct a single template for my Authorize.net relay pages, of which there are several. It's also not practical to change the templates to use fully qualified URLs for everything. So what I did was write a simple Smarty modifier that prepends a specified URL to all of the relative src, href, and action attributes within the supplied string. It also fixes background: url() inline CSS styles. On my relay templates, all I have to do is wrap the contents of the template in a {capture} function, assign the output to a variable, and run it through my modifier. Absolute paths for all. Here's the modifier code, hopefully it will save someone a few grey hairs.</p>
<pre class="syntax brush-php">&lt;?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty base_href modifier plugin
 *
 * Type:     modifier&lt;br&gt;
 * Name:     base_href&lt;br&gt;
 * Purpose:  apply a base URL to all relative
 *				src, href, and background:url() attributes in a string
 * @author   Rob Ruchte &lt;rob care of thirdpartylabs com&gt;
 * @param string
 * @param string
 * @return string
 */
function smarty_modifier_base_href($string, $base_href)
{
	$base_href = (substr($base_href, -1)!='/') ? $base_href.'/':$base_href;

	$patterns = array();
	$patterns[] = '/href=\&quot;[^#]\/?([^\&quot;]*)\&quot;/i';
	$patterns[] = '/src=\&quot;\/?([^\&quot;]*)\&quot;/i';
	$patterns[] = '/action=\&quot;\/?([^\&quot;]*)\&quot;/i';
	$patterns[] = '/background: ?url\(\'?\/?([^\'|\)]*)\'?\)/i';
	$patterns[] = '/background-image: ?url\(\'?\/?([^\'|\)]*)\'?\)/i';

	$replacements = array();
	$replacements[] = 'href=&quot;'.$base_href.'$1&quot;';
	$replacements[] = 'src=&quot;'.$base_href.'$1&quot;';
	$replacements[] = 'action=&quot;'.$base_href.'$1&quot;';
	$replacements[] = 'background: url(\''.$base_href.'$1\')';
	$replacements[] = 'background-image: url(\''.$base_href.'$1\')';

    return preg_replace($patterns, $replacements, $string);
}

/* vim: set expandtab: */

?&gt;</pre>
<p>Usage:</p>
<pre class="syntax brush-html">{capture assign=output}
{include file=&quot;include/header.tpl&quot; title=&quot;Event Ticket Registration&quot;}
	&lt;div class=&quot;span-3&quot; id=&quot;leftbar&quot;&gt;
		[...]
	&lt;/div&gt;
	&lt;div class=&quot;span-11 last&quot; id=&quot;content&quot;&gt;
		&lt;div style=&quot;background: url(/images/feature.jpg);&quot;&gt;
			[...]
		&lt;/div&gt;
		&lt;div class=&quot;span-11 last&quot; id=&quot;copy&quot;&gt;
		&lt;div id=&quot;breadcrumbs&quot;&gt;
			{foreach from=$breadcrumbs item=currNav name=breadcrumbs}
				{if !$smarty.foreach.breadcrumbs.last}&lt;a href=&quot;{$currNav.path}&quot;&gt;{/if}
				{$currNav.title}
				{if !$smarty.foreach.breadcrumbs.last}&lt;/a&gt;&amp;nbsp;&amp;raquo;&amp;nbsp;{/if}
			{/foreach}
		&lt;/div&gt;
        &lt;h2&gt;Registration: {$event-&gt;getTitle()|escape}&lt;/h2&gt;
	    {$purchaseSuccess}
        &lt;/div&gt;
    &lt;/div&gt;
{include file=&quot;include/footer.tpl&quot;}
{/capture}
{$output|base_href:$smarty.const.BASE_URL}</pre>
]]></content:encoded>
			<wfw:commentRss>http://karoshiethos.com/2010/08/07/smarty-base-href-modifier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smarty File Size Modifier</title>
		<link>http://karoshiethos.com/2009/05/02/smarty-file-size-modifier/</link>
		<comments>http://karoshiethos.com/2009/05/02/smarty-file-size-modifier/#comments</comments>
		<pubDate>Sun, 03 May 2009 02:30:59 +0000</pubDate>
		<dc:creator>Rob Ruchte</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://karoshiethos.com/?p=342</guid>
		<description><![CDATA[Here's a simple Smarty modifier that will format an integer that represents the number of bytes in a file as a human readable string.
Usage:
{$fileSizeInBytes&#124;file_size}
Example:
{assign var=fileSizeInBytes value=10485760}
{$fileSizeInBytes&#124;file_size}
{assign var=fileSizeInBytes value= 768000}
{$fileSizeInBytes&#124;file_size}
{assign var=fileSizeInBytes value=303}
{$fileSizeInBytes&#124;file_size}
Output:
10 MB
750 Kb
303 bytes

&#60;?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty file_size modifier plugin
 *
 * Type:    [...]]]></description>
			<content:encoded><![CDATA[<p>Here's a simple <a href="http://smarty.net/">Smarty</a> modifier that will format an integer that represents the number of bytes in a file as a human readable string.</p>
<p><strong>Usage:</strong><br />
{$fileSizeInBytes|file_size}</p>
<p><strong>Example:</strong><br />
{assign var=fileSizeInBytes value=10485760}<br />
{$fileSizeInBytes|file_size}</p>
<p>{assign var=fileSizeInBytes value= 768000}<br />
{$fileSizeInBytes|file_size}</p>
<p>{assign var=fileSizeInBytes value=303}<br />
{$fileSizeInBytes|file_size}</p>
<p><strong>Output:</strong><br />
10 MB<br />
750 Kb<br />
303 bytes</p>
<pre class="syntax brush-php">
&lt;?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty file_size modifier plugin
 *
 * Type:     modifier&lt;br&gt;
 * Name:     file_size&lt;br&gt;
 * Purpose:  format file size represented in bytes into a human readable string&lt;br&gt;
 * Input:&lt;br&gt;
 *         - bytes: input bytes integer
 * @author   Rob Ruchte &lt;rob at thirdpartylabs dot com&gt;

 * @param integer
 * @return string
 */
function smarty_modifier_file_size($bytes=0)
{
    $mb = 1024*1024;

    if ($bytes &gt; $mb)
    {
        $output = sprintf (&quot;%01.2f&quot;,$bytes/$mb) . &quot; MB&quot;;
    }
    elseif ( $bytes &gt;= 1024 )
    {
        $output = sprintf (&quot;%01.0f&quot;,$bytes/1024) . &quot; Kb&quot;;
    }
    else
    {
        $output = $bytes . &quot; bytes&quot;;
    }

    return $output;
}

/* vim: set expandtab: */

?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://karoshiethos.com/2009/05/02/smarty-file-size-modifier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
