Jump to content

Templating System


chokk

Recommended Posts

Hey all,I'm wanting to look into creating a template system. I've done a few searches on google, but none of them really go into details about creating a template system.Can you help me get started?

Link to comment
Share on other sites

If by this you mean your own templates (not ready-mades) then you should explore all the things that can be done with the include statement.The examples are not to good about this, but a common use of include is to embed long/short snippets of HTML into a longer HTML document. It functions almost like a paste. Whatever is in the included document gets pasted exactly where the include statement is.

Link to comment
Share on other sites

If by this you mean your own templates (not ready-mades) then you should explore all the things that can be done with the include statement.The examples are not to good about this, but a common use of include is to embed long/short snippets of HTML into a longer HTML document. It functions almost like a paste. Whatever is in the included document gets pasted exactly where the include statement is.
I would advise against this. It's a bad coding habit to include files with code that actually outputs something.
Link to comment
Share on other sites

I would advise against this. It's a bad coding habit to include files with code that actually outputs something.
It depends - for example, Wordpresse's templating system makes heavy use of inbuilt functions within individual templates. For most applications, though, it is possible to avoid live code within template files through systems like keyword replacement (e.g. Smarty) or DOM manipulation like the TAL system uses. Then you can read the files using filesystem functions like file_get_contents().
Link to comment
Share on other sites

It depends - for example, Wordpresse's templating system makes heavy use of inbuilt functions within individual templates. For most applications, though, it is possible to avoid live code within template files through systems like keyword replacement (e.g. Smarty) or DOM manipulation like the TAL system uses. Then you can read the files using filesystem functions like file_get_contents().
Right, and that's exactly what I would aim for in making a template system. include, however, is not the way to go.
Link to comment
Share on other sites

This is news to me. Care to elaborate?
Why not to use include in this way?It's messy coding. It makes your code less portable. If your project is open source or someone else ever plans on using your code, they're going to have to sort several included files to get a sense of what variables are declared in the main file's scope.It's also not the intended use of "include". include is there to include files that contain functions or classes - not anything that would define variables or actually output something. Not to mention it also makes your site subject to abuse. If somebody accesses the included file directly, presumably it won't work without the mess of variables contained in the main file's scope and throw several errors.
Link to comment
Share on other sites

You're thinking about include() in a very, uh, traditional way :) - I don't think most programmers use it like that. For example, look at Wordpress! Big, open source project - all the template files are parsed using include(), and all of them are designed to call upon WP functions defined outside the template files.Anyway, the main problem I see with including template files directly is the potential for abusive code to be written into the files by the template developer, and then executed when the script is included. Only applies if you are allowing other people to write templates, though.

Link to comment
Share on other sites

Hmm. That's a lot of assumptions. OP did mention a system, so many of these concerns might apply. Without more info, I was imagining something smaller. I was also assuming the included files would be firewalled behind an .htaccess directive, or stored below root level. That's routine for me.I think I was also imagining the included files would contain zero or next to zero actual code.Abusive template designers . . . caveat emptor? Anyway, OP is the designer; he's not shopping for a designer.I think we're discussing very different kinds of projects, and it could be my assumptions are out of line. Maybe we should find out what OP is really after before saying yea or nay to specific technologies?

Link to comment
Share on other sites

Hmm. That's a lot of assumptions. OP did mention a system, so many of these concerns might apply. Without more info, I was imagining something smaller. I was also assuming the included files would be firewalled behind an .htaccess directive, or stored below root level. That's routine for me.I think I was also imagining the included files would contain zero or next to zero actual code.Abusive template designers . . . caveat emptor? Anyway, OP is the designer; he's not shopping for a designer.I think we're discussing very different kinds of projects, and it could be my assumptions are out of line. Maybe we should find out what OP is really after before saying yea or nay to specific technologies?
Sorry if I misunderstood you, I thought you were suggesting something along the lines of:
<?php   include('header.php');   //whatever here   include('footer.php');?>

Link to comment
Share on other sites

What I had in mind was a system with templates written in html/css in which there are placeholders.PHP would grab a template, insert data into the placeholders and save the template + data to a new file.Example...1. A user inputs some text through a form..2. PHP grabs the default template from a template folder..3. PHP inserts the users input into the placeholders in the template..4. The template is then saved to a default directory as a static webpage.Do you know what I mean?

Link to comment
Share on other sites

Why do you want to do that, instead of having the page dynamically generated every time?

Link to comment
Share on other sites

Why do you want to do that, instead of having the page dynamically generated every time?
Because I don't know how to do that and I'm sure it has some advantages. I would imagine on a big site, it reduces the database stress level.Or am I completely mistaken; does that defeat the purpose of PHP?
Link to comment
Share on other sites

Batch processing can be advantageous in ... certain limited situations, but the vast majority of the time it is much more convenient to dynamically generate pages. For example, what happens if your markup changes later? Then all the pages need to be regenerated. Furthermore, databases are highly optimised and the effect of constant querying is negligible. For larger sites, it is still much, much more common to employ a, uh, post-preprocessing caching system than to batch-process all their pages. What you suggest is a very drastic measure and is hardly ever (if ever at all) implemented in real life. Trust me :).Err, how to dynamically generate pages? You just need to retrieve the data, for example from a database using mysql_query(), and echo it out. Have you read the PHP tutorial yet?

Link to comment
Share on other sites

Batch processing can be advantageous in ... certain limited situations, but the vast majority of the time it is much more convenient to dynamically generate pages. For example, what happens if your markup changes later? Then all the pages need to be regenerated. Furthermore, databases are highly optimised and the effect of constant querying is negligible. For larger sites, it is still much, much more common to employ a, uh, post-preprocessing caching system than to batch-process all their pages. What you suggest is a very drastic measure and is hardly ever (if ever at all) implemented in real life. Trust me :).Err, how to dynamically generate pages? You just need to retrieve the data, for example from a database using mysql_query(), and echo it out. Have you read the PHP tutorial yet?
Yeah, I know how to dynamically generate pages. I've built my own _simple_ cms. Maybe I've misunderstood the purpose of templates.If all your data(text, images, whatever) loads dynamically in one single page (index.php?id=something), then what IS the purpose of templates?
Link to comment
Share on other sites

You don't have to stick to a single page (i.e. contiguous non-modular piece of code) when generating markup dynamically... you can load and parse the template within that page, you just don't have to save the result to disk.The purpose of a template is to help separate your processing logic and the presentational part of your site, and to allow for the easy manipulation of that presentation. For example, templates in the context of CMSs allow for the modification of the site's appearance without the need to change the actual code.

Link to comment
Share on other sites

You don't have to stick to a single page (i.e. contiguous non-modular piece of code) when generating markup dynamically... you can load and parse the template within that page, you just don't have to save the result to disk.The purpose of a template is to help separate your processing logic and the presentational part of your site, and to allow for the easy manipulation of that presentation. For example, templates in the context of CMSs allow for the modification of the site's appearance without the need to change the actual code.
And how would I accomplish that?I need some keywords to search for, so I can find some good articles :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...