<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Building Browsergames &#187; templates</title>
	<atom:link href="http://buildingbrowsergames.com/category/design/templates/feed/" rel="self" type="application/rss+xml" />
	<link>http://buildingbrowsergames.com</link>
	<description>Ever wanted to build a browsergame?</description>
	<lastBuildDate>Mon, 29 Mar 2010 14:00:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting Started With A Templating System (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/08/29/getting-started-with-a-templating-system-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/08/29/getting-started-with-a-templating-system-ruby-on-rails/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 14:00:42 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=277</guid>
		<description><![CDATA[Introduction
This entry is based on the Building Browsergames entry: Getting Started With A Templating System (PHP)
Since we&#8217;re using Ruby on Rails, we&#8217;re don&#8217;t have to select a templating system, there&#8217;s one already built into Rails itself. It offers many different features to make it easy to build web pages out of parts, cache parts of [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This entry is based on the Building Browsergames entry: <a href="http://buildingbrowsergames.com/2008/05/16/building-browsergames-setting-up-a-templating-system-php/">Getting Started With A Templating System (PHP)</a></p>
<p>Since we&#8217;re using Ruby on Rails, we&#8217;re don&#8217;t have to select a templating system, there&#8217;s one already built into Rails itself. It offers many different features to make it easy to build web pages out of parts, cache parts of pages or whole pages, etc. As with most (probably all) parts of Rails, alternatives are available to what&#8217;s built in but you&#8217;ll be satisfied with what&#8217;s already there for quite a while.</p>
<p>I particularly want to amplify something Luke said in the original posting. He thought a lot of developers were fans of developing their own templating systems. Wow. If that is in fact true, then you are seriously misguided if you go down that path. What&#8217;s built into Rails and the Smarty templating system Luke used for his PHP work is more than enough to get your pages done. <strong>Do not waste your time on side projects like building a templating system</strong>, use the time to improve your overall programming skills or build your game. If you do, you might be one of the few percent who actually finishes a project and others get to play it.</p>
<h2>Using Rails Views</h2>
<p>The design of Rails is already about separating the view which builds our HTML from the controller which connects the model to the view. So displaying the user&#8217;s name in the view is as simple as adding some code like the following to the already existing app/views/welcome/index.html.erb:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'users/user_bar'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= h <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
&lt;h1&gt;Welcome To The Game&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> logged_in? <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;Hello, <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#0066ff; font-weight:bold;">@current_user</span>.<span style="color:#9900CC;">login</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>!&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>It&#8217;s the same as it was before except that we&#8217;ve added the section at the end that checks to see if we&#8217;re logged in and greets us if we are. Now, as long as the controller method that is called for this page sets up a @current_user variable the view will pull the login out of it and display it within the page. The restful_authentication plugin provides us with an easy to use function to check if the user is logged in or not and put the user&#8217;s info into the @current_user variable if he/she is. We&#8217;ll call that from our controller (app/controllers/welcome_controller.rb) in the index function (remember, the flow of a user&#8217;s page request always goes to the controller first to set everything up and then to the view from there).</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> index
  current_user
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If you recall from before, the old index method didn&#8217;t do anything. Now it calls the function which will fill in the @current_user variable before the index.html.erb file gets used to generate the view. At this point if you login you should find that everything works nicely.</p>
<p>By this point we&#8217;ve seen that the templating code (in the .html.erb files) gives us the ability to include partials pages to reuse sections of HTML, pull data from variables, and even conditionally show or not show parts of the page.</p>
<h2>Extra Credit</h2>
<ol>
<li>To learn more about the functions that restful_authentication makes available to you, look in the file lib/authenticated_system.rb. There are several functions in there to help you determine whether or not the user is logged in, restrict pages from users who aren&#8217;t yet logged in, etc.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/08/29/getting-started-with-a-templating-system-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Diary of a Browsergame: Planning out the templates</title>
		<link>http://buildingbrowsergames.com/2008/07/23/diary-of-a-browsergame-planning-out-the-templates/</link>
		<comments>http://buildingbrowsergames.com/2008/07/23/diary-of-a-browsergame-planning-out-the-templates/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 14:00:13 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[diaryofabrowsergame]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[workingtitle]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=99</guid>
		<description><![CDATA[Now that I&#8217;ve set up my database, it&#8217;s time to settle on a templating system, and decide on how I want my templates to work.
Working Title only has a few pages that will need to be displayed &#8211; according to Design 1.0, there are a total of 7 &#8211; Home, Design Document, Register, Login, Logout, [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;ve <a href='http://buildingbrowsergames.com/2008/07/14/diary-of-a-browsergame-setting-up-the-database/'>set up my database</a>, it&#8217;s time to settle on a templating system, and decide on how I want my templates to work.</p>
<p>Working Title only has a few pages that will need to be displayed &#8211; according to <a href='http://buildingbrowsergames.com/workingtitle/design/1.0.html'>Design 1.0</a>, there are a total of 7 &#8211; Home, Design Document, Register, Login, Logout, Codex, and Play. This will make developing the templates very easy, as there are only a few that I need to build.</p>
<p>I&#8217;m going to use <a href='http://search.cpan.org/dist/HTML-Template/Template.pm'>HTML::Template</a> for my templating system, just like we have been in our <a href='http://buildingbrowsergames.com/category/buildingbrowsergames/perl/'>Perl-based browsergame tutorial</a>. However, my templating setup will be slightly different; I am planning on nesting two templates within each other. I&#8217;ll have a simple outer template which defines some of the global styles for all of the pages, and then another inner template that is populated with the information unique to the page(this is an example of applying DRY to my templates).</p>
<p>To that end, I&#8217;ve whipped up a quick outer template for Working Title, that will(for the moment) do what I need it to. There isn&#8217;t much to it, but you can take a look at it <a href='http://buildingbrowsergames.com/workingtitle/design/template-outer.html'>here</a>.</p>
<p>Is it a rough template? Yes. Will it change before Working title is done? Probably. But it&#8217;s enough to get me up and running, and free up my time to work on developing the rest of the game, instead of just working on the template.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/07/23/diary-of-a-browsergame-planning-out-the-templates/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating your games template</title>
		<link>http://buildingbrowsergames.com/2008/06/23/creating-your-games-template/</link>
		<comments>http://buildingbrowsergames.com/2008/06/23/creating-your-games-template/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 14:00:37 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[gettingstarted]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=70</guid>
		<description><![CDATA[I talk to a lot of people who are interested in building a browsergame, but never really know where to start. I tend to recommend that they draw up their plans for what their game will be, and then come back to me once they have that &#8211; from there, they&#8217;ll be able to figure [...]]]></description>
			<content:encoded><![CDATA[<p>I talk to a lot of people who are interested in building a browsergame, but never really know where to start. I tend to recommend that they draw up their plans for what their game will be, and then come back to me once they have that &#8211; from there, they&#8217;ll be able to figure out what to build.</p>
<p>But there&#8217;s a nebulous step in between designing your game and actually building it, that a lot of new developers tend to miss: <strong>your game&#8217;s template</strong>.</p>
<p>While we&#8217;ve been working through how to build a game, we&#8217;ve been creating some <strong>very basic</strong> HTML for our templates &#8211; but nothing fancy, and definitely not what you&#8217;d want to use for a game you planned on releasing to the public.</p>
<p>So what&#8217;s a developer to do? Most of the developers I know couldn&#8217;t design their way out of a wet paper bag &#8211; no matter how much they may try to delude themselves otherwise. If you&#8217;re a developer reading this and you actually <strong>can</strong> design, go celebrate &#8211; you are a true rarity among developers.</p>
<p>Thankfully, for those of us who can&#8217;t design, there are options out there. Here&#8217;s a couple of sites that will help you out with building your template:</p>
<ul>
<li>
<h2>Layout Gala</h2>
<p><a href='http://blog.html.it/layoutgala/'>Layout Gala</a> is one of my favorite websites for this sort of thing &#8211; 40 CSS based layouts written with valid CSS and HTML, and good cross-browser compatability. Whenever I&#8217;m building something, I tend to start with a LayoutGala layout and hack it to suit my needs. They&#8217;re all free to use, and as far as I can tell there&#8217;s no license placed on them stating you need to attribute their author &#8211; although I tend to leave a comment in my source code for anyone who&#8217;s really interested.</p>
</li>
<li>
<h2>Arcsin Templates</h2>
<p><a href='http://templates.arcsin.se/'>Arcsin Templates</a> has a lot of neat, free templates &#8211; both for regular websites <strong>and</strong> Wordpress. The only rule is that if you use their templates for free, you need to leave the link back to their website intact &#8211; but if you&#8217;re willing to spend $20 US, you can pay for the rights to remove the link. The benefit to using an Arcsin template over one from LayoutGala is that it&#8217;s already nicely styled and graphic&#8217;d &#8211; with a LayoutGala template you&#8217;ll need to re-style it yourself, whereas an Arcsin template is ready to go right out of the box.</p>
</li>
<li>
<h2>Elance</h2>
<p>Finally, if you&#8217;re willing to shell out a bit more cash for your design and get it done by a professional, you can use services like <a href='http://www.elance.com/'>Elance</a> &#8211; all you have to do is put up your project and a detailed description, and watch people bid on it &#8211; then you choose which bidder will do the project for you, and wait. While I haven&#8217;t personally tried Elance, I&#8217;ve heard a couple of raving reviews over it &#8211; much moreso than any other online outsourcing service.
	</li>
</ul>
<p>These are just a few of the options available to you when looking to build your site&#8217;s template &#8211; and everyone&#8217;s preferences are different. While I prefer Layout Gala templates and then modifying them to whatever I need them to be, you might prefer something pre-built like Arcsin&#8217;s templates, or even getting something build-to-order with Elance. They&#8217;re all valid approaches, and they&#8217;ll all help you hammer out that template before you start building anything.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/06/23/creating-your-games-template/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Getting started with a templating system (Perl)</title>
		<link>http://buildingbrowsergames.com/2008/05/18/building-browsergames-setting-up-a-templating-system-perl/</link>
		<comments>http://buildingbrowsergames.com/2008/05/18/building-browsergames-setting-up-a-templating-system-perl/#comments</comments>
		<pubDate>Mon, 19 May 2008 03:00:47 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[setup]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=36</guid>
		<description><![CDATA[So far we&#8217;ve built a registration page and a login page for our game, but not much else. That&#8217;s because we&#8217;re hitting a bit of an important milestone for our game: what templating system will we use?
It&#8217;s important to choose a templating system before you start building any functionality more complex than your registration and [...]]]></description>
			<content:encoded><![CDATA[<p>So far we&#8217;ve built a <a href='http://buildingbrowsergames.com/2008/04/18/building-browsergames-the-registration-page-perl/'>registration page</a> and a <a href='http://buildingbrowsergames.com/2008/04/25/building-browsergames-the-login-page-perl/'>login page</a> for our game, but not much else. That&#8217;s because we&#8217;re hitting a bit of an important milestone for our game: what templating system will we use?</p>
<p>It&#8217;s important to choose a templating system before you start building any functionality more complex than your registration and login system &#8211; it will help you speed up your development time, and keep your code and your design separate. If you&#8217;re like me, that means that you can safely pass your template off to someone who actually <strong>can</strong> design, and get them to work their magic on your template &#8211; without having to worry about your code breaking.</p>
<p>To that end, what Perl templating systems are there out there?</p>
<p>There are a lot of Perl templating systems available. There&#8217;s <a href='http://search.cpan.org/~abw/Template-Toolkit-2.19/lib/Template/Toolkit.pod'>Template::Toolkit</a>, and <a href='http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm'>HTML::Template</a>, and scores of others. For the sake of this quick guide, I&#8217;ve chosen to use HTML::Template &#8211; although really, you can use whatever templating system you want to(although my examples will all use HTML::Template).</p>
<p>To begin with, we need to install HTML::Template. There are a number of ways to go about this, and if you&#8217;ve never installed a Perl module before I recommend that you read <a href='http://www.cpan.org/modules/INSTALL.html'>this guide to installing CPAN modules</a>, and then install HTML::Template on your system. Alternately, a lot of systems with Perl on them come with the cpan shell &#8211; at which point all you need to do to install the module is run this command:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="shell" style="font-family:monospace;">cpan install HTML::Template</pre></td></tr></table></div>

<p>At this point, HTML::Template is installed &#8211; so let&#8217;s build a quick tester script to take advantage of that. First off, we&#8217;ll set up the template for the index page that <a href='http://buildingbrowsergames.com/2008/04/25/building-browsergames-the-login-page-perl/'>our login page from earlier</a> sends non-administrator users to:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;My Index Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p&gt;Hello, &lt;!--tmpl_var name='username'--&gt;!&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>It looks like a regular HTML file, doesn&#8217;t it? And that&#8217;s the great thing about templating systems. With a templating system, <strong>you can write regular HTML and just intersperse markers for whatever content you&#8217;re going to dynamically add</strong>, instead of hard-coding your content into your template.</p>
<p>We&#8217;ll call that template file <strong>index.tmpl</strong> &#8211; our index page will load it in when it outputs the user&#8217;s information. Really, you can use whatever extension you like &#8211; I just like .tmpl.</p>
<p>The next thing we need to do is write the code for the page that actually loads in and outputs the template. Here&#8217;s what that looks like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> CGI <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>cgi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CGI<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'username+password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\+/</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$cookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$template</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
		filename	<span style="color: #339933;">=&gt;</span>	<span style="color: #ff0000;">'index.tmpl'</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span>username	<span style="color: #339933;">=&gt;</span>	<span style="color: #0000ff;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #0000ff;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">header</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>All we do in this code is load in the value of the cookie that we set in our login page &#8211; named &#8216;username+password&#8217;. Then, we split it based on &#8216;+&#8217;, to return our username and password &#8211; because we only need the username, we discard the password:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>10
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\+/</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$cookie</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This obviously shows us that there&#8217;s a bit of a problem with our cookie &#8211; if a user signs up with a &#8216;+&#8217; in their username, our split will no longer work! One good way to work around this is to use something a little more complex to divide our username and password, or modify our code slightly to use a regular expression capture instead of split:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>10
11
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$cookie</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">/(.+)\+/</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$1</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This way, we&#8217;ll retrieve all information that comes before the last &#8216;+&#8217; sign &#8211; because <a href='http://perldoc.perl.org/5.8.8/functions/crypt.html'>crypt</a> returns characters in the set [./0-9A-Za-z], we don&#8217;t need to worry about a &#8216;+&#8217; sign cropping up and wrecking our regular expression.</p>
<p>Once that&#8217;s done, all we do is create a new template object, tell it where to load our template from, and then populate the paramater &#8216;username&#8217; with the value we pulled out of our cookie. And then we&#8217;re finished!</p>
<p>As you can see, working with a templating system isn&#8217;t that hard &#8211; and the gains you make by being able to instantly use the same template accross many pages are huge. There&#8217;s really no reason <strong>not</strong> to be using some sort of templating system &#8211; whether you roll your own, or use a pre-existing system.</p>
<p>If you want to see the example code in action, <a href='http://buildingbrowsergames.com/sandbox/register.cgi'>register</a>, and then <a href='http://buildingbrowsergames.com/sandbox/login.cgi'>login</a>, and you&#8217;ll be redirected to the index page &#8211; which will display the value out of our cookie.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/05/18/building-browsergames-setting-up-a-templating-system-perl/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Building Browsergames: Getting started with a templating system (PHP)</title>
		<link>http://buildingbrowsergames.com/2008/05/16/building-browsergames-setting-up-a-templating-system-php/</link>
		<comments>http://buildingbrowsergames.com/2008/05/16/building-browsergames-setting-up-a-templating-system-php/#comments</comments>
		<pubDate>Fri, 16 May 2008 14:00:16 +0000</pubDate>
		<dc:creator>Luke</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[setup]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=35</guid>
		<description><![CDATA[When you start building significant pieces of your game beyond the basic login and registration systems, you start to realize that you&#8217;re copying and pasting the basic layout of your pages a lot. Wouldn&#8217;t it be nice to have a template that you can drop all of the unique content into, and re-use on every [...]]]></description>
			<content:encoded><![CDATA[<p>When you start building significant pieces of your game beyond the basic login and registration systems, you start to realize that you&#8217;re copying and pasting the basic layout of your pages a lot. Wouldn&#8217;t it be nice to have a template that you can drop all of the unique content into, and re-use on every page?</p>
<p>The answer is that you can &#8211; and should. It will help you separate your design from your code, which(if you&#8217;re anything like me) means that you can have someone with actual designing skills come through and fix up your template before you push your site out to the rest of the world.</p>
<p>This will be a brief rundown on how to get up and running with a templating engine, which is what we will be using to build all of the other parts of our browsergame in progress. For a PHP templating engine, I&#8217;ve chosen Smarty. I did this by taking a look at <a href='http://www.smarty.net/rightforme.php'>Smarty&#8217;s list of features</a>, and deciding whether I liked them or not &#8211; when you choose a templating engine, that&#8217;s really all the forethought you need to put into it &#8211; will it work for you, and are you okay with working with it? As an aside, if you don&#8217;t like your templating system you can always switch systems for your next project.</p>
<p>A lot of developers are fans of &#8220;rolling their own&#8221; templating system. While this can definitely work, you&#8217;re probably going to make larger gains by simply using an existing one &#8211; if you need anyone to help you with your game later(or it really takes off and you hire someone), it will be a benefit to be using a system that they might already know as opposed to a proprietary system. Also, why do all that work when it&#8217;s already been done for you?</p>
<p>The Smarty website has a <a href='http://www.smarty.net/quick_start.php'>smarty quick start tutorial</a>, which you can use to get yourself up and running with Smarty.</p>
<p>Once you&#8217;ve gotten Smarty set up on your system, we&#8217;ll be building the index page that <a href='http://buildingbrowsergames.com/2008/04/24/building-browsergames-the-login-page-php/'>our login page from earlier</a> redirects to for non-administrator users. To start off with, we&#8217;ll just use the testing <strong>index.tpl</strong> file from the Smarty quick start:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;My Index Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;p&gt;Hello, {$name}!&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Next, we&#8217;ll use the <strong>index.php</strong> file from the Smarty quick start, and modify it so that it displays the currently authenticated user&#8217;s username:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// put full path to Smarty.class.php</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/usr/local/lib/php/Smarty/Smarty.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Smarty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">template_dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/web/www.domain.com/smarty/templates'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">compile_dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/web/www.domain.com/smarty/templates_c'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache_dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/web/www.domain.com/smarty/cache'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">config_dir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/web/www.domain.com/smarty/configs'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index.tpl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As you can see, there isn&#8217;t that much of a change made &#8211; we add a call to <a href='http://ca3.php.net/manual/en/function.session-start.php'>session_start()</a> to load up our previously stored session values, and then we use one of them to display the user&#8217;s username. If you visit your testing page, you should see the username that you logged in with &#8211; which means that Smarty is working! With Smarty we&#8217;ll be able to make sure that our code and designs no longer have to be intermingled &#8211; meaning that there&#8217;s less chance of accidentally screwing up one while working on the other.</p>
<p>If you want to see it in action, you can first <a href='http://buildingbrowsergames.com/sandbox/register.php'>register</a>, then <a href='http://buildingbrowsergames.com/sandbox/login.php'>login</a> to get redirected to the index page &#8211; which will show you the username you&#8217;re currently logged in with.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/05/16/building-browsergames-setting-up-a-templating-system-php/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

