Skip to content

wikibook code template info

Lawrence edited this page Jan 15, 2013 · 1 revision

Table of Contents

Documentation

The purpose of this series of templates is to increase the readability of code examples in programming books. Each template includes a div with an colored outline and colored background, and a second div with a colored outline and white background. Inside the first div is a space which allows a comment or other instruction; inside the second div is the code space. The include code is used along with two arguments, the first being the comment and the second being the code.

Due to parsing limitations, the current version of the code template requires the user to include <pre></pre> tags around the code sample. (Inclusion of the <pre></pre> tags causes the wiki parser to render the wiki include code as plain text instead of parsing it.) This may change if I am able to hack around the parser and insert the include tag manually. (Note you can now do this using {{#tag:pre|content}} Yan (talk) 21:38, 13 November 2008 (UTC))

Templates initially created by banzaimonkey 01:04, 6 August 2006 (UTC) for the PHP wikibook. These templates are intended to be usable in all programming wikibooks, but specific alterations or exceptions may be required for a particular programming language. Please be extremely careful when modifying these templates, as your changes may be propagated across a large number of pages unbeknownst to you. If you'd like to use a code template that is not described below, feel free to add your own and include it in the list (please also include the "Code Template" category on each new template's page, inside <noinclude></noinclude> tags; see an existing template as an example).

Another concern I have is that these will add large sections of color to "Print" pages on wikibooks that use them, thus vastly increasing printing costs. If anyone has a suggestion on how to filter these to white (see template Misc2, below), please leave a note on the discussion page.

Why Use Code Templates?

Why use code templates? Isn't there already a blue box around code?

The design presentation using the code template / code box highlights the code much more obviously, allowing the reader to see the code much more easily. Thus, the user is able to focus his or her energy on parsing the code only, rather than having to parse the wiki page to figure out which sections actually are code. Because there is room for comment text inside the code template, it is easier to attach a specific note, instruction, or link to a particular piece of code. This affords the author greater clarity when making reference to code examples. The code template also allows the code to be easily recognized and edited inside the wiki source because it has distinct bracketing (from the template code itself). Finally, some of the alternate stylesheets to not designate code with anything more than monospace font.

Usage Notes

  • I highly recommend the use of <pre></pre> tags to format code inside the code template. This allows code to break lines and allows additional code to be added easily. It also allows authors to establish an indentation scheme, which increases readability.
  • With the inclusion of the Highlight extension to wikibooks, you can now use <source lang="x"> </source> which is automatically converted to <pre></pre> with colour-formatted code.
  • All code templates must include the correct number of vertical lines |, even if the argument is blank. If you don't want to enter text after a particular argument, that's fine, but you still need the vertical line.

Code Templates

Example Template Usage

Code:Basic

The code for the above code box can be seen here:

{{Code:Basic
|This box can show code or simply be used to highlight a particular section of text with a blue outline.
|Pie
#Cherry
#Apple
#Peach
&lt;source lang="perl"&gt;$array_pie = {Cherry, Apple, Peach};&lt;/source&gt;
|The explanation can be continued after the code sample.
}}

Code:Valid

The code for the above code box can be seen here:

{{Code:Valid
|The following code is valid:
|&lt;pre&gt;$valid code example;&lt;/pre&gt;
|The explanation can be continued after the code sample.
}}

Code:Error

The code for the above code box can be seen here:

{{Code:Error
|The following code contains an error:
|&lt;pre&gt;$this line does not end with a semicolon&lt;/pre&gt;
|The explanation can be continued after the code sample.
}}

Code:Alt

The code for the above code box can be seen here:

{{Code:Alt
|You can use either "echo" or "print" to output text.
|&lt;pre&gt;echo "Hello World.";&lt;/pre&gt;
|and / or / not / etc.
|&lt;pre&gt;print "Hello World.";&lt;/pre&gt;
|The explanation can be continued after the code sample.
}}

Code:Output

The code for the above code box can be seen here:

{{Code:Output
|In this example, you'll be able to see the code and the output it generates.
|&lt;pre&gt;print "Hello World.";&lt;/pre&gt;
|&lt;pre&gt;Hello World.&lt;/pre&gt;
|The explanation can be continued after the code sample.
}}

Code:Tip

The code for the above code box can be seen here:

{{Code:Tip
|It's a good idea to add comments to your code.
|&lt;pre&gt;//This variable controls the loop
$loopvar1;&lt;/pre&gt;
|The explanation can be continued after the code sample.
}}

Code:Transclude

This can be used to create a transcluded, titled page, like so

BA Logo

Bibliotheca Anonoma

BASLQC Wiki

  • Introduction - A quick intro to the rationale and ideals of this guide, and modding in general.
  • Essential Maker Skills - Essential Skills that every maker should have.
  • Archivist Tools - All the tools an internet archivist needs under their belt.

General Guides

  • Android Development Codex - All kinds of Android smartphones.
  • Business Class Laptops - Computers that last: The ThinkPad, the HP EliteBook, the Dell Precision/Latitude.
  • EBook Readers - A good eInk screen remains the best way to read literature comfortably.
  • Game Consoles - Homebrew development scenes have made it possible to unlock the full power of your game console's computer chip.
  • Graphing Calculators - This is the last vestige of the age of 80's Home Computers; where programs were simple and graphics were minimal.
  • Home Server - Why buy cloud storage when you can build your own cloud? For media streaming, torrenting, and VPN access (to bypass blocked internet).
  • Routers - Amazingly, your ordinary router probably runs Linux on it's little embedded CPU.
  • Authentication Wallets - Manage your plethora of accounts, passwords, and RSA public keys using a GPG-encrypted wallet.
  • LEGO Mindstorms - The easiest way to build functional robots and machines; using good ol' LEGO and Technic bricks.
  • Vintage Computers - Amiga, Apple ][, and all the other random home computers that defined the 1980s.

Research

  • Genetic Programming - Programs that mutate and evolve by themselves, like Genes. It's a very difficult concept to grasp, but a very powerful method that transcends math or algorithms.

Content Guidelines

  • General Guidelines - The ideals that you should uphold while working with and editing this guide.
  • Device Guide Templates - Templates and general guidelines for creating customized guides for a device.
  • Linux - Run a full desktop OS on your little mobile device; research is being made to make it comfortable to use in the mobile space.

Reference

  • Glossary - Contains all the crazy acronyms and word soup that you'll need to wade through when using this guide.
Clone this wiki locally