Page 1 of 2

Replacing a Template Between Two Events

Posted: Sun Aug 03, 2014 10:39 am
by david63
Why does this not work?

In the event forumlist_body_category_header_before I have

Code: Select all

<!-- IF S_SOME_VAR -->

my code goes here

<!-- ELSE -->
which should then execute the original template

and then in forumlist_body_category_header_after

Code: Select all

<!-- ENDIF -->
so that I would effectively have

Code: Select all

<!-- IF S_SOME_VAR -->

my code goes here

<!-- ELSE -->

original code

<!-- ENDIF -->
I know that the var is there and valid but the whole page just stops irrespective of what goes in as the code

My understanding is/was that whatever goes into a template event just adds that code into the template at that point and the template carries on.

EDIT:
I should also add that if I put the code directly into the template file then it works perfectly :?

Re: Replacing a Template Between Two Events

Posted: Sun Aug 03, 2014 4:19 pm
by MattF
template events are rendered as a complete unit, so you have to have a fully enclosed conditional structure for it to work.

Conditionals and other control structures can not be started in one template event and finished in another.

This is a known limitation of the template and event system.

Re: Replacing a Template Between Two Events

Posted: Sun Aug 03, 2014 5:50 pm
by david63
Thanks for that - it would have been useful to know it in advance rather than waste two days trying to get it to work.

This is a major problem with extensions - without the ability to replace a block of template code (which with a mod could be done in one line) then it will be virtually impossible to recreate many of the current mods.

I am sure that I have read somewhere (cannot find it now) that it was the intention to be able to do an if/else within a template.

Re: Replacing a Template Between Two Events

Posted: Sun Aug 03, 2014 7:55 pm
by tbackoff
If it helps, you can replace template variables within the PHP code itself - example. Using this logic you could do something similar to 'TEMPLATE_VAR' => $conditional = true ? $if_true : $if_false.

Re: Replacing a Template Between Two Events

Posted: Sun Aug 03, 2014 9:06 pm
by nicofuma
You can also use JQuery and the css (display: none) to "replace" one part of the html code

Re: Replacing a Template Between Two Events

Posted: Mon Aug 04, 2014 6:10 am
by david63
tmbackoff wrote:If it helps, you can replace template variables within the PHP code itself - example. Using this logic you could do something similar to 'TEMPLATE_VAR' => $conditional = true ? $if_true : $if_false.
Yes - I know that I can replace template variables and this may be the way that I have to go, but it will not be elegant.

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 5:45 am
by 3Di
Hello, I am not bringing back to life a dead horse, am I?

Same problem here, I am working porting MODs to extensions and I am facing the same problem described by David. Basically while hard-coding the phpBB 3.1 the new block of code perfectly works..
it doesn't (apparently) once ported, I am speaking about of a replacement of a block of HTML.

File to modify: viewforum_body.html

Block of code to be replaced (if I replace it hard-coding it just works as expected)

Code: Select all

<!-- IF S_DISPLAY_ACTIVE -->{L_ACTIVE_TOPICS}<!-- ELSEIF topicrow.S_TOPIC_TYPE_SWITCH and (topicrow.S_POST_ANNOUNCE or topicrow.S_POST_GLOBAL) -->{L_ANNOUNCEMENTS}<!-- ELSE -->{L_TOPICS}<!-- ENDIF -->
I am atm using the event <!-- EVENT viewforum_body_topic_row_before -->
but I have to copy/paste on it the whole original code between
<!-- BEGIN topicrow --> and <!-- END topicrow -->
and I am not having the same expected result.

The extension is coded and it seems that everything (PHP side) works, the debug of the code gives to me the results I am expecting from it.


The question is.. is it something changed since? (in 2014 I wasn't studying the new core code)

My exhaustive search landed here, if someone can point me out to an elegant solution I will be grateful.

Thx.

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 7:14 am
by david63
3Di wrote:The question is.. is it something changed since?
Not that I have seen.

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 1:53 pm
by javiexin
Definitely, this is a very important limitation of the (template) EVENT system.

What I have done in my case is similar to what Nicofuma suggested, use jQuery to modify the DOM and "hide", or "remove" or "move" certain blocks of existing HTML code from the templates.

I have created a very small jQuery library of functions to do what I mentioned above, manipulating the DOM.
So, what I do in my HTML code in an event is to use data- parameters to the "new" DOM elements, indicating where to position themselves (either replacing, prepending or appending to an existing DOM element).

If you want, this library is available https://github.com/javiexin/jxtools although I have not modified it in some time... There are some examples there, and you will see it in real use in the next version of my Advanced Polls extension...

Hope this helps,
-javiexin

PD: This is one of the reasons I have asked about extension dependencies: I would like to install this library as an independent extension, only once, and have other extensions "depend" on it being present... Another limitation of the current extension system.

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 2:36 pm
by 3Di
Thanks, I will explore it for sure. I am interested on this library.
javiexin wrote:PD: This is one of the reasons I have asked about extension dependencies: I would like to install this library as an independent extension, only once, and have other extensions "depend" on it being present... Another limitation of the current extension system.
I posted a possible solution for your case here:
viewtopic.php?p=14572856#p14572856

Btw, if you know how I can solve my above issue with the template block to be replaced using your library I will be more than happy, I will learn by doing in this case.

Regards.

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 3:04 pm
by javiexin
3Di wrote:Thanks, I will explore it for sure. I am interested on this library.
javiexin wrote:PD: This is one of the reasons I have asked about extension dependencies: I would like to install this library as an independent extension, only once, and have other extensions "depend" on it being present... Another limitation of the current extension system.
I posted a possible solution for your case here:
viewtopic.php?p=14572856#p14572856

Btw, if you know how I can solve my above issue with the template block to be replaced using your library I will be more than happy, I will learn by doing in this case.

Regards.
Thanks for the tip in the other thread. I will definitely review it...

Regarding the possibility to use the library, please, do so. I am not sure if you will achieve what you want, as this is meant to change HTML elements, not labels, but maybe if you open it up a bit to the next containing element, I am positive you will be able to do what you want. Anyhow, if you have troubles, let me know, and I will try to help you.
-javiexin

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 3:25 pm
by 3Di
I believe I need a lot of time to understood myself how all of this works, I have to study some things I don't really perfectly know. A "real use case" instead will increase my learning curve, I do have a strange approach to some things, I am more a "visual code reader"... because it's faster to me than study every single bit and get conclusions. At the end of all.. I am a Cook. :)

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 7:59 pm
by javiexin
Ok, here it goes an example.

