Use PHP code in .TPL files in Kohana/Smarty Framework - php

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.

Related

PHP short way to echo string?

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

PHP own functions autocomplete from included files in Dreamweaver CS5

I'm using Dreamweaver CS5. I know that code hinting works in Dreamweaver with own functions/classes, but only if the functions file is included one level from main script.
Here an example:
main_script.php
include(my_functions.php)
this work and all functions which included in my_functions.php will hint when I'm editing the main_script.php (strg + space).
Other example:
main_script.php (own functions not available)
include(global_config.php) (own functions available)
include(my_functions.php) (own functions available)
Do you know if there is some trick or fix in other versions of Dreamweaver? If code hinting would work with several included levels that would make work much easier.
I've searched this site and several others but didn't find anybody with nearly the same problem, maybe somebody here can help.
Thankyou for the Editors you suggest me. I just tried out the PHPStorm and this Support codeHinting with several included files in a Project automaticly.
I just found out that Dreamweaver CS5 also Support this, in a bit other way here is a good tutorial Video for this:
http://tv.adobe.com/de/watch/lerne-dreamweaver-cs5/code-hinting-und-phphilfestellungen/
You can config a custome code hinting under "Site" => "SiteSpecific CodeHints"
Better then nothing ;)
Tom
Not sure if it works in dreamweaver but you could try adding a phpdoc eg:
include('global_config.php');
/* #var $functions Functions */
$functions = new Functions();
This assumes your functions are wrapped in a class

using the {php} tags in smarty outputs a blank page

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.

Working with CodeKit and haml/PHP

in my latests projects I have use Rails. Now I have to do something in PHP (which I actually hate, or love too much Ruby syntax).
So now I am very used to work with Sass and haml, which I also love. So I bought CodeKit for doing things outside rails framework.
Wanted just to know if there is any option for use haml in PHP files, or PHP in haml files, and that the file compiles with CodeKit, even having PHP.
I know this is a very late reply, just found this question when searching myself.
In addition to Kevin's reply above:
You can automatically convert the html to php by simply setting the output path for that particular file, and then specifying the extension.
Right click your HAML or HTML
"Set output path..."
"Output filename and extension"
Change it from index.html or index.haml to index.php.
You can use the :plain filter to preserve the php, it does not parse the filtered text. This is useful when you need to keep multiple lines of php.
ex:
:plain
<?php foreach ($es as $e) {
echo $e;
} ?>
I have not heard about any haml/php color syntax for Sublime Text 2 yet.
There is another setting that can be useful for working with php : check the don't escape HTML character in the HAML config on Codekit : http://d.pr/i/4pmv
This will manage with that
%form#booking_log{:name => "booking_log", :method => "post", :action => "<?=$this->action('bookingGetLog')?>"}

Smarty: how to include php in template?

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

Categories