Results 1 to 10 of 10

Thread: my own template engine

  1. #1
    Trusted Guru saltedm8's Avatar
    Join Date
    Jun 2011
    Posts
    1,969
    Blog Entries
    3

    Default my own template engine 24 May 2011 @ 08.11

    Using a tutorial I have created my own template engine as follows

    template.php
    PHP Code:
    <?php 
    class Digitalp{
         
         public 
    $output;
         public 
    $file;
         public 
    $values = array();
         
         function 
    __construct($file){
             
    $this->file $file;
             
    $this->output file_get_contents($this->file); 
             
         }
         
         function 
    set($key$value){
             
    $this->values[$key] = $value;
             }
             
         function 
    output(){
             foreach(
    $this->values as $key => $value){
                 
    $tagsToReplace "[$key]";
                 
    $this->output str_replace($tagsToReplace$value$this->output);
                 }
                 return 
    $this->output;
             
             }
    }
    ?>
    index.php
    PHP Code:
    <?php 
    require("template.php");

    $dig = new Digitalp('defult.tpl' 'form.tpl');
    $dig->set ('title','my site title');
    $dig->set ('form''include(form.php)');

    echo 
    $dig->output();
    ?>
    defult.tpl
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    [title]
    <hr />
    [form]
    <body>
    </body>
    </html>
    I have a question,

    as you can see in the index.php I am trying to call an external file and assign it the name 'form', this does not seem to work as obviously I want to position it where [form] is in the defult.php.. does anyone know how to make it work ?

    thanks
      Reply With Quote

  2. #2
    Junior Member webape's Avatar
    Join Date
    Jul 2010
    Posts
    3

    Default 24 May 2011 @ 10.13

    You would need to write another function in your template.php

    Something like the below after the set function:
    Code:
     function set($key, $value){
             $this->values[$key] = $value;
             }
         function get_file($key, $file){
            ob_start();                    
            include($file);            
            $contents = ob_get_contents(); 
            ob_end_clean();  
            $this->values[$key] = $contents;              
               
          }
    Then call the include as:

    Code:
    $dig->get_file('form', "form.php");
    Hope this works?
    Cheers, Matt
    Last edited by Frinkky; 24 May 2011 at @ 11.20. Reason: Forced signature removed. Please read the forum rules on signatures: http://bit.ly/leQSJI
      Reply With Quote

  3. #3
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 24 May 2011 @ 10.49

    no! no! no!

    why can't form.php just contain a function which parses another template (containing the form HTML) and return this HTML?

    form.php
    PHP Code:
    function get_form_html() {
      
    // create form html
      
    return $html;

    index.php
    PHP Code:
    include('form.php');
    $dig->set ('form'get_form_html()); 
      Reply With Quote

  4. #4
    Junior Member webape's Avatar
    Join Date
    Jul 2010
    Posts
    3

    Default 24 May 2011 @ 11.01

    I'm a little concerned about your no, no, no comment?

    Nothing wrong with my solution and it is reusable - and answers the question as asked?
    Not saying there is anything wrong with your solution.
    Just wondering why you think there is something wrong with my solution?
    Last edited by Frinkky; 24 May 2011 at @ 11.18. Reason: Forced signature removed. Please read the forum rules on signatures: http://bit.ly/leQSJI
      Reply With Quote

  5. #5
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 24 May 2011 @ 11.29

    Sorry, should have explained....

    Yes, it works, but it really is quite bad practice for a number of reasons:
    - breaks the normal line of execution which makes the code very hard to debug.
    - kills all chances of error handling.
    - parameters would have to be global (you can't pass them normally).
    - same with return parameters.
    - code readability.
      Reply With Quote

  6. #6
    Trusted Guru saltedm8's Avatar
    Join Date
    Jun 2011
    Posts
    1,969
    Blog Entries
    3

    Default 24 May 2011 @ 11.39

    Thank you that did it, my only issue now would be passing variables from the function to the template, so for example if I had something like

    PHP Code:
    <?php function get_form_html() {
      
    $html '<form action=$formsubmit method="get"><input name="" type="text" /><textarea name="" cols="" rows=""></textarea></form>';
         return 
    $html;
    }  
    ?>
    it would pass the $forumsubmit over to default.php if $forumsubmit = myform.php for example
      Reply With Quote

  7. #7
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 24 May 2011 @ 12.05

    if you're using a template engine, then why have any HTML in your PHP at all?

    PHP Code:
    <?php function get_form_html($formsubmit) { // add parameters which you need from index.php
      
    $dig = new Digitalp('form.tpl');
      
    $dig->set ('formsubmit',$formsubmit); 
      return 
    $dig->output();
    }  
    ?>
    form.tpl
    Code:
    <form action=[formsubmit] method="get"><input name="" type="text" /><textarea name="" cols="" rows=""></textarea></form>
      Reply With Quote

  8. #8
    Trusted Guru saltedm8's Avatar
    Join Date
    Jun 2011
    Posts
    1,969
    Blog Entries
    3

    Default 24 May 2011 @ 12.53

    I dont know if you have ever messed with phpbb, but that forum software uses or used {COMMAND} as its templating system ( I was using phpbb2 years ago ) each {COMMAND} called individual parts of the site template elements, {CONTACT_FORM}, {IMAGE_GALLERY} , {LATEST_POSTS} etc etc, all call every piece of those elements that minimizes the amount of stuff you have to learn in order to create a custom forum template in that case, I am attempting the same only with a cms to make it simpler to create templates for it, replacing things like [content] and [twitter_link] etc all controlled from the admin panel, but hopefully making it also possible for users to be able to dynamically create custom [calls] for their templates too by defining html with pre-made [ commands ] to utilise the built in php of my cms ( like an email form ).
      Reply With Quote

  9. #9
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 24 May 2011 @ 13.04

    I have no experience developing templates for phpbb, but the concept sounds like a good.
      Reply With Quote

  10. #10
    Trusted Guru WelshStew's Avatar
    Join Date
    Dec 2008
    Posts
    2,555
    Blog Entries
    3

    Default 6 Jun 2011 @ 13.47

    Hows your CMS coming along?
      Reply With Quote

Similar Threads

  1. New Domain, New Template Design..
    By saltedm8 in forum Design & Layout
    Replies: 10
    Last Post: 10 Jan 2011, @ 09.56
  2. How to design template?
    By lizahellen in forum Design & Layout
    Replies: 2
    Last Post: 26 Oct 2010, @ 09.16
  3. How to design template?
    By sellinageorge in forum Design & Layout
    Replies: 6
    Last Post: 28 Sep 2010, @ 15.17
  4. using a css file with smarty template
    By sudhakararaog in forum PHP, ASP & Java
    Replies: 0
    Last Post: 21 Oct 2009, @ 03.40
  5. Corporate Site Template
    By aazad in forum Free Reviews & Showcase
    Replies: 17
    Last Post: 6 Apr 2009, @ 17.03

Tags for this Thread

Posting Permissions

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