Templates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yehia

    Templates

    hello,

    is this the right place to post questions or what?

    anyway i'm trying to make a simple template system using php,
    here is:
    a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
    the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
    the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc

    but don't know why the page using the template is messed up,

    it should like this http://www.yehiaeg.com/focusonvbs/desc.htm ,
    any ideas?

    Yehia
  • rush

    #2
    Re: Templates

    "Yehia" <yehia@yehiaeg. com> wrote in message
    news:e5928add.0 407151018.32458 945@posting.goo gle.com...[color=blue]
    > anyway i'm trying to make a simple template system using php,
    > here is:
    > a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
    > the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
    > the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc
    >
    > but don't know why the page using the template is messed up,
    >
    > it should like this http://www.yehiaeg.com/focusonvbs/desc.htm ,
    > any ideas?[/color]

    well without seeing your code, it is hard to tell, exept that you have got
    something wrong :) . Have you considered using some existing template system
    for php?

    rush
    --



    Comment

    • Nel

      #3
      Re: Templates

      "rush" <pipa@rush.aval on.hr> wrote in message
      news:cd6k8n$6eo $1@ls219.htnet. hr...[color=blue]
      > "Yehia" <yehia@yehiaeg. com> wrote in message
      > news:e5928add.0 407151018.32458 945@posting.goo gle.com...[color=green]
      > > anyway i'm trying to make a simple template system using php,
      > > here is:
      > > a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
      > > the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
      > > the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc
      > >
      > > but don't know why the page using the template is messed up,
      > >
      > > it should like this http://www.yehiaeg.com/focusonvbs/desc.htm ,
      > > any ideas?[/color]
      >
      > well without seeing your code, it is hard to tell, exept that you have got
      > something wrong :) . Have you considered using some existing template[/color]
      system[color=blue]
      > for php?
      >
      > rush
      > --
      > http://www.templatetamer.com/
      >[/color]
      If it's any help I would recommend a different method, not necessarily
      better for everyone, but for me.

      Create a HTML template in your authoring packaging, preferably using CSS.

      Then in place your own tags anywhere on the page, where you would like to
      swap the tag for some variable.

      e.g. {{pagetitle}} or {{pagetext}}

      Then save that as a file called template.htm

      Then read your template.htm file in as array, get your pagetitle and
      pagetext (+ other variables) from your database (I assume) and use
      preg_replace to change over your tags to the appropriate string. (see
      below). Then you just have to output your page!

      Nel

      $template = "";
      $temp = file($setuptemp late);
      foreach($temp as $line) { $template .= $line; }

      // update template with menus and content
      $pat = array("/{{pagetext}}/s",
      "/{{pagetitle}}/s",
      "/{{topmenuh}}/s",
      "/{{topmenuv}}/s",
      "/{{submenuh}}/s",
      "/{{submenuv}}/s"
      );
      $rep = array("$pagetex t",
      "$pagetitle ",
      "$topmenuh" ,
      $topmenuv,
      "$submenuh" ,
      $submenuv
      );
      // replace all occurrences of search patterns with according replacements

      $template = preg_replace($p at, $rep, $template);

      echo $template;


      Comment

      • Christopher Finke

        #4
        Re: Templates

        > anyway i'm trying to make a simple template system using php,[color=blue]
        > here is:
        > a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
        > the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
        > the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc[/color]

        Your problem is with the HTML, not the PHP. Delete the </table> tag at the
        end of temp1.inc, and that should fix the biggest layout errors.

        Chris Finke


        Comment

        • Nel

          #5
          Re: Templates

          Forgot to say, main advantage of preg_replacing your own tags is that you
          don't have to split your template in to a header and footer, so you can
          change the template simply by loading any new html page containing your
          tags.

          Nel.


          Comment

          • neur0maniak

            #6
            Re: Templates

            Yehia wrote:[color=blue]
            > hello,
            >
            > is this the right place to post questions or what?
            >
            > anyway i'm trying to make a simple template system using php,
            > here is:
            > a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
            > the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
            > the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc
            >
            > but don't know why the page using the template is messed up,
            >
            > it should like this http://www.yehiaeg.com/focusonvbs/desc.htm ,
            > any ideas?
            >
            > Yehia[/color]

            Have you thought of using the Smarty template system?


            It's really easy to get going with it and does what Nel has recommended.

            Comment

            • Yehia

              #7
              Re: Templates

              actually the html work well:
              http://www.yehiaeg.com/focusonvbs/version.htm,

              and here is the contents of test.php :
              _______________ _______________ _______________
              <html>
              <head>
              <title>YehiaE g &gt; Products &gt; Focus On VBS &gt; Download</title>
              <?php
              include 'temp1.inc';
              ?>
              <p class="NormalFo nt"><br>
              Latest version: 1.5<br>
              Size: 509 KB <br>
              License: Demo<br>
              Requirements: None<br>
              Download Link: <a
              href="http://www.yehiaeg.com/focusonvbs.exe" >Click Here</a>
              <br><br>
              <strong>Note: </strong> "Save /As", "Cut", "Copy", are disabled
              in this demo version.<br><br >
              Available also on <a
              href="http://www.download.co m/Focus-on-VBS/3000-2352-10223282.html?t ag=lst-0-1"><img
              src="cnet.gif" width="127" height="27" border="0"></a>
              </p>
              <?php
              include 'temp2.inc';
              ?>
              _______________ _______________ _______________ ________
              All i did was just opening the original htm file and copy every before
              the main text in temp1.inc and everything after in temp2.inc ,

              Don't know what i'm doing wrong, PLZ help guys.


              "Christophe r Finke" <chris@efinke.c om> wrote in message news:<2lo6c4Ff9 f7iU1@uni-berlin.de>...[color=blue][color=green]
              > > anyway i'm trying to make a simple template system using php,
              > > here is:
              > > a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
              > > the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
              > > the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc[/color]
              >
              > Your problem is with the HTML, not the PHP. Delete the </table> tag at the
              > end of temp1.inc, and that should fix the biggest layout errors.
              >
              > Chris Finke[/color]

              Comment

              • Jeffrey Silverman

                #8
                Re: Templates

                On Fri, 16 Jul 2004 02:14:31 -0700, Yehia wrote:
                [color=blue]
                > All i did was just opening the original htm file and copy every before
                > the main text in temp1.inc and everything after in temp2.inc ,
                >
                > Don't know what i'm doing wrong, PLZ help guys.[/color]

                Evidently, you didn't do quite what you say you did.

                Take the pages as produced in your browser. Save each -- the working HTML
                file and the PHP file that doesn't quite work -- and compare the two
                documents. (here is where a Unix-like OS comes in handy: use the diff
                command. Or are you using Windows?)

                diff desc.htm test.php.html | less

                Compare the two files *carefully*!! Look at the differences -- there are
                MANY. There should be NONE! You want the final output from your PHP page
                to look EXACTLY like the original HTML page, but it doesn't.

                So, then, my question to you is: How can your PHP page end up looking
                exactly like your HTML draft page?


                later...

                --
                Jeffrey Silverman
                jeffrey@jhu.edu

                Comment

                • R. Rajesh Jeba Anbiah

                  #9
                  Re: Templates

                  "Nel" <nelly@ne14.co. NOSPAMuk> wrote in message news:<40f6e060$ 0$204$ed2619ec@ ptn-nntp-reader03.plus.n et>...
                  <snip>[color=blue]
                  > If it's any help I would recommend a different method, not necessarily
                  > better for everyone, but for me.
                  >
                  > Create a HTML template in your authoring packaging, preferably using CSS.
                  >
                  > Then in place your own tags anywhere on the page, where you would like to
                  > swap the tag for some variable.
                  >
                  > e.g. {{pagetitle}} or {{pagetext}}
                  >
                  > Then save that as a file called template.htm
                  >
                  > Then read your template.htm file in as array, get your pagetitle and
                  > pagetext (+ other variables) from your database (I assume) and use
                  > preg_replace to change over your tags to the appropriate string. (see
                  > below). Then you just have to output your page!
                  >
                  > Nel
                  >
                  > $template = "";
                  > $temp = file($setuptemp late);
                  > foreach($temp as $line) { $template .= $line; }
                  >
                  > // update template with menus and content
                  > $pat = array("/{{pagetext}}/s",
                  > "/{{pagetitle}}/s",
                  > "/{{topmenuh}}/s",
                  > "/{{topmenuv}}/s",
                  > "/{{submenuh}}/s",
                  > "/{{submenuv}}/s"
                  > );
                  > $rep = array("$pagetex t",
                  > "$pagetitle ",
                  > "$topmenuh" ,
                  > $topmenuv,
                  > "$submenuh" ,
                  > $submenuv
                  > );
                  > // replace all occurrences of search patterns with according replacements
                  >
                  > $template = preg_replace($p at, $rep, $template);
                  >
                  > echo $template;[/color]

                  Templates are one of the most controversial topic, at least in
                  this group. For example,
                  <http://groups.google.c om/groups?threadm= c17ndg%24l3t%24 1%40newsreader. mailgate.org>

                  * "Using a template system is a little like going back to using PHP
                  1--you get a far less powerful, less flexible system to work with." --
                  phpSt.Chung Leong
                  * "What about PHP ?-)" -- phpSt.Bruno Desthuilliers

                  Also see:
                  1. http://www.phpbuilder.com/annotate/m...hp3?id=1013434
                  2. http://www.phpbuilder.com/annotate/m...hp3?id=1013711
                  3. http://www.phpbuilder.com/annotate/m...hp3?id=1013461
                  4. http://www.phppatterns.com/index.php...cleview/4/1/1/
                  5. http://www.massassi.com/php/articles/template_engines/
                  6. http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf

                  --
                  | Just another PHP saint |
                  Email: rrjanbiah-at-Y!com

                  Comment

                  • Yehia

                    #10
                    Re: Templates

                    don't want to use any third party code, i'm happy with the php include function,

                    anyway, still want to know why my code doesn't work!


                    neur0maniak <usenet@neur0ma niak.co.uk> wrote in message news:<40f7069c$ 0$92638$ed2619e c@ptn-nntp-reader02.plus.n et>...[color=blue]
                    > Yehia wrote:[color=green]
                    > > hello,
                    > >
                    > > is this the right place to post questions or what?
                    > >
                    > > anyway i'm trying to make a simple template system using php,
                    > > here is:
                    > > a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
                    > > the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
                    > > the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc
                    > >
                    > > but don't know why the page using the template is messed up,
                    > >
                    > > it should like this http://www.yehiaeg.com/focusonvbs/desc.htm ,
                    > > any ideas?
                    > >
                    > > Yehia[/color]
                    >
                    > Have you thought of using the Smarty template system?
                    > http://smarty.php.net/
                    >
                    > It's really easy to get going with it and does what Nel has recommended.[/color]

                    Comment

                    • neur0maniak

                      #11
                      Re: Templates

                      Yehia wrote:
                      [color=blue]
                      > don't want to use any third party code, i'm happy with the php include function,
                      >
                      > anyway, still want to know why my code doesn't work!
                      >[/color]

                      Okay, how about doing the reverse of what you're trying to do?

                      The way I do simple "include style" templates is this:

                      You've got the HTML template you'd like to use, call that 'index.php'

                      Insert this code at the top:
                      $strPage=trim($ _GET["p"]);
                      if (strlen($strPag e)==0) {$strPage="main ";} //This is your default page
                      if (!file_exists(" pages/{$strPage}.php" )) {$strPage="404" ;}

                      Create a folder called 'pages' in the folder of 'index.php'
                      Place your pages in that folder, all with a '.php' extension.
                      Make sure you make one called "404.php" for when a user tries a URL
                      where a page is missing.
                      In 'index.php' replace the content area of your template with:
                      <?php include ("pages/{$strPage}.php" ); ?>

                      From then on, you can access pages by using 'index.php?p=pa gename'
                      This saves you from using two pages for your template.

                      Things to keep in mind:
                      When submitting a FORM using METHOD="GET", make sure you have a INPUT
                      tag of TYPE="HIDDEN", NAME="p", and VALUE="pagename " (If you don't then
                      you'll not submit to the page you want to)
                      You can't set headers/cookies in your pages, as output takes place
                      before the page is included, though there are many ways of working
                      around it..


                      Comment

                      • Tim Van Wassenhove

                        #12
                        Re: Templates

                        In article <40f859eb$0$454 0$ed2619ec@ptn-nntp-reader01.plus.n et>, neur0maniak wrote:[color=blue]
                        > Yehia wrote:[/color]
                        [color=blue]
                        > Insert this code at the top:
                        > $strPage=trim($ _GET["p"]);
                        > if (strlen($strPag e)==0) {$strPage="main ";} //This is your default page
                        > if (!file_exists(" pages/{$strPage}.php" )) {$strPage="404" ;}[/color]

                        ?p=../index


                        --
                        Tim Van Wassenhove <http://home.mysth.be/~timvw>

                        Comment

                        • neur0maniak

                          #13
                          Re: Templates

                          Tim Van Wassenhove wrote:
                          [color=blue]
                          > In article <40f859eb$0$454 0$ed2619ec@ptn-nntp-reader01.plus.n et>, neur0maniak wrote:
                          >[color=green]
                          >>Yehia wrote:[/color]
                          >
                          >[color=green]
                          >>Insert this code at the top:
                          >> $strPage=trim($ _GET["p"]);
                          >> if (strlen($strPag e)==0) {$strPage="main ";} //This is your default page
                          >> if (!file_exists(" pages/{$strPage}.php" )) {$strPage="404" ;}[/color]
                          >
                          >
                          > ?p=../index
                          >[/color]
                          $strPage=trim($ _GET["p"]);
                          if (strlen($strPag e)==0 || substr($strPage , "..")!==fal se)
                          {$strPage="main ";} //This is your default page
                          if (!file_exists(" pages/{$strPage}.php" )) {$strPage="404" ;}

                          Comment

                          • neur0maniak

                            #14
                            Re: Templates

                            neur0maniak wrote:
                            [color=blue]
                            > Tim Van Wassenhove wrote:
                            >[color=green]
                            >> In article <40f859eb$0$454 0$ed2619ec@ptn-nntp-reader01.plus.n et>,
                            >> neur0maniak wrote:
                            >>[color=darkred]
                            >>> Yehia wrote:[/color]
                            >>
                            >>
                            >>[color=darkred]
                            >>> Insert this code at the top:
                            >>> $strPage=trim($ _GET["p"]);
                            >>> if (strlen($strPag e)==0) {$strPage="main ";} //This is your default
                            >>> page
                            >>> if (!file_exists(" pages/{$strPage}.php" )) {$strPage="404" ;}[/color]
                            >>
                            >>
                            >>
                            >> ?p=../index
                            >>[/color]
                            > $strPage=trim($ _GET["p"]);
                            > if (strlen($strPag e)==0 || substr($strPage , "..")!==fal se)
                            > {$strPage="main ";} //This is your default page
                            > if (!file_exists(" pages/{$strPage}.php" )) {$strPage="404" ;}
                            >[/color]
                            urgh... replace substr with strpos

                            Comment

                            • Yehia

                              #15
                              Re: Templates

                              ok, i did copy them EXACTLY and it kinda worked but still have some
                              questions:
                              1.when using javascript in a php file should i write like this
                              <script language="javas cript> <!-- some function --> </script>
                              or like this <script language="javas cript"> some function </script>

                              2. somethings may vary from one page to another besides its content
                              like the page title, what is the simplest way to do this?
                              i read about sending arguments to the php file and then assign to the
                              page title, but this not really neat, what if i want to make it a long
                              title:
                              test.php?pageti tle=This a really long title, really long
                              one............
                              Anyother wahy to do this?

                              my questions might look stupid but i'm just really a beginner in php,
                              Thanks

                              neur0maniak <usenet@neur0ma niak.co.uk> wrote in message news:<40f7069c$ 0$92638$ed2619e c@ptn-nntp-reader02.plus.n et>...[color=blue]
                              > Yehia wrote:[color=green]
                              > > hello,
                              > >
                              > > is this the right place to post questions or what?
                              > >
                              > > anyway i'm trying to make a simple template system using php,
                              > > here is:
                              > > a page using the template: http://www.yehiaeg.com/focusonvbs/test.php
                              > > the header: http://www.yehiaeg.com/focusonvbs/temp1.inc
                              > > the footer: http://www.yehiaeg.com/focusonvbs/temp2.inc
                              > >
                              > > but don't know why the page using the template is messed up,
                              > >
                              > > it should like this http://www.yehiaeg.com/focusonvbs/desc.htm ,
                              > > any ideas?
                              > >
                              > > Yehia[/color]
                              >
                              > Have you thought of using the Smarty template system?
                              > http://smarty.php.net/
                              >
                              > It's really easy to get going with it and does what Nel has recommended.[/color]

                              Comment

                              Working...