Help - Search - Members - Calendar
Full Version: Encode_php For Dynamic Templates
Movable Type Community Forum > Other Product Discussion > Bugs and Odd Behavior
chipple
I needed to use encode_php in a dynamic template, and found out that the current function was in a "TODO" state (see below) that simply didn't do the work, as it added slashes to all single-quotes and double-quotes (etc.) which resulted in unneeded backslashes.
So I translated the Util.pm encode_php function to a PHP equivalent.

/php/lib/modifier.encode_php.php
Before:
CODE
<?php
function smarty_modifier_encode_php($text, $type) {
    // TODO: needs work
    return addslashes($text);
}
?>
After:
CODE
<?php
function smarty_modifier_encode_php($text, $type) {
    switch ($type) {
        case 'qq':
            $out = encode_phphere($text);
            $out = str_replace('"','\"',$out);
            break;
        case 'here':
            $out = encode_phphere($text);
            break;
        default:  // 'q'
            $out = str_replace("\\","\\\\",$text);
            $out = str_replace("'","\\'",$out);
            break;
    }
    return $out;
}
function encode_phphere($text) {
    $out = str_replace("\\","\\\\",$text);
    $out = str_replace('$','\$',$out);
    $out = str_replace("\n",'\n',$out);
    $out = str_replace("\r",'\r',$out);
    $out = str_replace("\t",'\t',$out);
    return $out;
}
?>
bschoate
That slipped through our tests. Curious though-- what are you using it for in dynamic publishing? I would typically assign a value like this:

CODE
<?php $entry = $this->tag('MTEntryBody');


There's no need to escape anything at all with that approach...
chipple
Thanks for your reply!

I'm quite new to dynamic templates, so perhaps what I'm doing doesn't make any sense. Indeed I think you're definitely right that there must be close to no use for encode_php in the dynamic templates.

Anyway I was using a template to output PHP code (that's to be later processed by another script from my site that requests the dynamic page), and that's where encode_php came into use... Something like:

CODE
<MTEntries>
<?
echo f_GetEntry('<$MTEntryPermalink encode_php="q"$>','<$MTEntryTitle encode_php="q"$>','<$MTEntryBody encode_php="q"$>','<$MTEntryMore encode_php="q"$>','<$MTEntryKeywords encode_php="q"$>');
?>
</MTEntries>


Thinking about it now, maybe I should try to move that code to a PHP Smarty function instead.
chipple
QUOTE
Thinking about it now, maybe I should try to move that code to a PHP Smarty function instead.


... which I did and that works much better. Thanks for the enlightenment. : )
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.