<?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"
	>

<channel>
	<title>Tilted Symmetry &#187; Development</title>
	<atom:link href="http://tiltedsymmetry.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://tiltedsymmetry.com</link>
	<description></description>
	<pubDate>Thu, 09 Oct 2008 18:50:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>TVs &#038; Chunks = BFF </title>
		<link>http://tiltedsymmetry.com/2007/04/development/tvs-chunks-bff/</link>
		<comments>http://tiltedsymmetry.com/2007/04/development/tvs-chunks-bff/#comments</comments>
		<pubDate>Sun, 22 Apr 2007 03:02:36 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[MODx]]></category>

		<guid isPermaLink="false">http://tiltedsymmetry.com/?p=16</guid>
		<description><![CDATA[This article is about <a href="http://modxcms.com/chunks.html">chunks</a> and <a href="http://modxcms.com/template-variables.html">template variables</a> (TVs), and how they love each other very, very much.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been building all of my websites, both professional and personal, on <a href="http://www.modxcms.com">MODx</a> for over a year now (probably more than two years, if you count the months that I was using <a href="http://www.etomite.org">Etomite</a>, which is where MODx has its roots).  I&#8217;ve said it before: MODx is the best CMS I&#8217;ve worked with, ever, by a long shot.  I&#8217;ve found no limits when it comes to expandability; most things are easy to build, and pretty much everything you can imagine is doable, because building additional functionality using PHP is easy as pie.</p>

<p>Enough said; bragging about MODx is not the purpose of this article.  This article is intended for anyone who has done any website development on top of MODx (which is also a framework).  You don&#8217;t have to think of yourself as a professional developer or anything like that.  You just need to understand the terminology of MODx components, because I&#8217;m not going to explain them.  Refer to the <a href="http://modxcms.com/documentation.html">MODx documentation</a> for that.</p>

<p>This article is about <a href="http://modxcms.com/chunks.html">chunks</a> and <a href="http://modxcms.com/template-variables.html">template variables</a> (TVs), and how they love each other very, very much.</p>

<p>There are many different content types for TVs.  There are text content types, which are just text fields and textareas.  There are also content types like list boxes and check box arrays, which have multiple choices that need to be preset.  When you set up the choices for these types of TVs, the syntax is something like this:</p>

<blockquote>
  <p>Display Name 1==Value 1||Display Name 2==Value 2||Display Name 3==Value 3</p>
</blockquote>

<p>The value of a choice is very flexible; it can be a string or a number, or you can really take advantage of MODx&#8217;s facilities and use a chunk or even a snippet.  Remember that a snippet is a bit of PHP script that you can pass variables to, which means that you can create a lot of intelligence in your system by using them.  A chunk, on the other hand, is just a bit of HTML; you can&#8217;t pass variables to it, and it doesn&#8217;t process anything.  A chunk is a chunk, and that&#8217;s it.</p>

<h3>The Problem with TVs</h3>

<p>In my time of developing with MODx, I&#8217;ve been able to experience some of the long term effects of certain choices that I&#8217;ve made.  In the past, when I would set up multiple choice TVs, I would set numerous snippets as their values.  The snippets would usually do something simple, like load up previews of the latest 5 articles, or format and display the entire article of the current document, or create a menu.  Then on each page that used the templates that the particular TV was connected to, I could use those choices to tell my system what I wanted to display on that page.  I started running into problems a few months later, however, when I wanted to change the values of those choices, so that, for example, the latest 5 articles might be formatted in a slightly different way.  I expected that, if I changed the snippet in that choice for that TV, that every page that had that choice would consequently change the way that it formated the latest 5 articles.  What I discovered quickly, however, was that MODx caches the values of the choices that you make for each TV under each document, so if I change the value of a choice in a TV, all of the documents will still have the old value cached.  The new value will only be reflected on new documents; the old value will remain in the old documents unless I go back to each document, one-by-one, and deselect that choice, save the document, then reselect the choice and save again.  Then the cached value will be reset.</p>

<p>Here&#8217;s a quick example.  Say I have a TV called “SideBarContent“.  The TV is a list box, and the choices are as follows:</p>

<blockquote>
  <p>Article Menu==&#91;&#91;Wayfinder? &amp;outerTpl=&#96;menuOuter&#96; &amp;innerTpl=&#96;menuInner&#96; &amp;rowTpl=&#96;menuRow&#96; &amp;startId=&#96;6&#96;&#93;&#93;||Comic Previews=&#91;&#91;Ditto? &amp;tpl=&#96;ComicPreviewTemplate&#96; &amp;display=&#96;3&#96; &amp;startID=&#96;45&#96;&#93;&#93;</p>
</blockquote>

<p>This TV has two choices, and the values of each choice is a snippet call.  It will allow me to choose, on a per-page basis, what I want to show on that page, either the article menu or a list of comic strip previews.  Good enough.  But then a few months later, maybe I decide that I want to switch the template for the comic previews and change the number of previews that are displayed.  So I go back into the TV settings, and change it like this:</p>

<blockquote>
  <p>Article Menu==&#91;&#91;Wayfinder? &amp;outerTpl=&#96;menuOuter&#96; &amp;innerTpl=&#96;menuInner&#96; &amp;rowTpl=&#96;menuRow&#96; &amp;startId=&#96;6&#96;&#93;&#93;||Comic Previews=&#91;&#91;Ditto? &amp;tpl=&#96;NewComicPreviewTemplate&#96; &amp;display=&#96;5&#96; &amp;startID=&#96;45&#96;&#93;&#93;</p>
</blockquote>

<p>I do this expecting that all of the documents that have the comic preview choice selected, will use the new template and show 5 previews instead of 3.  What I will find instead, is that only documents that are created after this change will reflect the new values; all of the old documents will remain unchanged.  This is because when the choice was selected for this TV in the old documents, the system cached the old snippet call with all the old parameters.  This makes the system run a bit faster, but can cause problems when changes like this need to be made.</p>

<h3>So what makes chunks so special?</h3>

<p>So this is where chunks can become very handy.  I said before that chunks are just bits of HTML markup; that&#8217;s true, but there&#8217;s more to it.  I also said that they can&#8217;t process anything; that&#8217;s true too, at least not in and of themselves.  One thing about chunks that makes them extremely useful is that you can embed snippets in them.  When you do this, you give them all the power of that snippet.  And in the case of setting TV choice values, you prevent the system from caching the snippet call.</p>

<p>So now I can set the TV list box up like this:</p>

<blockquote>
  <p>Article Menu==&#123;&#123;ArticleMenu&#125;&#125;||Comic Previews==&#123;&#123;ComicPreviews&#125;&#125;</p>
</blockquote>

<p>In the ComicPreviews chunk, I can put the following snippet:</p>

<blockquote>
  <p>&#91;&#91;Ditto? &amp;tpl=&#96;ComicPreviewTemplate&#96; &amp;display=&#96;3&#96; &amp;startID=&#96;45&#96;&#93;&#93;</p>
</blockquote>

<p>Now when I select this choice in my documents, the system will cache the call to my chunk, which in turn calls whatever snippets are in that chunk.  If I want to change the parameters of my snippet later down the road, I just go to my chunk and change it&#8217;s contents to whatever I want.  This will produce the cascading effect we&#8217;re looking for; the changes will be reflected in all documents that have this choice selected, new and old.</p>
]]></content:encoded>
			<wfw:commentRss>http://tiltedsymmetry.com/2007/04/development/tvs-chunks-bff/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Object-Oriented, Like It or Not</title>
		<link>http://tiltedsymmetry.com/2006/07/development/object-oriented-like-it-or-not/</link>
		<comments>http://tiltedsymmetry.com/2006/07/development/object-oriented-like-it-or-not/#comments</comments>
		<pubDate>Sun, 30 Jul 2006 02:59:09 +0000</pubDate>
		<dc:creator>Joey</dc:creator>
		
		<category><![CDATA[Development]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://tiltedsymmetry.com/?p=15</guid>
		<description><![CDATA[In the years that I've spent learning PHP, one of the major challenges I faced was deciding whether I wanted to <em>continue</em> learning PHP.  I've heard a lot of criticism of the language in the professional developers circles, one of the primary criticisms being that it's not an object-oriented language.  To many PHP developers, this comment is like fingernails on a chalkboard.  The PHP development community is predominantly made up of procedural developers who could care less that PHP may or may not be strictly <acronym title="Object-Oriented">OO</acronym>, and they've made a lot of decent arguments for procedural development.  Understanding the two sides of this debate can be tricky because, as would be expected, procedural developers are obviously going to argue for procedural development, and object-oriented developers are usually going to argue for OO development.  However, among the arguments that I've heard that I consider to be the most objective, from either side, the verdict tends to be, that whichever method you choose should depend predominantly upon the project, and as a general rule, when dealing with larger development projects, the more that your methods are object-oriented, the better off you're going to be.]]></description>
			<content:encoded><![CDATA[<p>In a former life I was a teacher of Art and English.  It was a valuable experience that taught me a lot about the organization of thoughts and ideas.  Every teacher has a different approach.  Some teachers begin by entering their class and talking about what they know, while others first spend time preparing a syllabus, with outlines, main topics, and clear starting and stopping points.  Either method can be effective under certain conditions.  If you were to attend a seminar that lasted 3-4 hours, it&#8217;s very possible that you would benefit equally from either teaching method.  If, however, you were to attend a college course that had 30 sessions lasting 2-3 hours each, which teacher would you prefer to have?  The teacher with syllabus and clearly defined topics is likely going to be much easier to follow.  Additionally, since that teacher has broken down their discipline into logical building blocks, you are likely to come away from that course with a very structured understanding of the subject matter, which will give you the ability to go much further, and much faster.</p>

<p>The shoot-from-the-hip teacher would probably run out of things to say before the end of the first week.  Nevertheless, there are some who may have chosen this teacher, and I don&#8217;t blame you.  After all, who doesn&#8217;t love free time, am I right?  Seriously, sometimes a teacher who can just sit down and talk about a subject matter like they&#8217;re just having a cup of coffee with you, can be much easier to learn from.  I have had at least one teacher who taught casually and seemingly unprepared, but nevertheless effectively; that teacher was the best I&#8217;ve had, ever.  Nevertheless, not only are the good ones an exception, they are also an <em>illusion</em>; just because a teacher seems like they haven&#8217;t prepared a structured approach, doesn&#8217;t mean they haven&#8217;t.  I argue that any teacher who trains their students effectively, and equips them with the subject matter thoroughly, does so partly, even largely, through the process of organization and structure.  Sometimes the best organization and structure will sneak up on you like it was never there.</p>

<p>So anyway, long-winded speech to make a simple point.  Coding is a lot like teaching, you&#8217;re just not teaching a human.  You are, however, teaching a computer, which knows far less than a human, and assumes far less as a consequence, which is both good and bad.  You are also leaving a trail of information for other human teachers (coders) to pick up on later, and therein lies one of the primary reasons to make sure that you&#8217;re code is organized.</p>

<p>In the years that I&#8217;ve spent learning PHP, one of the major challenges I faced was deciding whether I wanted to <em>continue</em> learning PHP.  I&#8217;ve heard a lot of criticism of the language in the professional developers circles, one of the primary criticisms being that it&#8217;s not an object-oriented language.  To many PHP developers, this comment is like fingernails on a chalkboard.  The PHP development community is predominantly made up of procedural developers who could care less that PHP may or may not be strictly <acronym title="Object-Oriented">OO</acronym>, and they&#8217;ve made a lot of decent arguments for procedural development.  Understanding the two sides of this debate can be tricky because, as would be expected, procedural developers are obviously going to argue for procedural development, and object-oriented developers are usually going to argue for OO development.  However, among the arguments that I&#8217;ve heard that I consider to be the most objective, from either side, the verdict tends to be, that whichever method you choose should depend predominantly upon the project, and as a general rule, when dealing with larger development projects, the more that your methods are object-oriented, the better off you&#8217;re going to be.</p>

<p>When I first began understanding the concepts of OO methodology, I was just stepping into PHP5.  I felt that PHP5 was pretty object-oriented, and I didn&#8217;t really understand what some PHP critics were talking about when they said it wasn&#8217;t (yes, many were talking about PHP4, but some were, even then, directing their comments toward PHP5).  Since that time I&#8217;ve worked quite a bit with javascript, and having now made a decent comparison of the two languages, I have a much better understanding of what an OO language is all about, and PHP5 is not extremely object-oriented when compared to languages like javascript or ruby.  Nevertheless, I still believe you can build a strong OO application based on PHP if you know what you&#8217;re doing.</p>

<p>One of the primary end results of object-oriented development, in my opinion, is simply organization.  It just makes more sense, for example, to take all the code that pertains to the user and place it in a user <strong>object</strong>, rather than intermingle it with a bunch of other code.</p>

<p>There are many procedural developers who don&#8217;t use <acronym title="Classes are collections of variables and functions that can be used as objects during runtime.">classes</acronym> to organize their code into objects, but <em>do</em> use files to accomplish some similar organization.  For example, they may have a file called <code>user.php</code> where they place all of the code pertaining to the user.  This is a very positive step in the right direction.  However, the level of organization when using classes is still far better.  Files give you the advantage of code separation, but they are still missing the advantage of trails.  For instance, if I have a class called user, and somewhere in my code I have to refer to a user, I do so by creating a user object, like so:</p>

<pre><code>$user = new User;
</code></pre>

<p>Then, if I need to refer to the user&#8217;s name, I do that like so:</p>

<pre><code>$user-&gt;name = "Johnny";
</code></pre>

<p>If I want to refer to a group that a user is in, I could do that like this:</p>

<pre><code>if ($user-&gt;groups-&gt;member) echo "Welcome back, valued customer.";
</code></pre>

<p>And again, if I wanted to refer to the permissions of a group that the user is in, I might do that like this:</p>

<pre><code>if ($user-&gt;groups-&gt;member-&gt;allowPublish()) {
    echo "Publish documents.";
} else {
    echo "Sorry, but you are not allowed to publish documents.";
}
</code></pre>

<p>This makes your code very easy to follow.  Looking at the previous permissions check and reading from left to right, it&#8217;s fairly straight forward to understand what we&#8217;re doing, i.e. we&#8217;re checking to see if the <strong>user</strong> is in a <strong>group</strong> called <strong>member</strong> that is <strong>allowed to publish</strong>.  Even better, looking at the last member of that call, <code>allowPublish</code>, we can tell it&#8217;s a function because it&#8217;s followed by a set of parentheses, and now we have a good idea of where the function is located, i.e. it is a method of a class that handles groups, very likely a class called &#8220;member&#8221;; assuming that we&#8217;re using sensible variables, we&#8217;re now on a fast track to fast tracking.  If this were procedural programming, this function would most likely be sitting on a line by itself without any trail or clues for tracing, so the fact that it&#8217;s a function would be all that we would be able to deduce.</p>

<p>OO methodology is also designed to make your code much more reusable, because when executed properly it separates code according to its purpose.  For example, perhaps you are creating a social news site which will allow your visitors to create accounts and publish news.  For this project you will need to design various bits of functionality, such as:</p>

<ul>
<li>The code that allows users to register, sign in, and write news articles</li>
<li>The code that sorts the news articles by category and publication dates</li>
<li>The code that formats all this information for viewing on web pages</li>
</ul>

<p>Now imagine that months after this project is completed and live, you are given the task of designing a photo gallery.  This new project has very little in common with your prior project, namely user accounts, and perhaps some of the code that formats information for web pages.  Trying to reuse your prior project to suit the needs of the current project is out of the question.  If you want to reuse the user code and formatting code, you&#8217;ll have to pull it out of your prior project and begin building upon it afresh.  If you built the prior project procedurally, it is likely that sifting out the pertanent code snippets is going to be confusing, because chances are you have intermingled <em>user code</em> with <em>input code</em> with <em>publishing code</em> with <em>database code</em> with <em>formatting code</em>.  On the other hand, if you built your site using OO methodology, you&#8217;re almost certainly in much better shape.  Applying OO methods to your prior project, you will have built a class for each bit of functionality, i.e. a user class for everything the user needs to do, a formatting class for all of the formatting that needs to be done, perhaps a news class for creating and accessing news, etc.  To reuse the user and formatting functionality in your new project, all you need to do is cut and paste the user and formatting classes over to your new project.  You might also need to sift out the few areas in the classes that access other classes; but if you&#8217;ve done a really good job with these classes, chances are there are little, if any, areas that do that.  Reusable OO code done well means clip, paste, and go.  Reusable code reduces your long-term workload, which will either increase your profit margin or make you much more competitive.</p>

<p>OO methodology is also famous for <em>abstraction</em>, because it makes it very easy to design generic objects with generic properties that can take on many specific forms during runtime.  Anytime you define a variable in a class and outside of the functions in that class, that variable is understood by PHP as a <strong>property</strong> of that class.  Also, any functions that you define in that class are understood by PHP as <strong>methods</strong>, or actions that the class can perform.  These methods can perform their actions differently based upon how the properties of the class/object are defined.  When a class is instantiated as an object during runtime, you can define a method for the class to define its own properties during instantiation, or you can define the properties of the object after instantiation from outside the object under certain conditions.</p>

<p>I can speak from personal experience when I say that OO methods have made my job much easier and quicker.  In a future article, I&#8217;m going to take some time to explain my own approach to OO development in PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://tiltedsymmetry.com/2006/07/development/object-oriented-like-it-or-not/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
