SitePoint Sponsor

User Tag List

Results 1 to 24 of 24

Thread: Template System

Hybrid View

  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Template System

    Hello,
    Someone a while back suggested I used a template engine for my site. Now I havn't got a clue what one is, what it does, how i can use it, and why i need to use it. Please can you explain about them and answer those queries I have. Also what one should i use and how should I start to use it. Someone suggested XiTemplate or Smarty but i really do not have a clue. I am also not that experienced in PHP.

    Regards,
    Neil

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,001
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Smarty documentation is quite friendly in explaining how smarty works and basically covers your questions: http://smarty.php.net/manual/en/what.is.smarty.php
    -- Jelena --

  3. #3
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right i understand what it is and what it does but i don't understand the coding and how could i convert convert my current site to use it?

    Regards,
    Neil

  4. #4
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Here is a nice little thread found on the PHP.net mailing list:

    [PHP] templating

    Maybe that will give you some good ideas... actually, I highly recommend signing-up for the PHP.net general mailing list, it is very informative and the folks on the list are very helpful. Your question would probably get a lot of great responses.

    PHP.NET Mailing Lists

    Hth,
    Cheers,
    Micky

  5. #5
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really do not see how this could benefit my website. Also if i give you my site address would you give me some ideas on how I could convert the main page to use Smarty?

    Regards,
    Neil

  6. #6
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I personally have not used smarty. I have read good/bad things about it. Typically, my sites are so small that I prefer not to use a bloated templating system... a simple setup typically meets my needs.

    I usually build my own PHP templates from scratch, or use something like Textpattern (CMS/BLOG software).

    Hmmm, let me see if I can explain how I typically set things up:


    For content and/or code that will be the same on all pages, I set those up as php includes on my main php template... for example:

    Header
    Footer
    Navigation
    Sidebar

    Then I setup a variable, say "$page" and all my content is then pulled-in to the page depending on the query string. Example:

    index.html?page=home

    Lets say that is the href for my main navigation "home" link. When clicked, index.html (the template) loads and the home page content will display because I told my PHP script to grab content from a DB or a flat html file, or even a text file.

    Basically I tell my script:
    PHP Code:
    if($page == $_GET['home']) { readfile($_SERVER['DOCUMENT_ROOT'].'/gutz/home.html'); } 
    Keep in mind, I am leaving-out a bunch of detail... esp. security features (very important.)

    Any of this piquing your interest?

  7. #7
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats slightly how my site is done at the moment but i have tonnes of php files with the same template and then the content on those. the header, footer and navigation are brought in via includes. So are you saying i should start again with 1 page as a template and use that variable system? Also the site is www.atmania.com

    Could i make page as a template and then make another page with basically all the coding from the others brought in with if $page is this or whatever?

    Cheers

    Neil

  8. #8
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi rctneil,

    First, let me start-out by saying: I am not a PHP guru.

    Quote Originally Posted by rctneil
    Thats slightly how my site is done at the moment but i have tonnes of php files with the same template and then the content on those. the header, footer and navigation are brought in via includes.
    Sounds good to me.

    Quote Originally Posted by rctneil
    So are you saying i should start again with 1 page as a template and use that variable system? Also the site is www.atmania.coml
    Cool site.

    I like to minimize the amount of pages I have to update, so for me a one page PHP template is the way to go. It can be harder to setup (from scratch), but in the end much easier to maintain.

    Quote Originally Posted by rctneil
    Could i make page as a template and then make another page with basically all the coding from the others brought in with if $page is this or whatever?
    It sounds like you are thinking in the right direction. Basically, for me, I try to put all the PHP in the main template (includes, conditionals, security...) and then only pull-in raw html files or php files (depending on content.)

    As far as your site goes...

    Looks like you got a lot of content to deal with. Also, looks like you are already using page vars to load content... If you are thinking about doing things how I suggested you would have to make sure all of you links were updated... for example:

    http://www.atmania.com/parkguide.php

    I click a photo and go here:

    http://www.atmania.com/area.php?areaid=1

    Now, if you go with the system I mention, your urls would have to look something like this (with domain name - sorry, had to shorten url):

    http://www..com/index.php?page=parkguide

    and

    http://www..com/index.php?page=parkguide&areaid=1

    Also, your image gallery could make things tricky... sorry, I can't help too much without seeing how the backend is setup.

    Well, let me move on from my way of doing things, to another way... here is a script from 2004 (old, but might give you an idea or two, and maybe others could post something more contemporary):

    index.php
    PHP Code:
    <?php
    /* index.php */
    include('classes/AwesomeTemplateEngine.class.php');

    $aT=new AwesomeTemplateEngine('./templates/');

    /* Some sample data */
    $data['title']=" AwesomeTemplateEngine Demo ";
    $data['message']="You're looking at a revolution in templating.";
    $data['table'][1]['item']="include()";
    $data['table'][1]['url']="http://www.php.net/include";
    $data['table'][2]['item']="class{ }";
    $data['table'][2]['url']="http://www.php.net/manual/en/language.oop.php#keyword.class";
    $data['poweredby']="Powered by AwesomeTemplateEngine";

    /* Show the template */
    $aT->parseTemplate($data,"example_template.php");
    ?>
    classes/AwesomeTemplateEngine.class.php
    PHP Code:
    <?php 
    /* AwesomeTemplateEngine.class.php */ 
    class AwesomeTemplateEngine 
        var 
    $templatePath
        function 
    AwesomeTemplateEngine($templatePath) { 
            
    $this->templatePath=$templatePath
        } 
        function 
    parseTemplate($data,$template) { 
            include(
    $this->templatePath.$template); 
        } 


    /* Awesome isn't it? */
    ?>
    templates/example_template.php
    PHP Code:
    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> <?php echo ($data['title']); ?> </title>
    </head>

    <body>

    <h2 align="center"><?php echo ($data['message']); ?></h2>

    <table align="center">
    <tr>
    <td colspan="2">PHP elements used by AwesomeTemplateEngine:</td>
    </tr>

    <?php
    foreach ($data['table'] as $row) {
    ?>

    <tr>
    <td><?php echo ($row['item']); ?></td>
    <td><a href="<?php echo ($row['url']); ?>">Read More</a></td>
    </tr>

    <?php
    }
    ?>

    </table>

    <p align="center"><?php echo ($data['poweredby']); ?>

    <p align="center">The AwesomeTemplateEngine was inspired by 
    <a href="http://www.sitepointforums.com/showthread.php?s=&threadid=67849">this discussion at Sitepointforums</a>.
    Pay close attention to comments by <b>voostind</b>
    <p align="center">Flames to the AwesomeTemplateEngine author <a href="http://forum.pinkgoblin.com/viewtopic.php?p=325#325">here</a>
    </body>
    </html>
    Does that help to see some code?

  9. #9
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any thoughts on this.

    Also if using the method above you are still not keeping the template and content separate. Please try and give examples linked in with my site - www.atmania.com

    Regards,
    Neil

  10. #10
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm lost.

    Regards,
    Neil

  11. #11
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, sorry... I am not the best teacher.

    I think you are better off setting-up a CMS... looks like you are on top of that via another thread. I prefer Texpattern... Joomla might be worth checking-out.

    Good luck,
    Cheers,
    Micky

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Smarty is the best yet supports many advanced stuff but very easy to use

  13. #13
    SitePoint Enthusiast Everah's Avatar
    Join Date
    Mar 2004
    Location
    East Bay Area, CA, USA
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TemplateLite is a fork of Smarty that uses a lot less overhead and parses faster than Smarty. It uses the exact same syntax (with the exception of reserved 'smarty.' type notations and is super easy to implement. I'd highly recommend TemplateLite if you are looking for a template parser that works like Smarty but is not as heavy.
    Me | My Family | My Design Firm
    Much of what I say can be refuted or disputed.
    I am just a lowly professional PHP developer.
    I could be just as wrong as the next guy.

  14. #14
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So can i use one of these cms systems in stead of a system like smarty or something? I did wonder yesterday whether to just make a template php page which has the layout, and all the normal "includes" like header, footer, navigation and then a white space where content will go and then when i link to another page use a variable like said above such as $page. The page variable will "include" the content requested which will be stored in small php files. So basically mini websites which don't look good . and when they are shown in the template you get the full pages. I don't know if this is the best way to do it. I am getting too confused with a template engine and i cannot get to grips with the coding and i'm not sure what words like parse or other things mean. Would this be a better way. I do have a mini CMS system on the site which i made myself but it will not update the whole of the site just certain areas like adding extra rides into the rides database or things like that.

    What do you all think?

    Regards,
    Neil

  15. #15
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rctneil
    So can i use one of these cms systems in stead of a system like smarty or something?
    Yes, but it depends on how much work you want to do. It kinda sounds like you want to take the easiest route possible. A cms would be a good thing, but it would probably require a major re-structuring of your site if you want it done right.

    Personally, I find it more work to try an retrofit a website with new "stuff"... much easier to build from scratch... allows for a more clean website in the end. But that is just me... and sometimes one does not have that choice, which may be your case.

    Quote Originally Posted by rctneil
    I did wonder yesterday whether to just make a template php page which has the layout, and all the normal "includes" like header, footer, navigation and then a white space where content will go and then when i link to another page use a variable like said above such as $page. The page variable will "include" the content requested which will be stored in small php files. So basically mini websites which don't look good . and when they are shown in the template you get the full pages.
    You are definitely on the right track...

    Hmm, maybe read this:
    Switching content with PHP variables

    The above article is very basic (found it via a quick google search)... security is very important, and I do not see ALA covering much of that... clamcrusher brought-up one of the important things to consider, but you will probably have to start a new thread cause that would get a bit OT here.

    Quote Originally Posted by rctneil
    I don't know if this is the best way to do it. I am getting too confused with a template engine and i cannot get to grips with the coding and i'm not sure what words like parse or other things mean.
    Then it probably is best for you to use a CMS and/or blog software. Unless you are ready to crack the books and RTFM, you will have best luck with something more WYSYWYG... Most CMS/blogs are setup so one can choose to not touch much (if any) of the coding.

    I am talking about apps like Joomla, WordPress, Expression Engine, Django, Textpattern... so many to choose from.

    Quote Originally Posted by rctneil
    Would this be a better way. I do have a mini CMS system on the site which i made myself but it will not update the whole of the site just certain areas like adding extra rides into the rides database or things like that.

    Ah, well if you already have a setup, what is the prob?

    Looking at your site, that might be the best way to do it.
    Last edited by mhulse; Aug 13, 2006 at 23:51.

  16. #16
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would imagine what you just described is how most people start with templating.

    just using some simple includes.
    i think it would be a fitting project for you.

    a word of caution, dont do stuff like this
    include($_GET['page']);

    you need to check the value against a list of filename you want to allow before you include it. you could start another thread once you get going.

  17. #17
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by clamcrusher
    you need to check the value against a list of filename you want to allow before you include it. you could start another thread once you get going.
    How do you mean by that?

    Also is there any way that i can have a list of include files and then if the $page value is the same as the name as one of the include files then it will include that specific file? Also is there any benefit to using this method of "include"ing the content pages into the main template page over using the system i currently have where i have all pages and everypage has the layout, template, the layout includes - header, footer, navigation and the content on them as well?

    Regards,
    Neil

  18. #18
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also if i use the including system as i wrote about above then how would i do the homepage?, Because my domain links to index.php?

    Regards,
    Neil

  19. #19
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone help here?

    Cheers

    Neil

  20. #20
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Melbourne, Australia
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm personally opposed to including logic in your templating engine. I think logic should be performed by your functions/methods. In my system I use html style syntax which I think makes things quite simple. My methods are generally quite flexible with their output, so there is really no need. I also include the ability to use php in templates. Also I always have the output generated by methods passed through a template too.

    So I could have like <mgx:get_news template="(template id/name)" />

    Ah yeah temapltes can set vairables too, so the template in the above example may set the number of items to display and such.
    sigy

  21. #21
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Jixor:
    Your method sounds good.

    Just goes to show that there's many different ways to "skin a cat."

    IMHO, rctneil would do best with a CMS and/or a blogging system.

  22. #22
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, I am getting way to confused. I am currently using dreamweaver to create my site. Below is what i am going to do:

    I will close my site for a week while i do the work. I will have one page called template.php which will have the look of everypage and use includes for the header, navigation and footer. I will then have hundreds of mini pages called things such as ride.php, scoreboard.php, links.php, downloads.php and these will be embedded into the template in the area left for page content using includes depending on what the current $page value is. There are to things i need to know:

    1. I a user clicks a link to go to a page such as www.example.com/template.php?page=downloads

    how will i get template to know what include to include? Do i have to put hundreds of if statements in the template page and when i make a new conten page i have to add a new if statement or is there an easier way?

    2. How will i get the page to load template.php$page=home when a user types in my domain such as www.example.com?

    Thanks in advance.

    Regards,
    Neil

  23. #23
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Np about the questions above. They were all answered in the link that mhulse gave above. I shall now go ahead and close the site and start making the changes
    Too be honest they shouldn't take that long. I just have to check all links carefully.

    Regards,
    Neil

  24. #24
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,
    Completed changing all the content to be in samller pages whcih do not include the template so these pages can be "include"d on the index page. but i have a small problem. When people go to a page called www.example.com?page=parkguide they have to choose an area to view more details on using drop down boxes. These boxes are populated using a mysql database table. Below is the code i have for the box:

    <?php

    require "db.php";

    // do query to get id to pass to address bar
    $result = mysql_query("SELECT id, area, showit FROM atm_areas WHERE showit=1 ORDER BY area asc") or die('Error, query failed');
    ?>
    </div>
    <form method="get" action="index.php?page=area&areaid=">
    <div align="center">
    <select name="areaid">
    <optgroup label="::Select an Area::">
    <!--option selected="selected" value="">--Select an Area--</option-->
    <?php
    // go through the database and while there are different areas add them to the list dynamically
    while ($row = mysql_fetch_array($result)) {
    // $areaid is the variable that the area.php page will look for and then use that id to display the right data
    $areaid=$row['id'];
    echo '<option value="' . $areaid . '">' . $row['area'] . '</option>';

    }
    ?>
    </optgroup>
    </select>
    <input name="submit" type="submit" value="Go to Area" />
    </div>
    </form></td>

    I cannot seem to get it to go to the correct page when the submit button is pressed. The page it is meant to go to is www.example.com/index.php?page=area

    but it also needs to pass the variable from the drop down so the Area page can GET the variable to show the correct data. An example of a link before i did any of this change was like:

    www.example.com/area.php?areaid=4

    Please help.

    Regards,

    Neil


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •