Simple template system

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Radon3k
    Senior Member
    • Nov 2001
    • 1889

    Simple template system

    I'd like to create a simple template system for no particular reason other than I want to do it. I realize solutions such as Smarty exist, but I'd prefer to create one on my own. Something that is simple but I can expand on it should I want to. What is the best way to do this? I'm not looking for code, but rather a pointer in the right direction of what needs to be done. I've tried before but I've got to be missing something because all of my attempts fail. Thanks.
  • daemon
    Senior Member
    • Jun 2003
    • 2351
    • 3.5.x

    #2
    With my template parser, I use a very simple loop that goes through every character of the template string and checks for matches of the open tag. When one is found, it looks for an <else> or </if> tag. It uses a stack to add any nested <if> tags so it can then balance the </if>s. At the end, when the stack is 0 and there's an </if>, it takes the position of the open tag and the position of the close tag, substr()s it and then processes the data. Finally, it uses str_replace() to replace the original extracted text with the processed data and advances from the previous character position by one place.

    If you want to see the actual code for it, download ISSO (my framework) and open up template.php.
    Bugdar: PHP bug tracking software that is beautiful, fast, and robust.

    Comment

    • Radon3k
      Senior Member
      • Nov 2001
      • 1889

      #3
      Thanks daemon. However, I'm still a little bit lost as to where to start, including how to set up the database and what kind of code I should be trying to write and how everything should be organized. Could you provide any insight into that?

      Comment

      • eXaulz
        Senior Member
        • Jan 2005
        • 469

        #4
        Wait, do you want a Flat-File (templates are files) template engine or a DB (templatesare table entries) template-ngine?

        Comment

        • rossco_2005
          Senior Member
          • Apr 2005
          • 465
          • 4.1.x

          #5
          The way I do templates is to just put something like:
          Code:
          eval('$whateverbits .= "' . addslashes(file_get_contents('templatefolder/template.html')) . '";');
          That can get you started with creating variables from template files.
          If you want to use teh database it will get a little harder with need for caching templates and stuff like that, so create a function for it.

          I'm not sure how to do conditions in it, would like to hear some peoples ideas though.
          Last edited by rossco_2005; Wed 24 May '06, 11:53am.

          http://filesharingtalk.com

          Comment

          • Radon3k
            Senior Member
            • Nov 2001
            • 1889

            #6
            Originally posted by eXaulz
            Wait, do you want a Flat-File (templates are files) template engine or a DB (templatesare table entries) template-ngine?
            Sorry for the delay in replying. I'm looking for a simple database template system, something along the lines of vBulletin but not quite as complex.

            Comment

            • °°Ben³
              New Member
              • Sep 2005
              • 0
              • 3.5.x

              #7
              Why you want to use a template engine that uses templates that are stored in a database?
              Why not using files?

              It's german but the code describes itself .. imho:
              --> http://forum.developers-guide.net/showthread.php?t=684

              Maybe it helps you?

              If not .. why not using Smarty instead? Sure, Smarty is sometimes a little bit toooo large but I do not know what you want to realize.

              Comment

              • daemon
                Senior Member
                • Jun 2003
                • 2351
                • 3.5.x

                #8
                Opening files is actually slower than using a database by up to a second.
                Bugdar: PHP bug tracking software that is beautiful, fast, and robust.

                Comment

                • Radon3k
                  Senior Member
                  • Nov 2001
                  • 1889

                  #9
                  Originally posted by °°Ben³
                  Why you want to use a template engine that uses templates that are stored in a database?
                  Why not using files?
                  I really don't want to mess with added files unless there is a good reason to. I prefer just to store it in a database.

                  Originally posted by °°Ben³
                  If not .. why not using Smarty instead? Sure, Smarty is sometimes a little bit toooo large but I do not know what you want to realize.
                  I want to create my own system just because I can. I know Smarty exists however I do not wish to use it. Any other ideas?

                  Comment

                  widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                  Working...