• Resolved cwcj

    (@cwcj)


    I have some variables that are set in the Twig for our site. I am not able to reference these in the Plain Twig block for the plug-in.

    I have tested, and these variables (both set in the function.php and specific php templates through the context array) can be called in other twig files embedded in the same page template. However, the same references do not work in the Plain Twig blocks in WordPress. Is this possible? Can the be referenced?

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author alexacrm

    (@alexacrm)

    @cwcj

    do you have your own instance of twig (eg timber plugin)? And embedded twig files – is that the reference to twig files brought in via Timber or similar?

    Can you expand a little bit and perhaps insert some sample code + twig fragments where it does / does not work?

    Thanks
    George

    Thread Starter cwcj

    (@cwcj)

    Thank you for your response George, and apologies for the delay in mine.

    I do have the timber plugin – but our theme does not use Twig, in the main. Perhaps easiest if I give the specifics. Please bear with me.

    We have added the relevant timber code to our functions.php file (the timber support (public function __construct() ) etc) and for a testing example have included:

    public function add_to_context( $context ) {
    		$context['stuff'] = 'I am a value set in your functions.php file';
    		return $context;
    	}

    Then in the relevant post template php file we have included:

    $context['the_message'] = 'Further value from template file';

    When we then render a twig file inside this file eg:
    Timber::render( 'welcome.twig', $context);

    we are able to call inside welcome.twig the variables:

    {{ the_message }} {{ stuff1 }}

    (this proceeds the php code the_content())

    However, when we use the same twig code inside the Plain Twig block on the wordpress page, these varaibles do not render.

    Hope this is clear.

    Plugin Author alexacrm

    (@alexacrm)

    @cwcj

    yes, as I thought, you’re adding variables in Timber twig engine. Our twig instance is independent of that one (we can’t rely on Timber being there on all installs!). To add variable to our instance of twig:

    
    add_action( 'integration-cds/twig/after-globals', function ( $twigEnvironment ) {
        $twigEnvironment->addGlobal( 'stuff', 'I am a value set in your functions.php file' );
    } );
    

    And then {{ stuff }}

    HTH
    George

    Plugin Author alexacrm

    (@alexacrm)

    @cwcj and if you are after custom functions:

    add_filter( 'integration-cds/twig/functions', function( $functions ) {
        $functions['translate'] = new TwigFunction(
            'translate',
            function ( $text ) {
                return __( $text );
            }
        );
    
        return $functions;
    }, 10, 1 );

    Usage:
    {{ translate('Hello') }}

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Variables in the Plain Twig block’ is closed to new replies.