How to add php in a tpl file ?

Discussion in 'PHP' started by jinch, Sep 29, 2012.

  1. #1
    Hello,

    I would like to display a random text in my header but I can't put PHP in my TPL file.
    I tried to put PHP by using {php} but it's not working, does anybody have a lead on how to put PHP in a TPL file ?

    Best regards,
     
    Solved! View solution.
    jinch, Sep 29, 2012 IP
  2. stuklen

    stuklen Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    put code in <?php {code} ?>
     
    stuklen, Sep 29, 2012 IP
  3. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #3
    You have to name the framework you're working with for anyone to help you, cuz .tpl file are parsed differently in different software packages.
     
    HostPlanz, Sep 29, 2012 IP
  4. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    Thanks for your answers, I believe it's smarty :)
     
    jinch, Sep 29, 2012 IP
  5. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Any idea anyone ? :)
     
    jinch, Sep 30, 2012 IP
  6. #6
    try {lietral}Your Code {/literal}
    or write it on a php page and call it in smarty
     
    MrPJH, Sep 30, 2012 IP
  7. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    i've tried {literal} but it's not working, how can I call a page from smarty ?
     
    jinch, Sep 30, 2012 IP
  8. MrPJH

    MrPJH Well-Known Member

    Messages:
    1,066
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    155
    #8
    i fall in same situation few months ago forgot how i fixed that email your both php and tpl files i will try it
     
    MrPJH, Sep 30, 2012 IP
  9. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    Thanks a lot for the proposition, but I managed to find a javascript code to do the random text and it's working with the {literate} tag ;)
     
    jinch, Sep 30, 2012 IP
  10. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #10
    It's been a long time since I've worked with smarty, sorry. :(
     
    HostPlanz, Oct 1, 2012 IP
  11. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #11
    Thanks for your answer anyway :)
     
    jinch, Oct 1, 2012 IP
  12. jinch

    jinch Greenhorn

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #12
    I've found the answer, create a file named block.php_code.php in your smarty plugin folder.
    With this code:

    function smarty_block_php_code($params, $content, &$smarty)
    {
        if (is_null($content))
        {
            return;
        }
        if ('<?php' == substr($content,0,5) && '?>' == substr($content, -2))
            $content = substr($content,5,-2);
        ob_start();
        eval($content);
        return ob_get_clean();
    }
    
    PHP:

    On your TPL you can use PHP like this :

    
    {php_code}{literal}<?php
    
        print "Hello, world!";
    
    ?>{/literal}{/php_code}
    
    Code (markup):
     
    jinch, Oct 1, 2012 IP
  13. Drent123

    Drent123 Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    No, i have not more detail for this.
     
    Drent123, Oct 3, 2012 IP
  14. webmasterpagerank

    webmasterpagerank Member

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #14
    you prefer is switch();
     
    webmasterpagerank, Oct 4, 2012 IP