Changing a template var from a template

Discussion forum for Extension Writers regarding Extension Development.
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: Changing a template var from a template

Post by javiexin » Wed Apr 29, 2015 11:05 pm

Thanks a lot nicofuma, received and tested a few things with this.

Unfortunately, no positive results.

I seem to have isolated the issue: if I use the variable in the SAME file, everything works well; but if I use the variable ACROSS files, then the value is not modified. So it is either a problem of scoping (variable are applied "per file"), which I believe is not the case, or else is a problem of ORDER of evaluation of the files, the template being processed before the events code.

Could anyone confirm?

And, also, in case this is true, any suggestion as to how to achieve what I want (influence the template processing within the main template file by applying changes in the event code)?

Thanks a lot,
-javiexin

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Mon Feb 04, 2019 12:52 pm

Hi
How to change value for var in template eg. vname.S_SOMETHING ?
I try to do this

Code: Select all

<!-- PHP -->
	$template->assign_var('vname.S_SOMETHING', 0));
<!-- ENDPHP -->
or

Code: Select all

<!-- PHP -->
	$template->assign_var('vname', array('S_SOMETHING' => 0));
<!-- ENDPHP -->
but no efffect.
Something like this

Code: Select all

{% set name.S_SOMETHING = 0 %}
ends with error:
Uncaught Twig_Error_Syntax: Unexpected token "punctuation" of value "." ("end of statement block" expected)

