admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
Derek Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Alternate PHP Syntax for View Files</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
| 19 | <meta name='author' content='Rick Ellis' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 |
|
| 22 | </head>
|
| 23 | <body>
|
| 24 |
|
| 25 | <!-- START NAVIGATION -->
|
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 27 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
| 28 | <div id="masthead">
|
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 30 | <tr>
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 33 | </tr>
|
| 34 | </table>
|
| 35 | </div>
|
| 36 | <!-- END NAVIGATION -->
|
| 37 |
|
| 38 |
|
| 39 | <!-- START BREADCRUMB -->
|
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 41 | <tr>
|
| 42 | <td id="breadcrumb">
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 43 | <a href="http://www.codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Alternate PHP Syntax
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 47 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | </tr>
|
| 49 | </table>
|
| 50 | <!-- END BREADCRUMB -->
|
| 51 |
|
| 52 | <br clear="all" />
|
| 53 |
|
| 54 |
|
| 55 | <!-- START CONTENT -->
|
| 56 | <div id="content">
|
| 57 |
|
| 58 | <h1>Alternate PHP Syntax for View Files</h1>
|
| 59 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 60 | <p>If you do not utilize CodeIgniter's <a href="../libraries/parser.html">template engine</a>, you'll be using pure PHP
|
admin | b93464d | 2006-10-31 00:36:32 +0000 | [diff] [blame] | 61 | in your View files. To minimize the PHP code in these files, and to make it easier to identify the code blocks it is recommended that you use
|
| 62 | PHPs alternative syntax for control structures and short tag echo statements. If you are not familiar with this syntax, it allows you to eliminate the braces from your code,
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 63 | and eliminate "echo" statements.</p>
|
| 64 |
|
admin | b93464d | 2006-10-31 00:36:32 +0000 | [diff] [blame] | 65 | <h2>Automatic Short Tag Support</h2>
|
| 66 |
|
| 67 | <p><strong>Note:</strong> If you find that the syntax described in this page does not work on your server it might
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 68 | be that "short tags" are disabled in your PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly,
|
admin | cef2106 | 2006-10-30 17:13:13 +0000 | [diff] [blame] | 69 | allowing you to use that syntax even if your server doesn't support it. This feature can be enabled in your
|
| 70 | <dfn>config/config.php</dfn> file.</p>
|
| 71 |
|
admin | b93464d | 2006-10-31 00:36:32 +0000 | [diff] [blame] | 72 | <p class="important">Please note that if you do use this feature, if PHP errors are encountered
|
| 73 | in your <strong>view files</strong>, the error message and line number will not be accurately shown. Instead, all errors
|
| 74 | will be shown as <kbd>eval()</kbd> errors.</p>
|
| 75 |
|
admin | cef2106 | 2006-10-30 17:13:13 +0000 | [diff] [blame] | 76 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 77 | <h2>Alternative Echos</h2>
|
| 78 |
|
| 79 | <p>Normally to echo, or print out a variable you would do this:</p>
|
| 80 |
|
| 81 | <code><?php echo $variable; ?></code>
|
| 82 |
|
| 83 | <p>With the alternative syntax you can instead do it this way:</p>
|
| 84 |
|
| 85 | <code><?=$variable?></code>
|
| 86 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 87 |
|
| 88 |
|
| 89 | <h2>Alternative Control Structures</h2>
|
| 90 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 91 | <p>Controls structures, like <var>if</var>, <var>for</var>, <var>foreach</var>, and <var>while</var> can be
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 92 | written in a simplified format as well. Here is an example using foreach:</p>
|
| 93 |
|
| 94 | <code>
|
| 95 | <ul><br />
|
| 96 | <br />
|
| 97 | <var><?php foreach($todo as $item): ?></var><br />
|
| 98 | <br />
|
| 99 | <li><var><?=$item?></var></li><br />
|
| 100 | <br />
|
Derek Allard | bc53894 | 2007-04-03 11:42:31 +0000 | [diff] [blame] | 101 | <var><?php endforeach; ?></var><br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 102 | <br />
|
| 103 | </ul></code>
|
| 104 |
|
| 105 | <p>Notice that there are no braces. Instead, the end brace is replaced with <var>endforeach</var>.
|
| 106 | Each of the control structures listed above has a similar closing syntax:
|
| 107 | <var>endif</var>, <var>endfor</var>, <var>endforeach</var>, and <var>endwhile</var></p>
|
| 108 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 109 | <p>Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 110 | important!</p>
|
| 111 |
|
| 112 | <p>Here is another example, using if/elseif/else. Notice the colons:</p>
|
| 113 |
|
| 114 |
|
| 115 | <code><var><?php if ($username == 'sally'): ?></var><br />
|
| 116 | <br />
|
| 117 | <h3>Hi Sally</h3><br />
|
| 118 | <br />
|
| 119 | <var><?php elseif ($username == 'joe'): ?></var><br />
|
| 120 | <br />
|
| 121 | <h3>Hi Joe</h3><br />
|
| 122 | <br />
|
| 123 | <var><?php else: ?></var><br />
|
| 124 | <br />
|
| 125 | <h3>Hi unknown user</h3><br />
|
| 126 | <br />
|
| 127 | <var><?php endif; ?></var></code>
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 | </div>
|
| 132 | <!-- END CONTENT -->
|
| 133 |
|
| 134 |
|
| 135 | <div id="footer">
|
| 136 | <p>
|
admin | cef2106 | 2006-10-30 17:13:13 +0000 | [diff] [blame] | 137 | Previous Topic: <a href="managing_apps.html">Managing Applications</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 138 | ·
|
| 139 | <a href="#top">Top of Page</a> ·
|
| 140 | <a href="../index.html">User Guide Home</a> ·
|
| 141 | Next Topic: <a href="security.html">Security</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 142 | </p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 143 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 144 | </div>
|
| 145 |
|
| 146 | </body>
|
| 147 | </html> |