src/Twig/TerrificCompiler.php
/**
   * Checks if given variable exists, and adds it to the context.
   *
   * So it it's data is available in the compiled component.
   *
   * @param \Namics\Terrific\Twig\Data\VariableNameAndArrayKeysPair $variableNameAndArrayKeysPair
   *   Object containing the variable name and optionally array keys.
   * @param string|null $variableDoesNotExistErrorMessage
   *   Custom error message that is used as exception message
   *   when the given variable does not exist.
   */
Checks if given variable exists, and adds it to the context.
So it it's data is available in the compiled component.


Checks if given variable exists and adds it to the context . So it it ' s data is available in the compiled component .
public function compileAndMergeVariableToContext(VariableNameAndArrayKeysPair $variableNameAndArrayKeysPair, ?string $variableDoesNotExistErrorMessage = NULL): void {
    $twigCompiler = $this->getTwigCompiler();
    $contextArrayKeys = $variableNameAndArrayKeysPair->toTwigContextArrayKeysString();
    if ($variableDoesNotExistErrorMessage === NULL) {
      $variableDoesNotExistErrorMessage = addslashes('The variable ' . $variableNameAndArrayKeysPair->toTwigVariableString() . ' does not exist. Could not compile it to the Twig context.');
    }
    $twigCompiler
      ->raw("\n")->write('')
      ->raw('if (')
      ->raw('isset(');
    $this->compileAsContextVariable($variableNameAndArrayKeysPair, '$context');
    $twigCompiler
      ->raw(')')
      ->raw(') {')
      ->raw("\n")->write('')->write('')
      ->raw(ContextProviderInterface::TERRIFIC_CONTEXT_VARIABLE . ' = array_merge(' . ContextProviderInterface::TERRIFIC_CONTEXT_VARIABLE . ', ');
    $this->compileAsContextVariable($variableNameAndArrayKeysPair, '$context');
    $twigCompiler
      ->raw(');')
      ->raw("\n")->write('')
      ->raw('} else {')
      ->raw("\n")->write('')->write('')
      ->raw('throw new \Twig\Error\Error("')
      ->raw($variableDoesNotExistErrorMessage)
      ->raw('");')
      ->raw("\n")->write('')
      ->raw('}')
      ->raw("\n\n");
  }
public function compileAndMergeVariableToContext ( VariableNameAndArrayKeysPair $ variableNameAndArrayKeysPair , ? string $ variableDoesNotExistErrorMessage = NULL ) : void { $ twigCompiler = $ this -> getTwigCompiler ( ) ; $ contextArrayKeys = $ variableNameAndArrayKeysPair -> toTwigContextArrayKeysString ( ) ; if ( $ variableDoesNotExistErrorMessage === NULL ) { $ variableDoesNotExistErrorMessage = addslashes ( 'The variable ' . $ variableNameAndArrayKeysPair -> toTwigVariableString ( ) . ' does not exist. Could not compile it to the Twig context.' ) ; } $ twigCompiler -> raw ( " \n " ) -> write ( '' ) -> raw ( 'if (' ) -> raw ( 'isset(' ) ; $ this -> compileAsContextVariable ( $ variableNameAndArrayKeysPair , '$context' ) ; $ twigCompiler -> raw ( ')' ) -> raw ( ') {' ) -> raw ( " \n " ) -> write ( '' ) -> write ( '' ) -> raw ( ContextProviderInterface :: TERRIFIC_CONTEXT_VARIABLE . ' = array_merge(' . ContextProviderInterface :: TERRIFIC_CONTEXT_VARIABLE . ', ' ) ; $ this -> compileAsContextVariable ( $ variableNameAndArrayKeysPair , '$context' ) ; $ twigCompiler -> raw ( ');' ) -> raw ( " \n " ) -> write ( '' ) -> raw ( '} else {' ) -> raw ( " \n " ) -> write ( '' ) -> write ( '' ) -> raw ( 'throw new \Twig\Error\Error("' ) -> raw ( $ variableDoesNotExistErrorMessage ) -> raw ( '");' ) -> raw ( " \n " ) -> write ( '' ) -> raw ( '}' ) -> raw ( " \n \n " ) ; }
Checks if given variable exists, and adds it to the context.
Checks if given variable exists and adds it to the context .