tags:

views:

158

answers:

1

I am going over/studying the facebook code that was leaked in 2007,
I notice 1 function they call several times on on all the pages is this tpl_set(param1,param2) function

I am going to assume this is something to do with templates, The function has 2 parameters/variables passed into it, it looks like the first 1 is the name of the template and the second one determines if it is on or off maybe? That is just my guess as only part of the code was available. This is just for study purposes, Based on something like below, what kind of code could be wrote for that function to show a certain area of the page? I am thinking about doing some sort of template system for the learning experience which is why I do not want to use smarty.

Please give me your ideas on how to do this?

<php
tpl_set('home_announcement', $home_announcement_tpl);
tpl_set('hide_announcement_bit', $HIDE_ANNOUNCEMENT_BIT);
tpl_set('orientation_info', $orientation_info);
tpl_set('feed_stories', $feed_stories);

if ($show_friend_finder && (user_get_friend_count($user) > 20)) {
    tpl_set('friend_finder_hide_options', array('text' => 'close', 'onclick' => "return clearFriendFinder()"));
}

//end of page has this
render_template($_SERVER['PHP_ROOT'] . '/html/home.phpt');
?>
+2  A: 

Looking at the code snippet you paste, I would assume tpl_set sets specific variables in the template - such as setting the value of feed_stories template variable to content of $feed_stories. In the end, the render_template function chooses a template and renders it.

Since you say you want to do a template system yourself, here's a few pointers:

The most basic template can be a PHP file, such as this:

//mytemplate.php
<h1><?php echo $title; ?></h1>
<p><?php echo $content; ?></p>

Then, you could utilize this like so

$title = 'some title';
$content = 'Hello world';
require 'mytemplate.php';

This is the simplest way to have templates in PHP. You can optionally wrap the above process into a class or set of functions, so you get a bit cleaner interface.

An approach taken by various template engines, such as Smarty, is to have a custom syntax. As you may know, Smarty lets you use {$foo} to echo variables in templates. The way this works, is that you first provide Smarty with values for each variable in your template. When rendering the template, Smarty does a search and replace in your template, replacing Smarty-syntax with your values.

(It doesn't actually do that - it converts it first into PHP code and caches it to make it faster, but the basic idea is that.)

Hope this helps.

Jani Hartikainen
thanks, that does look more like what it is doing now that you spelled it out. And based on your example I could basicly wrap it into a function that looks like the facebook one and have it do what you described, thanks
jasondavis