Talesoft / tale-jade

A complete and fully-functional implementation of the Jade template language for PHP
http://jade.talesoft.codes
MIT License
88 stars 10 forks source link

Mixin generates duplicated php codes #109

Closed YamiOdymel closed 8 years ago

YamiOdymel commented 8 years ago

tale-jade installed via composer today,

and here's the jade file

mixin item(link, icon, label)
    a.item(href=$link)
        span.icon(class=$icon)
        $label

+item('photo', 'icon--picture', 'A')
+item('photo', 'icon--circles', 'B')
+item('photo', 'icon--music'  , 'C')
+item('photo', 'icon--draw'   , 'D')
+item('photo', 'icon--dna'    , 'E')
+item('photo', 'icon--gamepad', 'F')

output

2016-06-19 3 36 48

Is it normal? Because if I use more and more mixins, it will be a disaster ..

I tried it on the demo site (http://sandbox.jade.talesoft.io/), it generated a normal code.

2016-06-19 3 44 05
TorbenKoehn commented 8 years ago

Yep, that looks pretty messy.

I could put that $__ignore stuff in an own, global variable but then i'd pollute the global scope again :(

But maybe I can put that in some kind of freeze_scope compiler function, I'll think about it :)

TorbenKoehn commented 8 years ago

I just want to leave a note, I didn't forget this.

I looked into this and it's actually a real problem. I can decide if I want to make the ignored variables configurable and have it messy or have them static and have it clean.

I'm working on a solution, but it will take a while. It's not a bug, it's a cosmetic thing, so it's at the end of my to-do list :)

TorbenKoehn commented 8 years ago

I adressed this in the latest patch. Can you pull *@dev and check if it produces less bloat?

I think I even reduced it to less than it has ever been. There's still more possible, working on it :)

YamiOdymel commented 8 years ago

Wow, great work! It's much shorter now.

Tale Jade

mixin item(label)
    =$label

+item('A')
+item('B')
+item('C')
+item('D')
+item('E')
+item('F')

Outputted

f611a93 (Newest)

Size: 1,566 byte

2016-08-23 11 06 36
<?php $__mixins = [];?><?php $__mixins['item'] = function(array $__arguments, array $__scope) {$__defaults = ['label' => null];extract(array_replace($__scope, array_replace($__defaults, $__arguments)));?><?=htmlentities(isset($label) ? $label : '', \ENT_QUOTES, 'UTF-8')?><?php };?><?php $__scope = \Tale\Jade\Compiler\create_scope(get_defined_vars()); $__mixinCallArgs = ['label' => 'A'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope); unset($__scope); unset($__mixinCallArgs); ?><?php $__scope = \Tale\Jade\Compiler\create_scope(get_defined_vars()); $__mixinCallArgs = ['label' => 'B'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope); unset($__scope); unset($__mixinCallArgs); ?><?php $__scope = \Tale\Jade\Compiler\create_scope(get_defined_vars()); $__mixinCallArgs = ['label' => 'C'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope); unset($__scope); unset($__mixinCallArgs); ?><?php $__scope = \Tale\Jade\Compiler\create_scope(get_defined_vars()); $__mixinCallArgs = ['label' => 'D'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope); unset($__scope); unset($__mixinCallArgs); ?><?php $__scope = \Tale\Jade\Compiler\create_scope(get_defined_vars()); $__mixinCallArgs = ['label' => 'E'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope); unset($__scope); unset($__mixinCallArgs); ?><?php $__scope = \Tale\Jade\Compiler\create_scope(get_defined_vars()); $__mixinCallArgs = ['label' => 'F'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope); unset($__scope); unset($__mixinCallArgs); ?>

4134b0a

Size: 3,828 byte

2016-08-23 11 06 47
<?php $__mixins = [];?><?php $__mixins['item'] = function(array $__arguments, array $__scope) {$__defaults = ['label' => null];extract(array_replace($__scope, array_replace($__defaults, $__arguments)));?><?=htmlentities(isset($label) ? $label : '', \ENT_QUOTES, 'UTF-8')?><?php };?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore);   $__mixinCallArgs = ['label' => 'A'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore);   $__mixinCallArgs = ['label' => 'B'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore);   $__mixinCallArgs = ['label' => 'C'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore);   $__mixinCallArgs = ['label' => 'D'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore);   $__mixinCallArgs = ['label' => 'E'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore);   $__mixinCallArgs = ['label' => 'F'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?>
TorbenKoehn commented 8 years ago

Awesome!

Thank you for testing :)