Using templates

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

    #1

    Using templates

    Anyone feeling like they want to give me a 101 on general template handling in
    PHP?

    My history
    I used to be a Roxen guy, where you could define your own HTML-tags, so that
    "<foo />" could be set by "<define tag='foo'>Hello World</define>" so every
    time you use the tag <foo /> it would be parse and replaced with "Hello World".
    Naturally, within a <define>, any kind of roxen code can be used.

    Today
    I am using "php_value auto_prepend_fi le" and "php_value auto_append_fil e" to
    insert a header and foot script to create the layout of a page, and function()
    in required files to make small scriptlets such as headline() and stuff like
    that.

    The problem
    With PHP, I can't make containers. Not real ones anyway. In Roxen, I could have
    done this:

    <define container="head line">
    <span class="headline ">
    <contents />
    </span>
    </define>

    So <headline>Hello !</headline>
    Would become <span class="headline ">Hello!</span>

    You get the idea.

    Now, with PHP, the above would be:

    function headline($headl ine){
    print "<span class='headline '>$headline</span>";
    }

    But the obvious problem is when you want to use this function for more than
    just a small string - you might want a big block of HTML and PHP code to be
    contained inside a HTML table with a border - for example. In Roxen this would
    be done by:

    <square>
    Big old chunk of text, html, mysql, rxml or whatever.
    </square>

    And the end result would be that big chunk of text surrounded with the code of
    a table. This is useful for lots of things, bbut generally for framing things
    in different ways - mainly for news-boxes on pages

    Currently, Im am doing this in php this way:

    <?=square("star t") ?>
    Big old chunk of text, html, mysql, rxml or whatever.
    <?=square("end" ) ?>

    But that's just ugly code and cumbersome.

    Now to the point (finally!) - How does templating workm, and could it be used
    to accomplish what I am trying to do here? I am aware of 'Smarty' and I have
    downloaded it and played with it, but it was just a difficult concept to grasp
    for me (yeah,. so call me stupid) and it did caching and had compiled scripts
    and whatever. Isn't there a smpler solution?

    Nayway, I'm grateful for any help in this area. :)

    --
    Sandman[.net]
  • Guest's Avatar

    #2
    Re: Using templates

    [color=blue]
    > I used to be a Roxen guy, where you could define your own HTML-tags, so that [/color]
    [color=blue]
    > Now to the point (finally!) - How does templating workm, and could it be used
    > to accomplish what I am trying to do here? I am aware of 'Smarty' and I have
    > downloaded it and played with it, but it was just a difficult concept to grasp
    > for me (yeah,. so call me stupid) and it did caching and had compiled scripts
    > and whatever. Isn't there a smpler solution?[/color]

    You might try patTemplate or simply use XSLT

    _______________ _______________ ______
    Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster

    Comment

    • Chung Leong

      #3
      Re: Using templates

      If you want simple, I guess you can turn on output buffering then pass
      everything through strtr(). Example:

      $my_own_tags = array(
      '<headline>' => '<span class="headline ">',
      '</headline>' => '</span>',
      ....);

      echo strtr(ob_get_cl ean(), $my_own_tags);

      Uzytkownik "Sandman" <mr@sandman.net > napisal w wiadomosci
      news:mr-E1102B.00305519 032004@news.fu-berlin.de...[color=blue]
      > Anyone feeling like they want to give me a 101 on general template[/color]
      handling in[color=blue]
      > PHP?
      >
      > My history
      > I used to be a Roxen guy, where you could define your own HTML-tags, so[/color]
      that[color=blue]
      > "<foo />" could be set by "<define tag='foo'>Hello World</define>" so[/color]
      every[color=blue]
      > time you use the tag <foo /> it would be parse and replaced with "Hello[/color]
      World".[color=blue]
      > Naturally, within a <define>, any kind of roxen code can be used.
      >
      > Today
      > I am using "php_value auto_prepend_fi le" and "php_value auto_append_fil e"[/color]
      to[color=blue]
      > insert a header and foot script to create the layout of a page, and[/color]
      function()[color=blue]
      > in required files to make small scriptlets such as headline() and stuff[/color]
      like[color=blue]
      > that.
      >
      > The problem
      > With PHP, I can't make containers. Not real ones anyway. In Roxen, I could[/color]
      have[color=blue]
      > done this:
      >
      > <define container="head line">
      > <span class="headline ">
      > <contents />
      > </span>
      > </define>
      >
      > So <headline>Hello !</headline>
      > Would become <span class="headline ">Hello!</span>
      >
      > You get the idea.
      >
      > Now, with PHP, the above would be:
      >
      > function headline($headl ine){
      > print "<span class='headline '>$headline</span>";
      > }
      >
      > But the obvious problem is when you want to use this function for more[/color]
      than[color=blue]
      > just a small string - you might want a big block of HTML and PHP code to[/color]
      be[color=blue]
      > contained inside a HTML table with a border - for example. In Roxen this[/color]
      would[color=blue]
      > be done by:
      >
      > <square>
      > Big old chunk of text, html, mysql, rxml or whatever.
      > </square>
      >
      > And the end result would be that big chunk of text surrounded with the[/color]
      code of[color=blue]
      > a table. This is useful for lots of things, bbut generally for framing[/color]
      things[color=blue]
      > in different ways - mainly for news-boxes on pages
      >
      > Currently, Im am doing this in php this way:
      >
      > <?=square("star t") ?>
      > Big old chunk of text, html, mysql, rxml or whatever.
      > <?=square("end" ) ?>
      >
      > But that's just ugly code and cumbersome.
      >
      > Now to the point (finally!) - How does templating workm, and could it be[/color]
      used[color=blue]
      > to accomplish what I am trying to do here? I am aware of 'Smarty' and I[/color]
      have[color=blue]
      > downloaded it and played with it, but it was just a difficult concept to[/color]
      grasp[color=blue]
      > for me (yeah,. so call me stupid) and it did caching and had compiled[/color]
      scripts[color=blue]
      > and whatever. Isn't there a smpler solution?
      >
      > Nayway, I'm grateful for any help in this area. :)
      >
      > --
      > Sandman[.net][/color]



      Comment

      • Perttu Pulkkinen

        #4
        Re: Using templates

        "Sandman" <mr@sandman.net > wrote:
        [color=blue]
        > Currently, Im am doing this in php this way:
        > <?=square("star t") ?>
        > Big old chunk of text, html, mysql, rxml or whatever.
        > <?=square("end" ) ?>[/color]

        Just want to mention that i usually include the contents, not the "borders":

        <div id="submenu">
        <? include submenu.php?>
        </div>

        This gives the benefit of updating easily the contents that are all over the
        site. And when those contents are php, they speculate form variables and
        change automatically according to them.






        Comment

        • Sandman

          #5
          Re: Using templates

          In article <3zv6c.9$_p.0@r ead3.inet.fi>,
          "Perttu Pulkkinen" <perttu.pulkkin en@co.jyu.fi> wrote:
          [color=blue]
          > "Sandman" <mr@sandman.net > wrote:
          >[color=green]
          > > Currently, Im am doing this in php this way:
          > > <?=square("star t") ?>
          > > Big old chunk of text, html, mysql, rxml or whatever.
          > > <?=square("end" ) ?>[/color]
          >
          > Just want to mention that i usually include the contents, not the "borders":
          >
          > <div id="submenu">
          > <? include submenu.php?>
          > </div>
          >
          > This gives the benefit of updating easily the contents that are all over the
          > site. And when those contents are php, they speculate form variables and
          > change automatically according to them.[/color]

          Sorry, that won't help in my situations.

          --
          Sandman[.net]

          Comment

          • Sandman

            #6
            Re: Using templates

            In article <fa6dncg7noGI58 fdRVn-uQ@comcast.com> ,
            "Chung Leong" <chernyshevsky@ hotmail.com> wrote:
            [color=blue]
            > If you want simple, I guess you can turn on output buffering then pass
            > everything through strtr(). Example:
            >
            > $my_own_tags = array(
            > '<headline>' => '<span class="headline ">',
            > '</headline>' => '</span>',
            > ...);
            >
            > echo strtr(ob_get_cl ean(), $my_own_tags);[/color]

            Ok, sounds interesting - exactly how do I do it? Is it called at the very last
            in the script or could I call it in the beginning? Could it be used with
            external files? Like so:

            ## headline.txt
            <span class='headline '>
            &contents; <- To be replaced somehow.
            </span>

            ## init.php (loaded every time - before the current page)
            ???

            ## index.php:
            <headline>Hello !</headline>



            Looking at php.net, it seems ob_get_clean() fetches the current buffer, so
            executing it in the beginning of the script won't do much. Hmmm...

            --
            Sandman[.net]

            Comment

            • Chung Leong

              #7
              Re: Using templates

              Uzytkownik "Sandman" <mr@sandman.net > napisal w wiadomosci
              news:mr-23BF2C.12250319 032004@news.fu-berlin.de...[color=blue]
              > Ok, sounds interesting - exactly how do I do it? Is it called at the very[/color]
              last[color=blue]
              > in the script or could I call it in the beginning? Could it be used with
              > external files? Like so:
              >
              > ## headline.txt
              > <span class='headline '>
              > &contents; <- To be replaced somehow.
              > </span>
              >
              > ## init.php (loaded every time - before the current page)
              > ???
              >
              > ## index.php:
              > <headline>Hello !</headline>[/color]

              You would call ob_start() in init.php and strtr(ob_get_cl ean(), ...) in the
              file that's included at the end, or you can give a callback to ob_start()
              which gets called automatically when the page is flushed. Example

              init.php:

              function add_custom_tag( $name, $content) {
              global $custom_tags;

              $custom_tages["<$name />"] = $content;
              }

              function add_custom_cont ainer($name, $start, $end) {
              global $custom_tags;

              $custom_tages["<$name>"] = $start;
              $custom_tages["</$name>"] = $end;
              }

              function replace_custom_ tags($buffer) {
              global $custom_tags;

              return strtr($buffer, $custom_tags);
              }

              load_tag_defini tions('custom_t ags.txt');
              ob_start('repla ce_custom_tags' );

              For the tag definitions, you can use Roxen's syntax with something like
              this:

              function load_tag_defini tions($filename ) {
              $def = file_get_conten ts($filename);

              if(preg_match_a ll('!<define tag="(.*?)">(*. ?)</define>!s', $def,
              $matches)) {
              $tags = $matches[1];
              $content = $matches[2];
              foreach($tags as $index => $tag) {
              add_custom_tag( $tag, $content[$index]);
              }
              }
              if(preg_match_a ll('!<define container="(.*? )">(*.?)<conten t
              />(*.?)</define>!s', $def, $matches)) {
              $tags = $matches[1];
              $start = $matches[2];
              $end = $matches[3];
              foreach($tags as $index => $tag) {
              add_custom_cont ainer($tag, $start[$index], $end[$index]);
              }
              }
              }



              Comment

              • rush

                #8
                Re: Using templates

                "Sandman" <mr@sandman.net > wrote in message
                news:mr-E1102B.00305519 032004@news.fu-berlin.de...[color=blue]
                > The problem
                > With PHP, I can't make containers. Not real ones anyway. In Roxen, I could[/color]
                have[color=blue]
                > done this:
                >
                > <define container="head line">
                > <span class="headline ">
                > <contents />
                > </span>
                > </define>[/color]

                Maybe this could help:



                rush
                --



                Comment

                • Andy Hassall

                  #9
                  Re: Using templates

                  On Fri, 19 Mar 2004 00:30:55 +0100, Sandman <mr@sandman.net > wrote:
                  [color=blue]
                  >Anyone feeling like they want to give me a 101 on general template handling in
                  >PHP?
                  >
                  >My history
                  >I used to be a Roxen guy, where you could define your own HTML-tags, so that
                  >"<foo />" could be set by "<define tag='foo'>Hello World</define>" so every
                  >time you use the tag <foo /> it would be parse and replaced with "Hello World".
                  >Naturally, within a <define>, any kind of roxen code can be used.[/color]

                  Sounds like a Smarty templates block function.




                  --
                  Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                  http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

                  Comment

                  Working...