2017-04-03 2 views
3

Ich möchte benutzerdefinierte Zweig-Tag, ähnlich Tag enthalten, aber nehmen Sie Variablen aus JSON-Datei. I.e. In Zweig Vorlage schreibe ich {% Abschnitt "Header"%}, und es enthält Header-Datei und anfügen von Variablen aus der Datei config.json nur diese Vorlage. Wie erreiche ich das?Wie fügt man Twigs TokenParser Variablen hinzu?

ich How to create a twig custom tag that executes a callback? paar Male, bevor schreibe diese Frage lesen, aber keine konkrete Lösung finden, wie

+0

Mögliches Duplikat von [Wie einen Zweig benutzerdefinierten Tag erstellen, die einen Rückruf ausführt?] (Http://stackoverflow.com/questions/26170727/how-to- create-a-twig-custom-tag-das-führt-ein-callback) – DarkBee

+0

Nein, es ist nicht das gleiche – Julius

+0

Es gute Tutorial, um Ihren Knoten zu starten. Um Argumente an Ihren Knoten weiterzugeben, schlage ich vor, dass Sie in den [Block] (https://searchcode.com/codesearch/view/46764421/) Knoten schauen, '$ name = $ stream-> expect (Twig_Token :: NAME_TYPE) - > getValue(); ' – DarkBee

Antwort

1

Okay, mein Problem zu lösen, ich habe ein kleines Mockup erstellt, die Ihnen weiter auf diesem Weg helfen sollen,

MyNode.php

<?php 
    namespace Namespace\Base\Twig\Node; 

    class MyNode extends \Twig_Node { 
     private static $nodeCount = 1; 
     /** 
     * @param \Twig_Node_Expression $annotation 
     * @param \Twig_Node_Expression $keyInfo 
     * @param \Twig_NodeInterface $body 
     * @param integer    $lineno 
     * @param string    $tag 
     */ 
     public function __construct(\Twig_NodeInterface $body, $lineno, $tag = null) { 
      parent::__construct(['body' => $body,], array(), $lineno, $tag); 
     } 

     public function compile(\Twig_Compiler $compiler) { 
      $i = self::$nodeCount ++; 

      $json_data = json_decode(file_get_contents(__DIR__ . '/../../../../files/tmp/file.json'), true); 
      $compiler 
       ->addDebugInfo($this) 
       ->write('$context[\'injected_variable\'] = '.var_export($json_data, true).';') //add data to context 
       ->subcompile($this->getNode('body')) //compile everything in between the node 
       ->write('unset($context[\'injected_variable\']);'); //clean context afterwards 
     } 
    } 

file.json

{ 
    "glossary": { 
     "title": "example glossary", 
     "GlossDiv": { 
      "title": "S", 
      "GlossList": { 
       "GlossEntry": { 
        "ID": "SGML", 
        "SortAs": "SGML", 
        "GlossTerm": "Standard Generalized Markup Language", 
        "Acronym": "SGML", 
        "Abbrev": "ISO 8879:1986", 
        "GlossDef": { 
         "para": "A meta-markup language, used to create markup languages such as DocBook.", 
         "GlossSeeAlso": ["GML", "XML"] 
        }, 
        "GlossSee": "markup" 
       } 
      } 
     } 
    } 
} 

template.twig

<!DOCTYPE html> 
<html> 
    <head></head> 
    <body> 
     {% mynode%} 
      {{ injected_variable.glossary.title }} {# prints example glossary #} 
     {% endmynode %} 
    </body> 
</html> 
+0

Vielen Dank! – Julius