Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-16 of 16 total

Barebones xHTML and CSS template pages

Ok, so this guy already did this: http://www.bigbold.com/snippets/posts/show/583 but I needed a bit more. So this is my xHTML template page (it doesn't declare xml since it makes IE kick out of standards mode):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>XHTML TEMPLATE</title>
  
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="description" content="THIS IS A TEMPLATE XHTML PAGE" />
  
  <link rel="stylesheet" href="main.css" type="text/css" media="screen" />
  
  <!-- <script type="text/javascript" src="prototype.js">
  </script> -->
  <script type="text/javascript" language="JavaScript"><!-- <![CDATA[
  // ]]> --></script>
</head>
<body>
  <div id="container">

  </div>
</body>
</html>


This is the accomponying CSS:

body {
  font-family :Verdana, Arial, Sans;
  font-size   :76%;
  margin      :0px; }
div#container {
  font-size   :1.0em; }

.clearfix:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}

.clearfix {display: inline-table;}

/* Hides from IE-mac \*/
* html .clearfix {height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */

Generic XHTML template

I'm often having to reconstruct this, as I can no longer memorize everything (just blank HTML and BODY tags used to be acceptable ;-)) with the DOCTYPE and namespaces.. so to make it easier to grab in future:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title></title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
		<script type="text/javascript" src="common.js"></script>
	</head>

	<body>
		<div id="container">

			<div id="header">
			</div>

			<div id="wrapper">
				<div id="main">
					
				</div>
			</div>

			<div id="footer">
			</div>
			
		</div>
	</body>

</html>

Simple Perl Templating

Useful when you can't install modules because your host doesn't allow it:

sub parse_template() {
    my($file, %context) = @_;
    my($buffer) = "";
    open(FILE, $file) || die("Cannot open template " . $file);
    while ($line = <FILE>) {
        $line =~ s/\$(\w+)/$context{$1}/g;
        $buffer .= $line;
    }
    return $buffer;
}


To use it, put values in the context map:

%context = (
    "var1" => "value1",
    "var2" => "value2",
);
$out = &parse_template("template.txt", %context);
print $out;

Render a partial to an instance variable

Normally, if you call "render_partial" within a controller, nothing but the partial will be rendered.

Occasionally, it is useful to render a partial to an instance variable as a string so that the view can still be rendered as normal, and the string can be passed in to the view.

  add_variables_to_assigns
  @content_for_navbar = @template.render_partial 'layouts/public_navbar'

Convert string to anything

template <class T> T
fromString(const string &s) {
  T t;
  istringstream iss(s);
  iss >> t;
  return t;
}


You can then convert numbers in strings to other types like this:

double d = fromString<double>("10.2")

Skeleton c# class with test harness (

Using SnippetCompiler (http://www.sliver.com) a lot, I needed a template which has the nUnit components started... Here it is.

using System;
using System.Collections;
using NUnit.Framework;

#region entry point
public class EntryClass
{
	public static void Main()
	{
		NewClass t = new NewClass();
	}

}
#endregion entry point

// To test, select Build|Build To File... then point nunit's guii at the resultant dll


// Class we will build up for testing
public class NewClass
{
    // Sample Method which is tested
    public int MyMethod(int firstNumber, int secondNumber)
    {
        return firstNumber + secondNumber;
    }
}


// Class that does the nunit testing ([TestFixture] tells nunit it's the test class)
[TestFixture]
public class MyNewClassTestClass
{    
        // Method for testing the method in the class above ([Test] tells nunit this is a test method)
		[Test]
		public void MyMethodTest() 
		{
            // create an instance of the class
			NewClass obj_newClass = new NewClass();
            
            // Verify assertion
			Assertion.Assert(obj_newClass.MyMethod(10, 20) == 30);
		}    

} 

« Newer Snippets
Older Snippets »
Showing 11-16 of 16 total