Sorry, Im am beginner in this question :(
Sorry my english. I hope You understand what I mean.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 946
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Changing a template var from a template

Post by mrgoldy » Mon Feb 04, 2019 2:02 pm

You can try {% set attribute(name, S_SOMETHING) = 0 %}
Or perhaps something like this in PHP:
$this->template->alter_block_array('name', array('S_SOMETHING' => 0), false, 'change');

Haven't tried these options but that should put you in the right direction.
phpBB Studio / ''Proud member of the Studio"

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Mon Feb 04, 2019 2:40 pm

I try to use this
{% set attribute(name, S_SOMETHING) = 0 %}
but instead 0 I want to use logical value of another vars eg var.S_SOMETHING1 and var.S_SOEMTHING2 and ....
{% set attribute(name, S_SOMETHING) = (var.S_SOMETHING1 and var.S_SOEMTHING2) %}
but this make me error:
Fatal error: Uncaught Twig_Error_Syntax: Unexpected token "punctuation" of value "(" ("end of statement block" expected).
Sorry my english. I hope You understand what I mean.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 946
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Changing a template var from a template

Post by mrgoldy » Mon Feb 04, 2019 4:17 pm

As you've noticed, you can not use loop.VARIABLE, that's where the attribute(loop, VARIABLE) comes in within Twig.
So that would result in
{% set attribute(name, S_SOMETHING) = attribute(var, S_SOMETHING_1) and attribute(var, S_SOMETHING_2) %}
phpBB Studio / ''Proud member of the Studio"

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Mon Feb 04, 2019 7:50 pm

I don't know why, but it doesn't work for me:
{% set attribute(postrow, S_SOMETHING) = attribute(postrow, S_SOMETHING_1) and S_SOMETHING_2 %}

Still
Fatal error: Uncaught Twig_Error_Syntax: Unexpected token "punctuation" of value "(" ("end of statement block" expected).

Even for this
{% set attribute(postrow, S_SOMETHING) = attribute(postrow, S_SOMETHING_1) %}

The same error occured :(
Sorry my english. I hope You understand what I mean.

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Tue Feb 05, 2019 10:11 pm

I can not assign a value to any variable in the 'postrow' block.
This {% set attribute(postrow, VAR)=value always make me an error :(
Sorry my english. I hope You understand what I mean.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3632
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Changing a template var from a template

Post by kasimi » Tue Feb 05, 2019 10:17 pm

attribute() is a function. You can't assign a value to a function result.

Is there no way for you to change the value in PHP, before it is assigned to the template?

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Wed Feb 06, 2019 7:55 am

I tried to do something as Mrgoldy advised me.
I want to change value of var inside style file event in depends on post number. I have never written an extension before, so I have a problems on start. Maybe should I operate on $postrow in php and next assign it to postrow? I don't know.
Sorry my english. I hope You understand what I mean.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3632
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Changing a template var from a template

Post by kasimi » Wed Feb 06, 2019 9:02 am

You can change the value within your template event file with the merge filter, but it won't affect the value outside of your template event file which is probably what you're aiming for. You will have to do it in PHP.

tojag wrote:
Wed Feb 06, 2019 7:55 am
Maybe should I operate on $postrow in php and next assign it to postrow?
Ideally, you modify the value before it is assigned to the template.

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Wed Feb 06, 2019 12:30 pm

Kasimi, can You give me a sample of code, how to assign something to postrow.VARIABLE in style file? I can't see any idea how to use merge() in this case.
By using attribute() function, as You write I can only read value, not assign.
{% set postrow.VARIABLE = VALUE %} doesn't work too
Fatal error: Uncaught Twig_Error_Syntax: Unexpected token "punctuation" of value "." ("end of statement block" expected).

I have no idea :(
Sorry my english. I hope You understand what I mean.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3632
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Changing a template var from a template

Post by kasimi » Wed Feb 06, 2019 12:33 pm

Code: Select all

{% set postrow = postrow | merge({'VARIABLE': 'new value'}) %}
But I think you missed this part:
kasimi wrote:
Wed Feb 06, 2019 9:02 am
it won't affect the value outside of your template event file

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Wed Feb 06, 2019 1:06 pm

Fine! It works!
kasimi wrote:
Wed Feb 06, 2019 9:02 am
it won't affect the value outside of your template event file
It seems to be ok for me, because I want to do something in one place only and next restore original variable value. In this case I will not to do it.
But if I would to restore value, should I do something like thise?

Code: Select all

<!-- store original value to tmp -->
{% DEFINE TEMP = attribute(postrow, VARIABLE) %}
{% set postrow = postrow | merge({'VARIABLE': 'NEW_VALUE'}) %}
{% endif %}
........ //
<!-- restore original value -->
{% set postrow = postrow | merge({'VARIABLE': 'TEMP'}) %}
Sorry my english. I hope You understand what I mean.

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Wed Feb 06, 2019 1:40 pm

You are right. I didn't see (or can't see) 'event' in your sentence.
it won't affect the value outside of your template event file
:oops:
Sorry my english. I hope You understand what I mean.

User avatar
tojag
Registered User
Posts: 398
Joined: Thu Aug 07, 2014 8:00 am
Location: Warsaw, Poland, EU
Name: Gregory

Re: Changing a template var from a template

Post by tojag » Thu Feb 21, 2019 1:31 pm

I want to change links to terms and privacy on login page. For this I used the event login_box_modify_template_data.
So, I have this:

Code: Select all

class main_listener implements EventSubscriberInterface
{
	public static function getSubscribedEvents()
	{
		return array(
			'core.login_box_modify_template_data'	=> 'login_box_modify_template_data',
		);
	}

	public function login_box_modify_template_data($event)
	{
		$login_box_template_data = $event['login_box_template_data'];
		
		$login_box_template_data = array(
			'U_TERMS_USE' => 'https://maindomain.com/rules.html',
			'U_PRIVACY' => 'https://maindomain.com/privacy.html',
		);
		/* early was this but not affected too
		$login_box_template_data['U_TERMS_USE'] = 'https://maindomain.com/rules.html';
		$login_box_template_data['U_PRIVACY'] = 'https://maindomain.com/privacy.html';
		*/

		$event['login_box_template_data'] = $login_box_template_data;
	}
}
What wrong I do?
Sorry my english. I hope You understand what I mean.

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 6 guests