Symfony2 does not render php templates correctly - php

I have a problem with rendering PHP templates instead Twig templates in Symfony2. The problem is Symfony for some reason does not interpret PHP code in template. Instead of interpreting PHP blocks of code, Symfony renders them simply as plain text. I try to follow to the documentation and render form to template like so.
When I write in controller
return $this->render('MyStudyBundle:Default:new.html.twig', array(
'form' => $form->createView(),
));
form loads as I expect. But when I change "new.html.twig" to "new.html.php" and try to render form in php template - as a result I have HTML page, where php code simply showed like plain text.
The question is why Symfony does not interpret blocks of php code in php templates?

You need to enable PHP templating engine.
In your app/config/config.yml:
framework:
templating: { engines: ['twig', 'php'] }
This might be helpful to you: How to use PHP instead of Twig for Templates.

You need to enable PHP template engine fron the app/config.yml file, and save your template as .php and not .twig

Related

Use PHP inside Handlebars template

I'm using NodeJS with Express and Handlebars for rendering my webpage. I am trying to use PHP on my webpage, is this possible with Handlebars?
When I try to use PHP in my Handlebars template file, but it seems like Handlebars can't render PHP? PHP is installed on the webserver and definitely works, but it seems inside my handlebars template, it doesn't.
When I try pasting this into my Handlebars template file: (Inside the body part)
<?php
echo "Test!";
?>`
Nothing is being displayed on the page.
When I try this:
<?php
echo "<p>Test!</p>";
?>
It displays as:
"Test!
"; ?> "
On my webpage.
Am I doing something wrong, or is it not possible to include php in my handlebars file? If so, how can I use php with handlebars?
No, this will not work.
Unless your template file has a php extension or your web server is set to render handlebars files through a php handler. Which I doubt.
You cannot mingle front-end and back-end code.

How to Secure PHP code in htm Pages and Partials?

I'm using OctoberCMS, based on Laravel and Twig, with Nginx and PHP7.0-FPM.
I make a Page or Partial using the CMS Backend Editor. Can edit the HTML Markup and Code.
The Page will render as localhost/mypage and hides the php source code.
But I'm able to go to localhost/themes/mysite/pages/mypage.htm in the browser and view the Twig markup and PHP comments in plain text.
And on some pages I can view all of the PHP and Laravel code like connection to database names and tables.
Anything that is in function onStart() in the Code editor, even though its wrapped in php tags in the htm file.
mypage.htm output:
However when viewing a .php file, it only shows the output and not the source.
I tried to change the page file extension to php instead of htm but get the error.
Invalid file extension: php. Allowed extensions are: htm.
Have you setup nginx correctly to blacklist those and other unpermitted files? http://octobercms.com/docs/setup/configuration#nginx-configuration
Alternatively, you can take a whitelist approach to security and utilize the october:mirror command: http://octobercms.com/docs/setup/configuration#public-folder

Smarty localization label - how to use in php?

Im using smarty localization to get language labels
{config_load file="localization.conf" section=$project->lang}
When I need some label from localization.conf I will put into smarty template file (tpl)
ex. {#label#} (where label in localization.conf = 'some text')
But now I need to use label in php file , How to do that ?
If you want to use config variables before output (assume html output) you can load config in php
$smarty->configLoad('test.conf', 'Login');
var_dump($smarty->getConfigVars());
$smarty->display('test.tpl');
Those config data loaded via PHP will be also visible in Smarty templates. More info: http://www.smarty.net/docs/en/api.get.config.vars.tpl

How can I use a Smarty template from a Smarty template?

I've run into a funny problem. I need to use Smarty templates within a Smarty template.
Here is why. I use the same templates for various wiki websites, and each website has its own configuration. The configuration contains parts for the main template (such as changed titles and headings, etc).
Here is a simplified example. I've a file topic-list.template.html that's shared across all websites:
<div id="topics">
<h1>{$h1}</h1>
...
</div>
As you can see, this template file contains an <h1> tag that can be customized for each website.
Then for each of the websites I've a configuration file that looks like this (simplified):
$config = [
"h1-titles" => [
"topics" => "Showing Topics in {\$category}"
]
];
As you can see the configuration file contains a Smarty template.
So when I render the topic-list.template.html file, I've to render the $config['h1-titles']['topics'] first through $smarty->fetch("string":$config['h1-titles']['topics']), and then assign it to h1 Smarty variable. My simplified code looks like this:
$h1_tag = $smarty->fetch("string":$config['h1-titles']['topics']);
$smarty->assign('h1', $h1_tag);
$smarty->display('topic-list.template.html');
I wonder if I could somehow insert the $config['h1-titles']['topics'] in the topic-list.template.html file automatically and then it have all rendered in one go?
Please have a look at the docs on String Template Resources. You will immediately notice that your $smarty->fetch('string:…') approach can also be done within a template: {include file="string:…"}
I believe that {eval} tag may help you:
{eval} is used to evaluate a variable as a template. This can be used for things like embedding template tags/variables into variables or tags/variables into config file variables.
http://www.smarty.net/docs/en/language.function.eval.tpl

Is there a template system like Twig that uses PHP syntax?

I would like to use a template system like Twig (specifically the block functionality) but in plain PHP.
For example, I found http://phpsavant.com/docs/ but it seems that doesn't support blocks or anything similar.
Edit
I found something that appears to have block syntax with regular PHP code: http://phpti.com/
The templating language you are looking for is called PHP!
There is one included in the Laravel framework called Blade.
You can mix plain old PHP with the Blade templating syntax, where {{...}} also translates to <?=...?> or <?php echo ... ?>
Also has blocks you know in Twig, but they are called sections.
#section('heading')
{{ strtoupper("I'm not shouting") }}
#show
<?= strtolower('Shhh!'); ?>
This is under the Illuminate\View namespace - See on GitHub, and can be downloaded with Composer as it is also registered on Packagist - just at the following to your project's composer.json.
{
"require": {
"illuminate/view": "4.*"
}
}
I'm not certain at this point about how you'd attempt to render a template from a custom project. If I find out, I'll update my answer.
Symfony (which uses Twig by default) also has a php-based template system that works very much like Twig. It has template inheritance and uses 'slots', which are equivalent to Twig's blocks. It's a stand-alone library that can be used outside of the full Symfony framework.
http://symfony.com/doc/2.0/cookbook/templating/PHP.html
PHP is a templating language(yes it is) , blocks can be implemented with buffers :
<?php
ob_start();
?>
this the content of the block for <?= date("Y-m-d") ?>
<?php
$content = ob_get_clean();
then in the main layout echo the content of the blocks :
echo $head;
echo $content ;
// ....
in fact that is what most template engines are using.
using template libs have huge advantages though ( caching , escaping by default ,etc ... )
As far as I understand use off Blade for views is optional in Laravel. You can use file name like view.php instead of view.blade.php and use plain old PHP syntax there. Only catch is you cannot have both view.php and view.blade.php as they both respond to
return View::make('view);

Categories