SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    {$var} - How do they work?

    I was going through invision boards codes today and I noticed this in one of the templates:

    {$ibforums->lang['sm_forum_leaders']}

    Can anyone explain to me how they work? I know they are replaced by actual values later on.

    any article on this?

    thx
    asT.

  2. #2
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I only use that with the heredoc method, but I think you can use that in variables as well. For example:

    PHP Code:
     $var 'variable';

     
    $HTML = <<<HTML
    This is a $var
    This is also a 
    {$var}
    HTML;

    // Or:

     
    $HTML "This is a $var";
     
    $HTML .= "This is also a {$var} 
    As far as I know, its the same as a regular variable. I've also seen something like this: {VAR} in some of the phpBB templates, though I think those are constants.....
    - Saj

  3. #3
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont think it is the same as a variable in php(although i'm not 100% on this). I think its a placeholder for a template engine( which would be a template variable). The template engine parses the template replacing {$VAR} with what $VAR is.
    Erh

  4. #4
    SitePoint Addict Resident Evil's Avatar
    Join Date
    May 2003
    Location
    Barrow
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its a Property of an object.
    u need to know OO stuff to understand it

  5. #5
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I know most basic OO stuff, and I also know it can be used out of a class....at least its worked for me...
    - Saj

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The curly braces are valid PHP. See http://us4.php.net/manual/en/languag...ables.variable

    The syntax is most useful when dealing with variable variables, but can be used any time you need to make it especially clear to the parser what you mean.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, Jason got it right! For more information, check out the man pages on simple syntax and complex syntax.

  8. #8
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx for the replies guys.

    Quote Originally Posted by Resident Evil
    Its a Property of an object.
    u need to know OO stuff to understand it
    Yep i know OO, but with a java background

  9. #9
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    It's also handy when using arrays in strings. Like this -
    PHP Code:
     $var "<strong>Name:</strong> {$array['username']}<br />\n"

  10. #10
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...but he's talking about {$var} being in a template not a php script or string etc. Are you saying that by putting {$var} in a template file it will get replaced just like its in a string? I would be interested in knowing how you get this to work.
    Erh

  11. #11
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well yes, I would guess that the template 'engine' consists more or less of a eval call.

  12. #12
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what would a simple example look like? using eval in a simple template engine to output a simple template which contains {$var}. please
    Erh

  13. #13
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    What kind of template files does invision boards use?
    If they just use .php files as template then they won't need to do an eval().

    --ed

  14. #14
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gaheris
    Well yes, I would guess that the template 'engine' consists more or less of a eval call.
    I would have to disagree wtih you. Eval is known to be a bit slow, so that would be a pretty awful waste. I wrote a simple template engine that I'll share, that does what I need anyway (no where near the sophystication of Smarty, but I don't need Smarty or I'd use it. This templating engine is blazing fast for the basics).

    It supports variables, and it supports a tag displaying another template. I use this on all my sites.

    template.php:
    PHP Code:
    <?php
    class Template
    {
    var 
    $template;
    function 
    Template($name)
    {
    $sql mysql_query("SELECT * FROM template WHERE tpl_name='$name'");
    if(
    mysql_num_rows($sql) == 0) die("Invalid Template (".$name.") Selected");
    while(
    $row mysql_fetch_object($sql))
    {
    $this->template $row->template;
    }
    }
    function 
    assign($tag$old)
    {
    $this->template str_replace("{".$tag."}"$old$this->template);
    }
    function 
    parse($tag$class)
    {
    $this->template str_replace("{".$tag."}"$class->template$this->template);
    }
    function 
    display()
    {
    echo 
    $this->template;
    }
    }
    ?>
    Tpl.php:
    PHP Code:
    class TPL{
    var 
    $tpl;
    var 
    $start;
    var 
    $stop;
    function 
    TPL($template 'layout.html')
    {
    $this->tpl = new Template($template);
    }
    function 
    getmicrotime(){ 
    list(
    $usec$sec) = explode(" ",microtime()); 
    return ((float)
    $usec + (float)$sec); 
    }
    function 
    compare()
    {
    return 
    $this->stop $this->start;
    }
    function 
    pageStart($title "")
    {
    $this->tpl->assign("TITLE"$title);
    ob_start();
    }
    function 
    pageFinish()
    {
    $content ob_get_contents();
    ob_end_clean();
    $this->tpl->assign("CONTENT"$content);
    $this->stop $this->getmicrotime();
    if (
    $_GET['debug'] == 1)
    {
    $debug "[ Queries: ".$DB->querycount." | Execution Time: ".$this->compare()." ]";
    $this->tpl->assign("DEBUG"$debug);
    }
    else
    {
    $this->tpl->assign("DEBUG""");
    }
    echo 
    $this->tpl->display();
    }

    Then a quick example would be:

    PHP Code:
    <?php
     
    require("tpl.php");
     
    $tpl = new Tpl("layout.html");
    $tpl->pageStart("Test Page");
    ?>
    Content..
    <?php
    $tpl
    ->pageFinish();
    ?>
    And if you had a links.html, you could parse {LINKS} with another function in tpl.php. Pretty straight forward templating systme, and quick as a bee.

    Thoguth I'd share this.

    Regards,
    Nathan Wong

    P.S. If anybody needs an explination, I'd be happy to explain.

    [ Edit: Ignore $DB, my bad, it's a MySQL abstration layer I use and I guess I use it on all my pages too... ]

  15. #15
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coo_t2
    What kind of template files does invision boards use?
    If they just use .php files as template then they won't need to do an eval().

    --ed
    (Sorry to post twice in a row)

    IPB's skinning system is complex. I'm currently doing a job that needs integration. The /s1/skin_whatever.php file is a bunch of PHP functions with HTML in it. Each function just has a return, with an <<<EOF EOF; to output it. I presume the administration panel just calls the functions when neccesary to get the output, and does an actual fwrite to the PHP file appropriately (bit of coding work there I presume).

    To call it, you go $this->html = $std->load_template("skin_whatever");, and then $this->html is a class. The class then lets you call functions and pass the variables to them. This is a pretty neat scheme, and is good for a developer, and I'm amazed at how they got the administration panel to work.

    Edit: Oh, the main purpose of this post: IPB doesn't need eval, it's straight variables passed to the functions which are used.


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
  •