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.
  2. Better Analytics for WordPress Get It Free

Include PHP Variable Using Smarty

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

  1. #1
    I have a PHP site that is backed up by another server, so I have created a file called servername.php, and here are the contents:

    <?php
    //Host 1
    $servername = "My site name- Server 1";
    
    //Host 2
    //$servername = "My site name- Server 2";
    ?>
    PHP:
    In all my site pages, I simply add the following:

    include("servername.php");
    PHP:
    Then in the title tag, I insert

    <?php echo $servername; ?>
    PHP:
    That way, if my preferred host goes down, I can update the domain's nameservers and update the $servername in servername.php.

    It works great!

    The only problem is that Im not good with smarty, and I have a script installed on my site that uses smarty .tpl files.

    I need to customize header.tpl to be able to include the above code, so that it displays which server the site is running from.

    Can someone help me to utilize some sort of PHP include/echo function to do this in my tpl file?

    HUGE THANKS! +rep
     
    JWRmedia, Nov 6, 2008 IP
  2. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Hi, can you post your smarty code here, or send it to me as a PM?
     
    elias_sorensen, Nov 6, 2008 IP
  3. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    in your php file use:

    
    $smarty->assign("servername", $servername);
    
    PHP:
    then, in your .tpl file you can access it with:

    
    {$servername}
    
    Code (markup):
     
    dev_SeeInside, Nov 7, 2008 IP
  4. JWRmedia

    JWRmedia Banned

    Messages:
    499
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    First, thanks for your help.

    *****

    The file is a .tpl extension, but all the code inside is HTML.
    Its the header file for a script that utilizes Smarty.

    This is my php file I use throughout my site:

    First, I include:

    include("servername.php");
    Code (markup):
    Then, in the <title> tags I include:

    <?php echo $servername; ?>
    Code (markup):
    This is the content of the actual PHP file:

    <?php
    //Server1
    $servername = "Server 1";
    
    //Server 2
    //$servername = "Server 2";
    ?>
    
    PHP:
    Most of my site is in PHP, but I do utilize this script which runs off Smarty syntax.

    If my primary web host goes down, I can just update my nameservers and update this PHP file to display the secondary servername in the <title> tags of my pages, just to remind me to recheck to see if my primary server is back up.

    *****

    I just need to add the ability to automatically update the <title> using this PHP file within my smarty .tpl file.

    Hopefully, this makes sense. :)
     
    JWRmedia, Nov 7, 2008 IP
  5. JWRmedia

    JWRmedia Banned

    Messages:
    499
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How would the .tpl file know where the $servername variable is set?
     
    JWRmedia, Nov 7, 2008 IP
  6. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    How do you display your tpl files on the website?

    You do also have to remember, that even if you change your nameservers if your host goes down, it will take up to 24-hours to update for all dns servers all around the world, so some users might experience downtime in 24 hours.
     
    elias_sorensen, Nov 7, 2008 IP
  7. JWRmedia

    JWRmedia Banned

    Messages:
    499
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah, I know but I use .com and .net, so if the server for .com goes down, I login to godaddy and do a 301 redirect to .net until the server comes back up.

    I havnt experienced this yet, but Im just taking precautions.

    Anyway, the header.tpl file is from whmcs. I just want it to echo the contents of a php file for the <title>.
     
    JWRmedia, Nov 7, 2008 IP
  8. elias_sorensen

    elias_sorensen Active Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #8
    To be honest, I don't have the biggest experience with Smarty.

    You might try to use ob_start to change your output dynamicly.
    
    <?php
    
    function changeTitle($buffer)
    {
      return (str_replace("<title>Your current title (title that smarty writes)</title>", "<title>".$servername."</title>", $buffer));
    }
    
    ob_start("changeTitle");
    
    ?>
    
    PHP:
     
    elias_sorensen, Nov 7, 2008 IP
  9. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Sorry, I was assuming a standard smarty setup.. I don't know anything about WHMCS

    
    include('Smarty.class.php');
    include("servername.php");
    
    $smarty = new Smarty;
    
    // add all your usual PHP code here to do whatever you want to do
    
    $smarty->assign('servername', $servername);
    
    $smarty->display('templatefile.tpl');
    
    PHP:
    then in your template file you can do:

    
    <title>{$servername}</title>
    
    Code (markup):
     
    dev_SeeInside, Nov 7, 2008 IP