0

I have a smarty template file and I want to include code from a php file. It simply doesn't work. Here's the sfunc.php in templates folder:

function BeautySweet (){
    echo 'Script works fine.';
}
?>

Now here's the .tpl file code I added:

{include_php 'templates/sfunc.php';}
{ BeautySweet ();}

I also tried:

{include_php 'sfunc.php';}
{ BeautySweet ();}

Still no luck.

2
  • What Smarty version are you using? Commented Jun 16, 2016 at 16:12
  • I'm using Smarty 3.1.28 Commented Jun 16, 2016 at 16:15

2 Answers 2

3

Its

{include_php file="templates/sfunc.php"}

instead of

{include_php 'templates/sfunc.php';}

(Given that the path templates/sfunc.php is correct)

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, it works but there is an error Fatal error: Uncaught --> Smarty: {include_php} is deprecated, use SmartyBC class to enable
I tried digging deep into smarty documentation but I just can't find a way to make it work.
1

The use of php in smarty 3+ has been removed and for a good reason, templates are not supposed to have php code. You can use SmartyBC for backwards compatibility of old templates, but if you're creating a new project that's a really bad idea as you will probably have problems in the future. Create a plugin or do the required operations in your php file and pass the result as a variable instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.