Level-2 / Transphporm

Transformation Style Sheets - Revolutionising PHP templating
278 stars 27 forks source link

[Feature Request] More :data[condition] selectors? #197

Open cxj opened 6 years ago

cxj commented 6 years ago

Would it be possible and desirable to add a few more conditional operators to the :data[condition] selectors, possibly modeling the syntax on other CSS attribute selectors? For example, I would find the following operations very helpful (chosen syntax is completely arbitrary and repurposed from CSS):

TRPB commented 6 years ago

You can use div:data["x" in myArray] which does in_array('x', $myArray) or div:data[myArray!=""] {display: none;} to determine whether and array is empty.

cxj commented 6 years ago

Is there some way to check for a key in the array? in_array() works on values only.

TRPB commented 6 years ago

div:data[myArray.key1] should work, although I haven't tested it.

edit: Though that will be the same as !empty($array['key1']) rather than array_key_exists.

cxj commented 6 years ago

This check for an empty array doesn't seem to work:

<?php
require "vendor/autoload.php";

$xml = '<div>This is the foo message.</div>';
$tss = 'div:data[foo!=""] { content: "condition true"; }';

$tpl = new Transphporm\Builder($xml, $tss);

$data['foo'] = [];

echo $tpl->output($data)->body . PHP_EOL;

I expected the output to be <div>This is the foo message.</div> because array foo is empty, i.e. foo="". But my output is <div>condition true</div>.

garrettw commented 6 years ago

But it is true that empty array !== empty string.

TRPB commented 6 years ago

Transphporm uses != internally instead of !==