alexeyrybak / blitz

Blitz templates, template engine extension for PHP
Other
92 stars 42 forks source link

Problem with blocks in template that was included #69

Closed AlexanderDeev closed 3 years ago

AlexanderDeev commented 3 years ago

Hello! Early this construction worked in PHP5, but now when I tried to use it in PHP7 (blitz 0.10.5) or PHP8 (blitz 0.10.5) I had a problem. I can't iterate block in template that was included by Template API method. What am I doing incorrectly?

test_blitz.php:

$t = new \Blitz();
$t->load('Main template: <!--include("./test_blitz.html"); -->');
$t->block('block1');
$t->display();

test_blitz.html:


Second template
- <!-- BEGIN block1 -->block1<!-- END block1 -->

console:

# php -f ./test_blitz.php
Main template:
Second template
-
alexeyrybak commented 3 years ago

I confirm that something wierd happens, but just adding this here as I managed to show how this works (which doesn't mean there's no bug - just a step forward to either find a workaround or fix the issue). Please have a look.

fisher@fisher deev-18Feb2021 % cat test.php
<?php

$t = new Blitz();
$t->load('Main template: {{ include("test.html"); }}');
$t->display(array('block1' => array('you' => 'u')));

?>
fisher@fisher deev-18Feb2021 %
fisher@fisher deev-18Feb2021 %
fisher@fisher deev-18Feb2021 % cat test.html
Second template: {{ BEGIN block1 }} hey, {{ $you }}, get off my cloud {{ END }}
fisher@fisher deev-18Feb2021 % php test.php
Main template: Second template:  hey, u, get off my cloud

fisher@fisher deev-18Feb2021 % php -v
PHP 7.4.3 (cli) (built: Mar  3 2020 15:11:25) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
NickyX3 commented 3 years ago

Confirmed on php7.2 (blitz 0.10.5), but. It seems to me that without block1 data it just skips First - before include need a space symbol Second thing - use display() with one big array IMHO is better (oh, Alexey write this ) Third thing - using alternative tags bad idea %-)

alexeyrybak commented 3 years ago

@AlexanderDeev I think I know the answer "Note that we used an additional 3rd parameter in block method. This parameter tells blitz to iterate /test1 and /test2 blocks without any check if corresponding context exists. Include is processed at the execution stage, that's why Blitz knows nothing about included templates structure when setting varibles or iterating contexts". http://alexeyrybak.com/blitz/blitz_en.html, 8.7

try this: $t->block('block1', $params, TRUE);

Please check this out and close if that's the issue

NickyX3 commented 3 years ago

@alexeyrybak @AlexanderDeev cat ./test.php

<?php
ini_set('blitz.path',"/home/nicky/");
$t = new \Blitz();
$t->load('Main template: {{ include("test_blitz.html") }}');
$t->block('block1',$data,true);
$t->display();
?>

cat ./test_blitz.html

Second template
– {{ BEGIN block1 }}block1{{ END block1 }}

./test.php

Main template: Second template
– block1

All works

AlexanderDeev commented 3 years ago

Alex and Nick, really thanks for your answers! I remade my code and now it really works

$t = new \Blitz();
$t->load('Main template: <!-- include("./test_blitz.html"); -->');
$t->block('block1', [], true);
print $t->parse();

console:

# php -f ./test_blitz.php
Main template:
Second template
- block1
NickyX3 commented 3 years ago

@AlexanderDeev Using a block without data seems like a strange task. In my opinion, using the flag and IF seems more logical

$t = new \Blitz();
$t->load('Main template: <!-- include("./test_blitz.html"); -->');
print $t->parse(['block1'=>true]);
Second template
– {{ IF $block1 }}block1{{ END if-block1 }}
AlexanderDeev commented 3 years ago

@NickyX3 In the real case I need use block method. I have multilevel structure in my templates and I need iterate different blocks in different methods in my responder class. Yes, I can prepare some result array in this methods and use it as parameter of parse method, but I am used to do it with block method :)