05: Replace the REPLACE

This is a temporary forum setup for the purpose of discussing the EMC standards
Locked
Nuttzy99
Registered User
Posts: 927
Joined: Fri Aug 03, 2001 7:09 am
Contact:

05: Replace the REPLACE

Post by Nuttzy99 » Fri Sep 12, 2003 3:21 am

Replace the REPLACE

REPLACE is a 1st Generation MOD Template command. It seems harmless enough. Now fast forward two generations to our current desire to get MODs working in harmony. REPLACE has become a wolf in sheep's clothing! It is widely used by many Authors and unbeknowst to them, they are setting loose a demon on unsuspecting source code!

The least you need to know:
  • Use REPLACE as little as posssible
  • Usually IN-LINE commands are much safer to use
  • Lines intended for subSilver may not be appropriate for other template files
  • REPLACE is too dumb to realize that the inserted text will overwrite any previous modifications without warning
[/b]
Evil Inside
Golden Rule #2: Use REPLACE as little as posssible. Whenever you use the REPLACE command you are presumming to know exactly 100% how the line(s) being inserted should look. If no MODs have been installed and you have only subSilver installed then technically you can know this. But this is the deception of REPLACE! If there are any previous MOD installations or templates other than subSilver installed then you cannot guarantee that you know how every line should look. REPLACE is evil because what works on a virgin board can be disasterous on a MODed board. Since REPLACE is widely used today (prior to EMC release), using it may prove to be an old habit that is hard to kill.

Great care needs to be taken when using REPLACE. There are times when it just makes sense to use the REPLACE command over other approaches. However, even on a virgin board you must be certain that what you are inserting is exactly correct because the text from the MOD script is inserted literally as it appears. Usually IN-LINE commands are much safer to use.

REPLACE in a TPL file
Using the REPLACE command in a TPL can be considered a no-no just about 100% of the time. Just b/c the formatting works on subSilver does not mean it is appropriate for other template files installed. For TPL's it is much better to use the IN-LINE commands that have finer control of manipulating the code.

To demonstrate the danger of REPLACE in TPL files, take for example this line from index_body.tpl from subSilver...

Code: Select all

<td class="row2" align="center" valign="middle" height="50"><span class="gensmall">{catrow.forumrow.POSTS}</span></td>
...and let's say you want to add {FOO} to it so it would look like this...

Code: Select all

<td class="row2" align="center" valign="middle" height="50"><span class="gensmall">{catrow.forumrow.POSTS} {FOO}</span></td>
...the problem is that if you have the blueGray template installed, there is a slight difference on the line where the row class uses row1 instead of row2...

Code: Select all

<td class="row1" align="center" valign="middle" height="50"><span class="gensmall">{catrow.forumrow.POSTS}</span></td>
...and REPLACING the line is going to obliterate this difference.

The previous example is a minor one at that. There really is no telling what difference there may be between templates. TABLE and FORM structures can vary greatly and indiscriminately inserting <TD>, <TR>, and form data can have unintended consequences that really break the template.


REPLACING previous MODs to death
Using REPLACE could spell death for any MODs that were previously installed. You cannot know what MODs may have already installed and REPLACE cannot compensate for this in your MOD script. REPLACE is too dumb to realize that the inserted text will overwrite any previous modifications without warning. When your MOD replaces text it is like using a sledgehammer on the source code that causes indiscriminate destruction of other MODs.

I seriously cringe whenever I see something like this (Niels will have to forgive me for using an old version of his MOD in this example :P )...

Code: Select all

# 
#-----[ FIND ]------------------------------------------ 
# 
$sql = "UPDATE " . USERS_TABLE . " 
	SET " . $username_sql . $passwd_sql . "user_email = '" . str_replace("\'", "''", $email) ."', user_icq = '" . str_replace("\'", "''", $icq) . "', user_website = '" . str_replace("\'", "''", $website) . "', user_occ = '" . str_replace("\'", "''", $occupation) . "', user_from = '" . str_replace("\'", "''", $location) . "', user_interests = '" . str_replace("\'", "''", $interests) . "', user_sig = '" . str_replace("\'", "''", $signature) . "', user_sig_bbcode_uid = '$signature_bbcode_uid', user_viewemail = $viewemail, user_aim = '" . str_replace("\'", "''", str_replace(' ', '+', $aim)) . "', user_yim = '" . str_replace("\'", "''", $yim) . "', user_msnm = '" . str_replace("\'", "''", $msn) . "', user_attachsig = $attachsig, user_allowsmile = $allowsmilies, user_allowhtml = $allowhtml, user_allowbbcode = $allowbbcode, user_allow_viewonline = $allowviewonline, user_notify = $notifyreply, user_notify_pm = $notifypm, user_popup_pm = $popuppm, user_timezone = $user_timezone, user_dateformat = '" . str_replace("\'", "''", $user_dateformat) . "', user_lang = '" . str_replace("\'", "''", $user_lang) . "', user_style = $user_style, user_active = $user_active, user_actkey = '" . str_replace("\'", "''", $user_actkey) . "'" . $avatar_sql . "

# 
#-----[ REPLACE WITH ]------------------------------------------ 
# 
$sql = "UPDATE " . USERS_TABLE . " 
	SET " . $username_sql . $passwd_sql . "user_email = '" . str_replace("\'", "''", $email) ."', user_icq = '" . str_replace("\'", "''", $icq) . "', user_website = '" . str_replace("\'", "''", $website) . "', user_occ = '" . str_replace("\'", "''", $occupation) . "', user_from = '" . str_replace("\'", "''", $location) . "', user_interests = '" . str_replace("\'", "''", $interests) . "', user_sig = '" . str_replace("\'", "''", $signature) . "', user_sig_bbcode_uid = '$signature_bbcode_uid', user_viewemail = $viewemail, user_aim = '" . str_replace("\'", "''", str_replace(' ', '+', $aim)) . "', user_yim = '" . str_replace("\'", "''", $yim) . "', user_msnm = '" . str_replace("\'", "''", $msn) . "', user_attachsig = $attachsig, user_allowsmile = $allowsmilies, user_allowhtml = $allowhtml, user_allowbbcode = $allowbbcode, user_allow_viewonline = $allowviewonline, user_notify = $notifyreply, user_notify_pm = $notifypm, user_popup_pm = $popuppm, user_timezone = $user_timezone, user_dateformat = '" . str_replace("\'", "''", $user_dateformat) . "', user_lang = '" . str_replace("\'", "''", $user_lang) . "', user_style = $user_style, user_active = $user_active, user_actkey = '" . str_replace("\'", "''", $user_actkey) . "'" . $avatar_sql . ", user_gender=$gender
...first of all, don't even get me going on all the problems with the FIND command here. This example is fairly typical of MODs released prior to EMC. This is a VERY popular bit of code that many MODs need to use. If several MODs have been installed on this line already, then executing the REPLACE command will mean their destruction. Using the IN-LINE commands is the best way to ensure the code will not be damaged.

-Nuttzy :cool:
SpellingCow.com - Free spell check service for your forums or any web form!
My Other Site

Locked