Put the jxtools.js file into yourextension/styles/all/lib folder.

Say you want to replace the above. Well, you will have to replace "a little" more, the enclosing HTML element, that is dt#active_topics > div.list-inner.

You would have to add something like the below code in an appropriate event in viewforum_body.html, say viewforum_body_topic_row_before if the replacement code is generic (does not depend on the topicrow loop), or viewforum_body_topicrow_row_before if it is dependent on the topicrow loop.

Code: Select all

<!-- IF not $INCLUDED_JXTOOLSJS -->
	<!-- INCLUDEJS lib/jxtools.js -->
	<!-- DEFINE $INCLUDED_JXTOOLSJS = true -->
<!-- ENDIF -->
	<div id="my_list_inner_hidden_container" style="display:none;" jxdom-target="dt#active_topics > div.list-inner" jxdom-action="replace">
		<div class="list-inner">Your modified statements go here</div>
	</div>
Note that the hidden container could be generic (as in the example above) or loop-dependent, by changing the div id to be unique (different per topicrow), so you may want to add S_ROW_COUNT to the id...
Basically, what you are saying here is that:
  1. Load the library if not done already
  2. Create a hidden element that holds the "new" structure
  3. In that hidden element, specify the action ('replace') to perform on the target DOM element (or elements if the selector matches more than one object: dt#active_topics > div.list-inner)
  4. The library finds the DOM target element, and performs the action specified, with the contents of the hidden element, no further action needed
Take a look at the library to see what other options you may use.
This is jQuery based, so no extra library to load (the jxtools lib is extremely small).

Test it, and let me know what you think,
-javiexin

Re: Replacing a Template Between Two Events

Posted: Sat Dec 03, 2016 11:21 pm
by 3Di
A big step beyond indeed. :geek: :)

On a side note we must use @vendor_name/js in order to be loaded properly without TWIG errors.
example: <!-- INCLUDEJS @vendor_name/js/jxtools.js -->
I did put the .js into the js folder newly created /prosilver/js/ because with "lib" throws an error.

Well, at a first glance it works like a charm and very fast. Now I have to find the reason why I am not getting the expected behaviour like when I hard-coded the MOD into phpBB 3.1.. should be (I guess) something to be tuned into the PHP events (or the JS replacement), the debug logic though tells me are properly done.

You know, tomorrow is another day.

Many thanks, I will post back If I have other issues about templates.
Thanks for the hard work.

Re: Replacing a Template Between Two Events

Posted: Sun Dec 04, 2016 1:16 pm
by javiexin
Glad it helped.
3Di wrote:On a side note we must use @vendor_name/js in order to be loaded properly without TWIG errors.
example: <!-- INCLUDEJS @vendor_name/js/jxtools.js -->
I did put the .js into the js folder newly created /prosilver/js/ because with "lib" throws an error.
Not sure why this is happening to you, it has worked for me always, from the SAME extension that is using it. I understand the prefix is needed when using a library from other extension, but from within the same one? Is it mandatory to include this? Since when? Not questioning, just asking... I have been "out of the game" for over a year, and might have missed it. Another comment: the library should NOT go in the prosilver style folder (be it lib or js, that is irrelevant from my POV) but in the all style folder, as it applies to any style. The template event code will be VERY style specific (prosilver, or other) but the library itself is generic.
3Di wrote:Now I have to find the reason why I am not getting the expected behaviour like when I hard-coded the MOD into phpBB 3.1.. should be (I guess) something to be tuned into the PHP events (or the JS replacement), the debug logic though tells me are properly done.
If you need/want a hand with the style changes and library usage, let me know, and I will try to be more precise (PM me, as this might be not so much general interest).

Best regards,
-javiexin