作者worry (焦虑哥)
看板PHP
标题Re: [请益] 大家会怎麽去避免重复的html程式码
时间Thu Apr 4 00:27:02 2013
分合的时机先不论,要分要合都还是有些比较好看的作法
"All problems in computer science can be solved by another level of indirection"
要合的话
<?php
switch($client){
"deaf":
$show_text=true;
$show_audio=false;
$show_video=true;
break;
"blind":
$show_text=true;
$show_audio=true;
$show_video=false;
break;
"dog":
$show_text=false;
$show_audio=true;
$show_video=true;
break;
}
?>
<?php if($show_text): ?>
<div>text</div>
<?php endif; ?>
<?php if($show_audio): ?>
<div><embed src="audio.mp3" /></div>
<?php endif; ?>
<?php if($show_video): ?>
<div><embed src="video.mp4" /></div>
<?php endif; ?>
这样会比
<?php if($client=="deaf" || $client=="blind"):?>
<div>text</div>
<?php endif; ?>
<?php if($client=="blind" || $client=="dog"):?>
<div><embed src="audio.mp3" /></div>
<?php endif; ?>
<?php if($client=="deaf" || $client=="dog"):?>
<div><embed src="video.mp4" /></div>
<?php endif; ?>
好,多加一个间接层,把逻辑复杂但对照起来有意义的东西聚集在一起,
底下只让跟区块和跟那个区块有关系的变数同时出现。
--
要分的话,以下的作法对view来说可能不适合,但如果是controller/model,
或是不同客户使用同一个code base来改的话,就比较适合
在这种情境下,每一个版本其实都是某个原始版的分支。没错,就是要用VCS,
每个客制化版都从公版branch出来改,公版有任何修正的时候也方便移植回
每个分支。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.221.51.22
※ 编辑: worry 来自: 61.221.51.22 (04/04 00:27)