-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ternary operator shorthand #134
Comments
I don't know if overloading the |
I can't really see how it would cause confusion. I've always read the ternary operator as: Either way, the '?=' syntax isn't bad either and is still cleaner than the current method. |
Would you be looking for the default filter? eg: |
Like eliot said, the default filter is probably the best answer. |
Nah this has nothing to do with the default filter. I think this would be a good change for brevity's sake (which adds readability in cluttered templates), even supporting the |
So was it implemented? |
Still wouldn't mind seeing this implemented either. As @sedaek said, would be a step towards making complicated templates more readable. |
I think this is more in line with current twig syntax
with else
|
@char101 I kinda like this proposal as it reminds me of my good old Perl days ;) |
@fabpot It's also the syntax used by python. Since twig seems to take inspiration from Jinja, I think it's only natural to add the if expression syntax from python. |
Anyone willing to see if the implementation would be easy or hard? I suspect that it won't be that easy. |
There is implementation of "if()" filter in Twig-Extensions: twigphp/Twig-extensions#43 I use this filter in my projects. |
This is an example implementation protected function parseConditionalExpression($expr)
{
$ternary = false;
while ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '?')) {
$this->parser->getStream()->next();
$expr2 = $this->parseExpression();
$this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'The ternary operator must have a default value');
$expr3 = $this->parseExpression();
$expr = new Twig_Node_Expression_Conditional($expr, $expr2, $expr3, $this->parser->getCurrentToken()->getLine());
$ternary = true;
}
if (! $ternary) {
while ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'if')) {
$this->parser->getStream()->next();
$condition_expr = $this->parseExpression();
if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'else')) {
$this->parser->getStream()->next();
$else_expr = $this->parseExpression();
} else {
$else_expr = new Twig_Node_Expression_Constant('', $this->parser->getCurrentToken()->getLine());
}
$expr = new Twig_Node_Expression_Conditional($condition_expr, $expr, $else_expr, $this->parser->getCurrentToken()->getLine());
}
}
return $expr;
} test case <?php
require_once 'Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_String;
$twig = new Twig_Environment($loader);
echo $twig->render('{{ "Hello" if false else "World" }}'); |
Some test cases
|
I tend to find myself doing a lot of things like this:
{{ foo ? 'yes' : '' }}
This happens quite often when defining CSS class names. Would it be possible to shorten the code in these instances to:
{{ foo ? 'yes' }}
The text was updated successfully, but these errors were encountered: