SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Template tags

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Template tags

    Can someone tell me how i can create template of php code using template tags... for exampe i have in my code
    $title= abc;
    $username= xyz;
    I want to make this kind of tags for template..I have often seen this kind of tags..
    <$title$> this tag fetch title..
    <$username$> this tag fetch the user name
    sometimes i have seen this kind of tags <%title%> etc.
    Can some one guide me how to create these kind of tags please? i serached google but couldnt find anything useful..
    Thanks..

  2. #2
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Untested:
    PHP Code:
    $template = preg_replace('/(<\$)([A-Z0-9\-_]+)(\$>)/i', '<?php echo $\2?>', $template);
    EDIT: put a backslash between $ and 2, for some reason it's not displaying.

  3. #3
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your code is like this:
    PHP Code:
    <?php
    $title 
    "My page";
    $username "macintosh";
    ?>
    <html>...
    Then that's not really a template system. You want to use <?php echo $title ?> or <?=$title?> (if you have short_open_tag enabled in your php.ini).

    But if you really are phrasing a template file, then you can invent your own template syntax, and use a regex to replace it. You can also try out Smarty, instead of trying to re-invent the wheel.
    http://smarty.php.net/

  4. #4
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hypnos View Post
    You want to use <?php echo $title ?> or <?=$title?> (if you have short_open_tag enabled in your php.ini).
    I wouldn't recommend to use short open tags, they're likely to be removed from PHP 6.

  5. #5
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually just do <title>{{TITLE}}</title>, then at the end of the page, right before i send the output, I read the output buffer into a variable using ob_get_contents(), and bust out preg_replace() magic on it.

    I generally do all my page-specific logic in the included file (say, viewtopic.php), so that means the <title> part of the content has already been sent... using the method above, I can edit/append stuff onto the title (such as the title of the thread currently being viewed).

    There's probably easier ways to do it, but this way works for me.

    Good luck.


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
  •