Although Dreamweaver offers many options for using the visual interface of the Document
window, sometimes you just have to tweak the code by hand. Dreamweaver’s acceptance by
professional coders is due in large part to the easy access of the underlying code. Dreamweaver
includes several methods for directly viewing, inputting, and modifying code for your Web page.
For large-scale additions and changes, you might consider using an external HTML editor such
as BBEdit or Homesite, but for many situations, the built-in Code view and Code inspector are
perfectly suited and much faster to work with.
Code view is one of the coolest tools in Dreamweaver’s code-savvy toolbox. You can either
view your code full-screen in the Document window, split-screen with Design view, or in a
separate panel, the Code inspector. The underlying engine for all Code views is the same—
and, for Dreamweaver, the code editor has had significant enhancements made to the feature
set and performance.
You can use either of the following methods to display the full-screen Code view:
✦ Select View ➪ Code.
✦ Choose the Show Code View button from the toolbar. Code view is displayed, as shown
in Figure 6-7.
You can access the split-screen Code and Design view with any of the following methods:
✦ Choose View ➪ Code and Design.
✦ Select the Show Code and Design Views button on the toolbar.
✦ Press Ctrl+` (Command+`) when in Design view and the Code inspector is closed. The `
character is the grave accent paired with the tilde (~) on keyboards.
Caution
Chapter 6 ✦ Accessing the Code Directly 235
Figure 6-7: Code view.
To change the relative size of the Code and Design views, drag the splitter bar up or down. In
the split-screen Code and Design view, Code view is shown on top of the Design view. You can
reverse that order by choosing View ➪ Design View on Top or selecting Design View on Top
from the View Options button on the toolbar.
You have several ways to open the Code inspector:
✦ Choose Window ➪ Code Inspector.
✦ Select the Show Code Inspector button in the Launcher.
✦ Use the keyboard shortcut F10.
Once opened, the Code inspector (see Figure 6-8) behaves like any other floating panel in
Dreamweaver: The window can be resized, moved, or hidden, and the inspector can be
docked above or below the Document window or dragged out onto its own panel. When the
Code inspector is opened initially, it is automatically selected. If you click in the Document
window with the Code inspector open, the inspector dims but still reflects changes made in
the document.
In all Code views, Dreamweaver does not update the Design view of the document
immediately—whereas changes in Design view are instantly reflected in any open Code
view. This delay is enforced to enable the code to be completed before being applied. To
apply modifications made in the code, switch to Design view; if Design view is open, click
anywhere in it to give it focus. Should Dreamweaver detect any invalid HTML, such as an
improperly closed tag, the offending code is flagged with a yellow highlight in both Design
and Code views. Select the marked tag to see an explanation and suggestions for correcting
the problem in the Property inspector.
236 Part II ✦ Web Design and Layout
Figure 6-8: To update Design view while still working with the Code inspector, select the
handy Refresh button—either on the toolbar or the Property inspector—or choose F5.
You can also apply code changes to Design view by saving the document or by choosing the
Refresh button on the toolbar or the Property inspector. The Refresh button becomes active
only when modifications are made in any Code view. You also have a keyboard and menu
alternative: pressing F5 has the same effect as choosing View ➪ Refresh Design View.
Generally, the Code view and Code inspector act like a regular text editor. Simply click anywhere
in the inspector to add or modify code. Double-click a word to select it. Select an entire
line by moving your pointer to the left edge of the code—where the pointer becomes a rightpointing
arrow—and clicking once. Multiple lines can be selected in this same fashion by
dragging the right-pointing arrow. Once a section of code is selected, you can drag and drop it
into a new location; pressing the Ctrl (Option) key while dragging makes a copy of the selection.
Moving from word to word is accomplished by pressing Ctrl (Command) in combination
with any of the arrow keys.
Enabling Code view options
Some special features in Dreamweaver’s code editor simplify the task of writing HTML and
other types of code. When in any Code view, some of these features can be toggled on and off
by choosing the command either from the View ➪ Code View Options list or under the View
Options button on the Document toolbar:
✦ Word Wrap: Wraps lines within the boundaries of the Code view window or Code
inspector to eliminate the need for horizontal scrolling.
Chapter 6 ✦ Accessing the Code Directly 237
✦ Line Numbers: Displays a number for every line in the code; this feature is extremely
helpful when used in combination with the JavaScript Debugger, which reports the line
number of an error in the code.
✦ Highlight Invalid HTML: Toggles the highlighting of invalid tags in Code view when
Design view is refreshed. Invalid tags are always highlighted in the Design view.
✦ Syntax Coloring: Syntax coloring makes code easier to read. Basic tags and keywords
are shown in one color, with text in another color. Three different types of code are
given different colors: Reserved Keywords, Other Keywords, and Strings. These
colors are set in the Code Color category of Preferences. You can also set a color for
an individual tag to further distinguish it if you like.
✦ Auto Indent: Auto Indent is another feature intended to improve code readability. With
Auto Indent enabled, pressing Enter (Return) at the end of a line causes the new line to
start at the same indentation as the preceding line. Press Backspace (Delete) to move
the indented line closer to the left margin. The number of characters for each indentation
is set in the Code Format category of Preferences.
You can also easily change the indentation—in or out—for selected blocks of code. To
further indent a block of code, select it and then choose Tab. To decrease the level of
indentation for a selected code block, choose Shift+Tab. Alternatively, you can select Edit ➪
Indent Code or use the keyboard shortcut Ctrl+> (Command+>) to indent a code block, and
Edit ➪ Outdent Code or the keyboard shortcut Ctrl+< (Command+<) to outdent it.
As a further aid to help you find your way through a maze of code, Dreamweaver includes the
Balance Braces command. JavaScript is notorious for using parentheses, brackets, and curly
braces to structure its code—and it’s easy to lose sight of where one enclosing brace begins
and its closing mate ends. Dreamweaver highlights the content found within the closest pair
of braces to the cursor when you select Edit ➪ Balance Braces or use the keyboard shortcut
Ctrl+’ (Command+’). If you select the command again, the selection expands to the set of
surrounding braces. When the selection is not enclosed by parentheses, brackets, or curly
braces, Dreamweaver sounds an alert.
Although most Web designers prone to using the code editor in Dreamweaver prefer to
manually enter their code, the power of the Insert bar is still at your disposal for rapid code
development. Any element available from the Insert bar can be inserted directly into Code
view or the inspector. To use the Insert bar, you must first position your cursor where you
would like the code for the object to appear. Then select the element or drag and drop the
element from the Insert bar to Code view or the inspector.
Keep in mind that Dreamweaver’s code editor is highly customizable. You can change the
way the lines wrap, by using indents for certain tag pairs; you can even control the amount
of indentation. All the options are outlined for you in Chapter 4.
Printing code
Although you may spend the vast majority of your time writing, modifying, and debugging
your code onscreen, there are times when you need to see it in hard copy.
Dreamweaver now offers the option of printing out your code. Choose File ➪ Print Code to
open the standard operating system Print dialog box. You have the option to print all of the
code or a selection; you cannot, however, print individual pages of your code. Press the keyboard
shortcut, Ctrl+P (Command+P) to send your code directly to the printer. Although
Dreamweaver does not print the syntax coloring, you can print line numbers just by enabling
them in Code view options.
No comments:
Post a Comment