Skip to content

Loading…

Added Eval extension #53

Closed
wants to merge 8 commits into from

4 participants

@triplejumper12

I made an extension that adds an eval filter that can be called on twig code passed into a template to evaluate the string as code.

ie. twig->render(..path.., array('pattern' => '{{ key }}-{{ value }}', 'information' => array('somekey' => 'somevalue')))

In the template at ..path..: {% for key,value in information %}{{ pattern|eval }}{% endfor %} will evaluate to return "somekey-somevalue".

Billy Visto added some commits
@fabpot

It should probably be a function instead of a filter.

@Seldaek

Good thing, now we just need goto :)

@stof stof commented on the diff
lib/Twig/Extensions/Extension/Eval.php
((37 lines not shown))
+ * @param string $string The string template to load
+ * @return string
+ */
+ public function evaluateString(Twig_Environment $env, $context, $string)
+ {
+ $newEnv = $this->setLoader($env);
+ return $newEnv->loadTemplate($string)->render($context);
+ }
+
+ /**
+ * Clones the current environment and sets the loader to be a string loader
+ *
+ * @param Twig_Environment $env
+ * @return Twig_Environment
+ */
+ private function setLoader(Twig_Environment $env)
@stof
stof added a note

The name is weird for a method cloning the environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Billy Visto and others added some commits
Billy Visto Moved cloning of the environment out of setLoader
The cloned environment is passed into setLoader that way setLoader only sets the loader.
This also cuts down on a variable being assigned.
60d7c6e
Billy Visto Added comment explaining why setLoader is called on a cloned environm…
…ent for code readability
f7a734f
@fabpot fabpot added missing LICENSE file (closes #56) feb6d3f
@triplejumper12 triplejumper12 Merge branch 'master' of git://github.com/triplejumper12/Twig-extensions e81550c
@triplejumper12

What is the status of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 24, 2012
  1. added extension that adds the eval filter to be used to evaluate twig…

    Billy Visto committed
    … code that gets passed into a template
  2. removed leading \ from class names

    Billy Visto committed
  3. changed the eval filter to be a function instead

    Billy Visto committed
  4. removed leading \ from class name

    Billy Visto committed
Commits on Apr 25, 2012
  1. Moved cloning of the environment out of setLoader

    Billy Visto committed
    The cloned environment is passed into setLoader that way setLoader only sets the loader.
    This also cuts down on a variable being assigned.
  2. Added comment explaining why setLoader is called on a cloned environm…

    Billy Visto committed
    …ent for code readability
Commits on May 21, 2012
  1. @fabpot
Commits on May 23, 2012
  1. @triplejumper12
Showing with 78 additions and 0 deletions.
  1. +19 −0 LICENSE
  2. +59 −0 lib/Twig/Extensions/Extension/Eval.php
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2010-2012 Fabien Potencier
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
59 lib/Twig/Extensions/Extension/Eval.php
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Twig Extension to add the eval function to evaluate twig code passed into a template
+ */
+class Twig_Extensions_Extension_Eval extends Twig_Extension
+{
+ /**
+ * Returns the name of the extension.
+ *
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return 'eval';
+ }
+
+ /**
+ * Returns a list of functions to add to the existing list.
+ *
+ * @return array An array of functions
+ */
+ public function getFunctions()
+ {
+ return array(
+ 'eval' => new Twig_Function_Method($this, 'evaluateString', array(
+ 'needs_environment' => true,
+ 'needs_context' => true,
+ )),
+ );
+ }
+
+ /**
+ * Loads a string template and returns the rendered version
+ *
+ * @param Twig_Environment $env
+ * @param array $context
+ * @param string $string The string template to load
+ * @return string
+ */
+ public function evaluateString(Twig_Environment $env, $context, $string)
+ {
+ // set loader to be a string loader on a cloned environment so we aren't messing with the actual environment
+ $newEnv = $this->setLoader(clone $env);
+ return $newEnv->loadTemplate($string)->render($context);
+ }
+
+ /**
+ * Sets the environment's loader to be a string loader
+ *
+ * @param Twig_Environment $env
+ * @return Twig_Environment
+ */
+ private function setLoader(Twig_Environment $env)
@stof
stof added a note

The name is weird for a method cloning the environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ $loader = new Twig_Loader_String();
+ $env->setLoader($loader);
+ return $env;
+ }
+}
Something went wrong with that request. Please try again.