Skip to content

Instantly share code, notes, and snippets.

@joelpittet
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelpittet/c5a20ddd1621e4d07e88 to your computer and use it in GitHub Desktop.
Save joelpittet/c5a20ddd1621e4d07e88 to your computer and use it in GitHub Desktop.
Twig Magic Attributes
<?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