When I'm coding a form I find myself doing some very repetitive typing. For example, if I'm lining up a number of <input>
s in a table, I might write
<tr>
<td><label for="repeat">Repeat:</label></td>
<td><input id="repeat" name="repeat"></td>
</tr>
where the third "repeat"
is needed for GET/PUT form submission, the first and third are associated 开发者_开发问答with each other, the third is for DOM access, and the second is for the (human) reader.
If I avoid lining up the various inputs the need for the first goes away:
<p><label>Repeat: <input id="repeat" name="repeat"></label></p>
but usually I'm asked to make them line up.
Generally, I think of duplications in code (DRY) as a bad thing, so even minor as this is I thought I'd ask to see if there was a better way. As it stands, I have four opportunities for typos, three of which are user-facing and two which would cause programmatic issues if mistyped.
How about lining up using CSS (width or something) and keep using your code instead of tables and tds.
Well, obviously you are right about the wordiness of html, which doesn't always conform with the DRY-principle. And the next repetition comes in the server side code, that processes the form. But, as you put it correctly, each of these "repetitions" has its own meaning and could as well contain a different string.
Usually I try to take DRY to a higher level by not coding html forms by hand but using a web framework (in my case Django) which generates the html form automatically. That makes your question to a non-issue for me.
精彩评论