Last active
August 29, 2015 14:10
-
-
Save joelpittet/c5a20ddd1621e4d07e88 to your computer and use it in GitHub Desktop.
Twig Magic Attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Setup Twig. | |
require_once 'vendor/autoload.php'; | |
$loader = new Twig_Loader_String(); | |
$twig_options = array( | |
'cache' => FALSE, | |
'autoescape' => TRUE, | |
'strict_variables' => FALSE, | |
'debug' => TRUE, | |
'auto_reload' => TRUE, | |
); | |
$twig = new Twig_Environment($loader, $twig_options); | |
$twig->addExtension(new Twig_Extension_Debug()); | |
// Mock classes. | |
class Comment { | |
function getOwner() { | |
return new Owner; | |
} | |
} | |
class Owner { | |
function isAnonymous() { | |
return 'TRUE'; | |
} | |
} | |
// Variables passed into template. | |
$data = ['comment' => new Comment]; | |
// Preferred | |
print $twig->render('<br>{{ comment.owner.anonymous }} == TRUE', $data); | |
print $twig->render('<br>{{ comment.getOwner.isAnonymous }} == TRUE', $data); | |
print $twig->render('<br>{{ comment.getOwner().isAnonymous() }} == TRUE', $data); | |
print $twig->render('<br>{{ comment.owner.isAnonymous() }} == TRUE', $data); | |
print $twig->render('<br>{{ comment.getOwner().isAnonymxous() }} == FALSE', $data); | |
print $twig->render('<br>{{ comment.isAnonymous() }} == FALSE', $data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment