Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################################### |
| 2 | Alternate PHP Syntax for View Files |
| 3 | ################################### |
| 4 | |
| 5 | If you do not utilize CodeIgniter's :doc:`template |
| 6 | engine <../libraries/parser>`, you'll be using pure PHP in your |
| 7 | View files. To minimize the PHP code in these files, and to make it |
| 8 | easier to identify the code blocks it is recommended that you use PHPs |
| 9 | alternative syntax for control structures and short tag echo statements. |
| 10 | If you are not familiar with this syntax, it allows you to eliminate the |
| 11 | braces from your code, and eliminate "echo" statements. |
| 12 | |
| 13 | Automatic Short Tag Support |
| 14 | =========================== |
| 15 | |
| 16 | .. note:: If you find that the syntax described in this page does not |
| 17 | work on your server it might be that "short tags" are disabled in your |
| 18 | PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly, |
| 19 | allowing you to use that syntax even if your server doesn't support it. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 20 | This feature can be enabled in your *config/config.php* file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 21 | |
| 22 | Please note that if you do use this feature, if PHP errors are |
| 23 | encountered in your **view files**, the error message and line number |
| 24 | will not be accurately shown. Instead, all errors will be shown as |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 25 | ``eval()`` errors. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | |
| 27 | Alternative Echos |
| 28 | ================= |
| 29 | |
| 30 | Normally to echo, or print out a variable you would do this:: |
| 31 | |
| 32 | <?php echo $variable; ?> |
| 33 | |
| 34 | With the alternative syntax you can instead do it this way:: |
| 35 | |
| 36 | <?=$variable?> |
| 37 | |
| 38 | Alternative Control Structures |
| 39 | ============================== |
| 40 | |
| 41 | Controls structures, like if, for, foreach, and while can be written in |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 42 | a simplified format as well. Here is an example using ``foreach``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 44 | <ul> |
| 45 | |
| 46 | <?php foreach ($todo as $item): ?> |
| 47 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 48 | <li><?=$item?></li> |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 49 | |
| 50 | <?php endforeach; ?> |
| 51 | |
| 52 | </ul> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
| 54 | Notice that there are no braces. Instead, the end brace is replaced with |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 55 | ``endforeach``. Each of the control structures listed above has a similar |
| 56 | closing syntax: ``endif``, ``endfor``, ``endforeach``, and ``endwhile`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 57 | |
| 58 | Also notice that instead of using a semicolon after each structure |
| 59 | (except the last one), there is a colon. This is important! |
| 60 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 61 | Here is another example, using ``if``/``elseif``/``else``. Notice the colons:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 63 | <?php if ($username === 'sally'): ?> |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 64 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 65 | <h3>Hi Sally</h3> |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 66 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 67 | <?php elseif ($username === 'joe'): ?> |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 68 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 69 | <h3>Hi Joe</h3> |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 70 | |
| 71 | <?php else: ?> |
| 72 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 73 | <h3>Hi unknown user</h3> |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 74 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 75 | <?php endif; ?> |