1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to echo PHP variable in a .tpl file?

Discussion in 'PHP' started by JWRmedia, Nov 7, 2008.

  1. #1
    Let's say I've got a php file called servername.php.

    The contents would look like this:
    SEMrush
    <?php
    
    
    $servername= "My website";
    
    
    ?>
    PHP:
    On all my PHP pages, I insert this in the <title>

    <?php echo $servername; ?>

    My problem is that I have a script running .tpl files and I want to insert this in the <title> section of the header.tpl file. Of course, it wont work like that.

    How do I get it to echo this PHP variable in a .tpl file???????
     
    JWRmedia, Nov 7, 2008 IP
    SEMrush
  2. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you need to assign the variable to the tpl file using the smarty object

    ex: in your source file.

    $smartyObject->assign('yourVar', $whateverVar);

    and then you can output it to your .tpl file llike {$yourVar}
     
    shineDarkly, Nov 7, 2008 IP
  3. JWRmedia

    JWRmedia Banned

    Messages:
    499
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, I have done this, but how do I include the source file in the tpl file so that it knows where to pull the variable from?

    Im trying this but its not working:

    
    {include_php file='http://www.mydomain.com/servername.php'}
    
    Code (markup):
     
    JWRmedia, Nov 7, 2008 IP
  4. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you're trying to edit the .tpl file when you have to edit the PHP file that calls the .tpl file through the smarty object. Find that file, then use the code above to assign your servername variable, which you can then call from within the .tpl file. Including the servername.php file from within the .tpl file won't work.
     
    dev_SeeInside, Nov 7, 2008 IP
  5. JWRmedia

    JWRmedia Banned

    Messages:
    499
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So if I put something like

    {$yourVar}

    Inside a .tpl file, and I assign that variable in a PHP file, how does it know where to locate that variable from out of all the PHP files on my site?
     
    JWRmedia, Nov 7, 2008 IP
  6. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    try and look for the file that loads the tpl file, you can probably include the file there and then assign it using the method above, in case you need professional help, then please drop me message :)
     
    shineDarkly, Nov 7, 2008 IP
  7. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    you would have to load the .tpl file through the PHP file, using something like

    $smarty->dislay("templatefile.tpl");

    So the template file is loaded by the PHP, you don't access the .tpl file directly. You have to find the PHP file that loads your template, and assign the smarty variable through the PHP.
     
    dev_SeeInside, Nov 7, 2008 IP