Month: February 2014

Smarty 2 in PHP 5.5

When running a PHP powered website using Smarty 2 on a PHP 5.5 webserver it shows a deprecation error. This is caused by a deprecation of the /e parameter in the preg_replace function. Fortunately on the Smarty forum a topic is
available that provides a solution, without having to lower error_reporting in the server configuration.

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in xxx/xxxx/php5/smarty/base/Smarty_Compiler.class.php on line 270 

The original code on line 270 of _Smarty_Compiler.php is:

/* replace special blocks by "{php}" */
$source_content = preg_replace($search.'e', "'"
                                    . $this->_quote_replace($this->left_delimiter) . 'php'
                                    . "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
                                    . $this->_quote_replace($this->right_delimiter)
                                    . "'"
                                    , $source_content);

The culprit here is the /e modifier (PREG_REPLACE_EVAL)

The replacement to remove the deprecation notice is to use a preg_replace_callback:

$source_content = preg_replace_callback($search, create_function ('$matches', "return '"
                                   . $this->_quote_replace($this->left_delimiter) . 'php'
                                   . "' . str_repeat(\"\n\", substr_count('\$matches[1]', \"\n\")) .'"
                                   . $this->_quote_replace($this->right_delimiter)
                                   . "';")
                                   , $source_content); 

Leave a Comment

Second Crack Template Changes

An important change to the Second Crack template is to have a personal design that matches the goal of your blog. I wanted to keep it clean and simple and found a template design Simplesive that matches my preferences.

After modifying the template (originally made for Blogspot) I found that the Second Crack engine only allows you to display the full post on every page (overview and detail). You can circumvent this behavior by adding some changes to the template.
Besides that external links also need to be opened in an external window.

Open external links in a new window

My preference is to have links within the blog in the same window and to open external links in a new window or tab. Markdown does not have support for specifying a target for links, so I solved it using a piece of javascript I found.

//force external links to open in a new window, http://stackoverflow.com/questions/4425198/markdown-target-blank
var links = document.links;
for (var i = 0, linksLength = links.length; i < linksLength; i++) {
   if (links[i].hostname != window.location.hostname) {
       links[i].target = '_blank';
   } 
}

Read more function

The template must be modified in order to display it in a shorter version, or the full version. Every post must also include a marker (). On an overview page this marker and everything below it will be removed and replaced by a read more link.
On a detail page this marker is removed and the entire post is displayed. A detail page in Second Crack has the $pageType post

Template function

<?php if( !function_exists('tpl_getPostContents') ) { 
    function tpl_getPostContents( $postBody, $pageType, $permaLink, $moreLink = '<p class="readmore"><a href="%s">Read on</a></p>') {
    $excerptOnly = $pageType !== 'post';

    $morePatternPart = '<!---\s*more\s*--->';

    if( !$excerptOnly ) {
        //remove the comment from the source
        return preg_replace('/' . $morePatternPart . '/ms', '', $postBody);
    } else {
        $matches = null;
        if( preg_match('/^(.*)' . $morePatternPart . '/ms', $postBody, $matches) ) {
            return $matches[1] . sprintf($moreLink, $permaLink);
        } else {
            return $postBody; //there is no more part
        }
    }
} } ?>

The function starts with a check if the function already exists. This is necessary because for every page the Second Crack generates the template is included.

Display post

<div class="post-outer">
    <div class="post hentry">
        <h3 class="entry-title"><a href="<?php echo h($post['post-permalink-or-link']) ?>"><?php echo h($post['post-title']) ?></a></h3>
        <span class="post-timestamp">By Daan at <?php echo date('F j, Y', $post['post-timestamp']) ?></span>
        <div class="post-body entry-content">
            <?php echo tpl_getPostContents($post['post-body'], $content['page-type'], $post['post-permalink-or-link']); ?>
            <div class="post-footer-line post-footer-line-2"><span class="post-labels">
                Tags: <?php $tagCount = count($post['post-tags']); $iter = 0; foreach( $post['post-tags'] as $tag => $value ) { ?>
                    <a href="/tagged-<?php echo $tag; ?>.html" rel="tag"><?php echo $tag; ?></a><?php if( $iter < $tagCount-1) echo ', '; ?> 
                <?php $iter++; } ?>
                </span>
            </div>
        </div>
    </div>
</div>

In the post the function is used to display the contents.

RSS template Change

In the RSS feed I use the same function tpl_getPostContents to display the RSS contents. I do not include the function itself there, the main.php template file already defines the function.

Core change

I noticed that when validating the generated rss.xml that there were warnings about the timestamp in the post_date field. To fix this I updated the Post.php file with the following change:

In Post.php: function array_for_template() I changed this line:

'post-rss-date' => date('D, d M Y H:i:s T', $this->timestamp),
to:
post-rss-date' => date('r', $this->timestamp),

The difference is that the r modifier in the PHP date() function generates an RFC 2822 formatted date, which is a better format in the XML than the original date format in the Second Crack code.

Leave a Comment