I've tried {include_php file="phpfile.php"} and {php} tags but both cause deprecated error. Can you not do this in Smarty anymore? I can't find anything in the docs.
I circumvented this problem. Create a plugin file named block.php_code.php with this function in it:
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();
}
In your template, you can then write:
{php_code}{literal}<?php
print "Hello, world!";
?>{/literal}{/php_code}
They are depreciated for a reason as they allow poor practices. Smarty recommends putting the included script into the PHP logic or creating a plugin (which is simple).
{php} tags are deprecated from Smarty, and should not be used. Put your PHP logic in PHP scripts or plugin functions instead.
Source
{include_php} is deprecated from Smarty, use registered plugins to properly insulate presentation from the application code.
Source
If you include what you are trying to do in your phpfile.php, we can help you write a plugin function.
{include_php} is marked as deprecated in both Smarty2 and 3; {php} in Smarty3 only:
http://www.smarty.net/docsv2/en/language.function.include.php.tpl
http://www.smarty.net/docs/en/language.function.include.php.tpl
http://www.smarty.net/docs/en/language.function.php.tpl
Related
I'm trying to create a basic short code in Wordpress that will allow me to run PHP on pages. This is what I have so far, but it isn't working. Advice?
The idea is it will be [php] Insert PHP here [/php']
<?php
function php_shortcode( $attr, $content = null ) {
return '<?php' . $content . '?>';
}
add_shortcode('php', 'php_shortcode');
?>
Thank you.
Sorry to say you are exploring a concept that simply cannot yield success. The PHP that renders the shortcode, cannot "also" render code within itself.
Short codes as standard will filter PHP tags. You can however write php directly into the content editor. Without giving you all the advisories why it's not recommended, you can do something like the following which will allow you to write php into the content editor:
// write '<?php ... ?>' into the editor
add_filter('the_content', 'allow_php', 9);
function allow_php($content) {
if (strpos($content, '<' . '?') !== false) {
ob_start();
eval('?' . '>' . $content);
$content = ob_get_clean();
}
return $content;
}
After thinking about this for a little while I realized there is an obvious solution. So, someone probably has written a plugin to do it and someone has - https://wordpress.org/plugins/inline-php/.
It consists of about 40 lines of PHP. The critical implementation trick is it is not done as a shortcode but as a 'the_content' filter.
add_filter('the_content', 'inline_php', 0);
This is done before other 'the_content' filter processing and avoids all the problems that I encountered trying to use it as a shortcode. Of course, there is still a significant security risk.
In PHP we echo strings this way :
echo 'string';
But i saw PHP frameworks like Laravel and scripts echo strings using Curly Brackets :
{string}
How i can do that without using any PHP framework?
It's not necessary to use Curly Brackets if there is other way to short echo!
I prefer code examples.
PHP has a few methods to print strings, such as (but not limited to) print, and echo or just shorthand <?= "str" ?>.
The bracket print that you ask about from laravel is not per say in php.
That is from a template engine called Blade.
So the {} way of printing stuff is not possible in php.
You will have to stick to the standard ways or use a template engine!
You can short echo by
<?= $variable; ?> when you open a new php tag
or you use, as described in your question a framework such as laravel - that's pretty much it
What you can do, but I'm not really sure if it's a good idea - write a function like:
function x($string) {
echo $string;
}
x('Test'); // will output Test
The short answer is you can't do that with PHP. PHP provides language constructs to output to standard output, etc. PHP doesn't provide a pre-processor. Most template frameworks are a pre-processor meaning that they convert your {<STRING>} into an echo $x statement.
So either create your own template framework, or stick to PHP's API.
After researching i found a Template Engine called Smarty that allows me to echo using Curly Brackets
First i created 2 files index.php , template.html
Then i moved the folder /libs that i downloaded from the template page to my script directory
and in my index.php i required the Smarty.class file
require_once "libs/Smarty.class.php";
then i called the Smarty class :
$smarty = new Smarty;
after that i pushed the variable :
$smarty ->assign("name",'Amr');
and i displayed the template file index.html
$smarty ->display("template.html");
at the end i wrote in the index.html <p>{$name}</p> and it outputted : Amr
I am unable to write PHP code in .tpl file in either ways
I tried <?php echo 'test'; ?>
I also tried {PHP} echo 'test'; {/PHP}
But both returned error
on line 14 "{php}echo "hello!"{/php}" unknown tag "php"
I've not worked with Kohana, but with Smarty 3, use of the php tag is deprecated. You'll need to use the backwards compatibility mode with 3.0. It is highly recommended that all code logic be placed in your controller or php script files, and not your Smarty templates.
If you want to use PHP code in templates, it is suggested you create custom functions or modifiers.
So, basically, the short answer is find where your code (or the Kohana plugin) is instantiating Smarty and change it to use SmartyBC, but be aware that this is highly discouraged.
If you are using Kohana Smarty3 module for Kohana, these code lines should help set you in the right direction.
I have no experience in Smarty, Im trying to modify a .tpl file from a software not made by made, I am just added the {php} {/php} tags in a .tpl file with no code inside them at all, and its outputs seems a blankpage. First I had some code but it was outputting a blank page so I started to remove lines and now there are no lines at all inside the {php} {/php} tags yet it does that behaviour. What am I doing wrong here?
Which version of Smarty are you using? The PHP tag was deprecated a while back, and as of 3.1. can only be used with the backwards compatibility wrapper.
http://www.smarty.net/docs/en/language.function.php.tpl
It's best if possible to keep your php in a separate class or controller (that way it's accessible to other templates as well), but if you really need to include it on the page, just include the smartybc class, as it says here: http://www.smarty.net/docs/en/bc.tpl
<?php
// instead of
require_once('path/to/smarty/libs/Smarty.class.php');
$smarty = new Smarty();
// use
require_once('path/to/smarty/libs/SmartyBC.class.php');
$smarty = new SmartyBC();
?>
Also, if you'd like to see PHP errors (it's really annoying that you just get a blank page when something goes wrong, makes it hard to debug) you can activate debug mode by going to config/config.inc.php and finding the following lines and changing 'off' to 'on' for the first one and set to true the second
/* Debug only */
#ini_set('display_errors', 'on');
define('_PS_DEBUG_SQL_', true);
And that should display PHP and SQL errors for you.
You should not be using {php} tags, as they are described as deprecated in Smarty docs.
If you want some code to be ran, try preparing plugins for smarty as described here.
In expressionengine with php parse enabled,
if i do the following, it works and i get the username displayed. logged in user is admin. So it echos out admin.
<?php
$x = '{username}';
echo $x;
?>
However if i do the following and use the{username} tag insde mkdir() function, then it doesn't work. The directory created will have the name {username} instead of admin. Why is this happening.
<?php
$x = '{username}';
mkdir($x);
?>
I'd suggest writing a quick plugin that accepts the logged-in username as a parameter, then does your mkdir() work within the plugin.
class Make_directory
{
var return_data = '';
function __construct()
{
$this->EE =& get_instance();
$username = $this->EE->TMPL->fetch_param('username', FALSE);
if($username != FALSE)
{
$dir = mkdir(escapeshellarg($username));
}
$this->return_data = $dir;
}
There's more to the plugin, but that's the guts of it. Then call it like {exp:make_directory username="{logged_in_username}"}.
Expression engine is a templating engine. It almost certainly buffers output then replaces it, which is why this will work with echo but not functions.
I'm not an expert in EE, but something like this might work:
$name = get_instance()->TMPL->fetch_param('username', '');
mkdir(escapeshellarg($name));
The point is you need to get the return of EE interpreting that, rather than just passing the raw text.
You can also use ob_start() to capture the output if you can't easily get EE's return. For example:
function mkdir_obcb($dir) {
mkdir(escapeshellarg($dir));
return '';
}
ob_start('mkdir_obcb');
echo '{username}';
ob_end_clean();
Note also my use of escapeshellarg() to reduce the risk of attack.
Is it possible you have it set up so your PHP is being parsed before the EE tags? Not only do you need to set to allow php parsing but what order it happens in as well.
http://expressionengine.com/user_guide/templates/php_templates.html
You may need to set 'PHP Parsing Stage' to 'output' in the preferences of your template in the CP Template Manager, because then PHP executes after expression engine rendered the ee tags.