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

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);

Related

How to replace <?php echo with curly brace({{ }})

I want to cleanup my php file. I really like the way how blade template use curly brace to display php variables {{ $user }} https://laravel.com/docs/5.8/blade#displaying-data
Is there a way to use a functionality like that without using blade template. Using <?php echo $user ?> get's very repetitive.
This is not possible in PHP without using a template engine because it is a different syntax. Anyway in my opinion sometimes it is not the best idea to use a template engine because PHP is already kind of a template engine.
All what you seem to care about is the short syntax of echoing a variable in blade:
{{ user }}
This is as short as the syntax the PHP template engine itself provides:
<?= $user ?>
Of course Laravel (and similar) has a good reason to use template engine like blade - in order to seperate controller, logic and view (such that the view can be made by non-programmer designers for instance).
Update (thx to comments): The codes provided above do not exactly do the same as template engines (as an additional layer) may take care of e.g. escaping variables, which adds safety to the code.
As the OPs' code has already been built with echo and he is just searching for a replacement I did not mention this, but its a notable difference that you have to take care of some things yourself if not using one.

Access to a block view in a paragraph in Drupal

I created a paragraph in drupal which has a reference to a block.
In the preprocess I did :
$variables['bloc_video'] = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($bp);
And in my twig file I just called it like that :
{{bloc_video}}
The view is unfortunately not correct.
I want my preprocess to call my block view in another twig file. How can I do that?
Thanks a lot
I suggest using Twig Tweak module (it is very useful also for other cases). Here you can find more about including blocks in twig with Twig Tweak module.
In you case you can use:
{{ drupal_entity('block', 'block_content') }}
However, if you would still like to do it programmatically, check this answer.

Problems passing data to view using PRADO

I've been given a website to maintain. The website is using PRADO framework and I had to do some minor changes. There were no problems with the HTML and CSS changes, but now I have to pass some data from a page to a view.
I've tried using:
$this->HomeCalendar2->DataSource = $result['data'];
$this->HomeCalendar2->dataBind();
but it says Component property 'Home.HomeCalendar2' is not defined.
Above my code there is the following code:
$this->HomeCalendar->DataSource = $result;
$this->HomeCalendar->dataBind();
and it works perfectly fine and I can't see where is the definition of HomeCalendar.
Any help will be appreciated.
P.S: I've never worked with PRADO before.
This question is quite old now, hope you already solved it.
Using DataSource means that there must be in your template (.page or .tpl) a TDataBoundControl component like the folowwing (using TRepeater for example)
<com:TRepeater ID="HomeCalendar">
<!--- [ properties like ItemTemplate here and it contents ] --->
</com:TRepeater>
Subclasses of TDataBoundControl are for component needing to loop results, (sorts of for or foreach somehow).
If you juste need to pass a signle result to the view you can use a component like TPanel/TActivePanel, TLiteral, etc. or simply use the expression tag.
Examples :
Expression tag :
Php :
<?php
$this->myvalue = 'Hello World!';
Template :
<h1><%= $this->myvalue %></h1>
Or another solution:
Template :
<com:TLiteral ID="MyLiteral" />
PHP :
<?php
$this->MyLiteral->getControls()->add('<h1>Hello world !</h1>');

Symfony2 does not render php templates correctly

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

PHP code in FatFree template

I am trying to work with FatFree framework and trying to use the template engine. I render the template with the following code -
echo Template::serve('template.php');
The problem which I'm facing is that, inside the template.php file the F3 tags are recognised but any PHP code doesn't work. For instance, if I have the following code in the template.php file -
<?php
if (F3::get('var') == 'var1') {
?>
<span>var1 is present</span>
<?php
} else {
?>
<span>var1 not present</span>
<?php
}
?>
Here both var1 is present and var1 not present is printed irrespective of the value of var. Also, php for loops are not working - so basically all the php code is not working.
However, if I used <F3:check> to write the above PHP code, then everything works fine. Can we not use PHP code in templates. If this is the case, this is a serious limitation.
I have found the answer, although I don't really like it.
There is two different functions, F3::render() and Template::serve()
With F3::render() you can evaluate PHP expressions and use the F3::get() to retrieve variables. According to the website: "The only issue with embedding PHP code in your templates is the conscious effort needed to stick to MVC principles"
The Template::serve() is for templating only. Meaning its simply to process the templating language.
So basically, and yes it sucks and doesn't make sense, you can evaluate PHP code in the F3::render() and you can't use templating variables ({{#var}}) -OR- you can use Template::serve() and you are limited to only calling PHP functions, and not truly evaluating PHP code.
Maybe try to use different template engine which will allow you define easier the blocks variable dependency?
For example in PHPTal http://phptal.org/manual/en/split/tal-condition.html you can do it like that:
<div tal:condition="php: var == 'var1'">
....
</div>
It is undocumented but you can put code within {~ ~} in a template and it will be converted to <?php ?> when the template is compiled (using v3.6).
e.g. {~ #color = 'red' ~} will become <?php $color = 'red' ?>

Categories