Log in

View Full Version : Trying to find a good tutorial - inserting {tags}



ScottDB
06-07-2011, 05:50 PM
Hi, Ive been doing google searches for this for hours but no luck. Was wanting to learn how to insert into templates information by using the tags such as {header}, {sidebar}, stuff like that where {sidebar} would bring up some info from a php page. Not sure what this process is even called for me to corectly search for it. Any help would be appreciated.

djr33
06-07-2011, 07:48 PM
What kind of system are you using? As far as I know, there isn't any standard name for that, and it's not actually part of PHP. You can use it in PHP, but it's not a default. You'll need to use str_replace() or regular expressions to find and fix the content.

You could get some insight looking at a bbcode tutorial for PHP-- replacing user-input with real code.

You might find some more specific things for "inserting variables into templates" or replacing 'variables' with 'constants'.



While writing this, I wanted to check that constants don't work in {} within double quoted strings. Some things, like variables, will be output within {} inside strings defined in double quotes like ".

But this code shows that constants don't work like that:

<?php
define('test','value');
echo test;
//result 'value'
echo "{test}";
//result '{test}'
?>
So you will need to use some sort of replacement function.

djr33
06-07-2011, 07:48 PM
What kind of system are you using? As far as I know, there isn't any standard name for that, and it's not actually part of PHP. You can use it in PHP, but it's not a default. You'll need to use str_replace() or regular expressions to find and fix the content.

You could get some insight looking at a bbcode tutorial for PHP-- replacing user-input with real code.

You might find some more specific things for "inserting variables into templates" or replacing 'variables' with 'constants'.



While writing this, I wanted to check that constants don't work in {} within double quoted strings. Some things, like variables, will be output within {} inside strings defined in double quotes like ".

But this code shows that constants don't work like that:

<?php
define('test','value');
echo test;
//result 'value'
echo "{test}";
//result '{test}'
?>
So you will need to use some sort of replacement function.




By the way, any good (detailed) tutorials about PHP templating should have information about this sort of thing eventually. You can also use other methods instead if you prefer.

traq
06-07-2011, 07:51 PM
I've seen some CMSs that use this type of markup, though I can't recall where. It was some time ago; most are moving away from the concept.

What's going on is:

1) the CMS (using PHP) parses the entire document, looking for these "tags."
2) if it finds one, it runs the associated function and replaces the tag with the function return value.

This makes for a long, inefficient process. newer CMSs store the information about "what should be inserted where" in the database, and insert it directly in the first place. Alternatively (in many cases - not all), you could simply break into PHP and call the function directly:
<?php print some_function(); ?>

Are you building something new, or trying to modify an existing system?



posted at same time as Djr.

BTW Daniel:



$S = 'strval';
define('test','this is a test');

echo "{$S(test)}";

ScottDB
06-07-2011, 11:39 PM
Thanks for the responses. I think I might have worded it wrong. What I am trying to do is to alter an exsisting script. It is a real basic php login script with admin features.

What I would like to do is to create a template like I have seen on a couple of different sites that have an .tpl extension for the page. The page is made up with html mark up with div, loops, tables, and such. Inside the tables are just links to information such as {body}, {memb_info}, {sidebar}, stuff like that.

djr33
06-08-2011, 02:28 AM
Without knowing more specific information about this particular script, we can't help. As we've explained, it is some sort of search and replace operation, so that probably is how the existing script works.

Do you have a link to the script? Maybe you could ask the creators.

I've never heard of the .tpl extension. It might be a format created (actually, it's probably just text, like in a .txt) specifically for this templating project.

ScottDB
06-08-2011, 03:03 AM
The script I am using is
http://evolt.org/PHP-Login-System-with-Admin-Features?from=0&comments_per_page=50

One of the places I have seen this in use is on the ventrino traffic exchange script. It has to be one of the most efficient ways of making a template and calling up funtions that I have seen. By doing it the way they did it made it so simple to edit just about any part of the script from the admin section without haveing to ftp into the site and overwrite, or upload files. Site is here - http://www.ventrino.com/

Attached is the .tpl file from the ventrino script that I would like to learn how to make. I put it in a zip file since the upload doesn't support .tpl extensions.

traq
06-08-2011, 05:32 AM
... does the evolt script use this templating system? (it doesn't look like it.)

the " .tpl " file you attached is just a plain text file (i.e., typically having a .txt extension) that was named with a different extension. It has absolutely no PHP code in it. It would work as I described above: php parses the file, looking for "keywords" (e.g., {JavaScript} ), and then inserts new content in their place, depending on what the keyword is.

Other than that, this process has nothing to do with PHP: it could be done just the same in most server-side languages, but it is not a "standard" feature of any of them. You would need to write the processing logic yourself.

integrating it with your scripts from evolt would be even more complex, since they weren't designed with anything like this in mind. I would recommend breaking into php at the appropriate points on your page and simply using the include() function.

ScottDB
06-08-2011, 07:47 AM
Thanks so much for answering my questions. I did find what I was looking for and your right, it doesn't have anything to do with php. It is called Smarty and is a template enging for php language. I'm sure you have probably heard about it. If not here is a link to what I am talking about. http://www.smarty.net/about_smarty

For now though I think I will stick with php and using the include() function like you sugested. I will study Smarty and maybe use it later. I would appreciate an unbiased view of using the smarty template system though if you know about it.

traq
06-08-2011, 01:22 PM
like I said, any parsing-based system is going to be less efficient than organizing things to be done the way you want in the first place. Instead of using keywords, this approach might be better:
$HEADER = func_builds_header();
$MENU = func_builds_menu();
$ETC = and_so_forth();

$template = '
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Page template</title></head>
<body>
<div id="header">'. $HEADER .'</div>
<div id="formatting_wrapper">
<div id="menu">'. $MENU .'</div>
<div id="etc">'. $ETC .'</div>
</div>
</body>
';

print $template;

you could also simply include() the template file after all your content has been generated.