XF 2.2 Template tags not matching in template modification

kirsty

Member
In the forum_post_thread template I want to conditionally replace the line

Code:
<xf:form action="{{ link('forums/post-thread', $forum) }}" ajax="true" class="block" data-xf-init="attachment-manager"
    draft="{{ link('forums/draft', $forum) }}">

with a different form action.

I thought this could be done using a template modification that finds the above text and replaces it, something like this

Code:
<xf:if is="{$forum.my_condition}">
    <xf:form action="{{ link('forums/check-thread', $forum) }}" ajax="true" class="block" data-xf-init="attachment-manager"
    draft="{{ link('forums/draft', $forum) }}">
<xf:else />
    <xf:form action="{{ link('forums/post-thread', $forum) }}" ajax="true" class="block" data-xf-init="attachment-manager"
    draft="{{ link('forums/draft', $forum) }}">
</xf:if>

But though the Test looks ok the modification won't save and the xf:if> is displayed in bright red.
Line 13: Template tags are not well formed. Tag if was found when expecting form. - Template modifications: public:forum_post_thread
So I think it's saying 'hey, you put in a form tag with no ending tag? Which I have indeed done because I only want to modify this bit of this template at this time.

I've tried it with regex and get the same result. I guess I could make the <xf:form bit part of a regex grouping so the replace part of the template would just see a $1 or whatever but that seems needlessly complicating.

Am I missing something?

Thanks.
 
Why not just wrap the whole form in your conditional statements?
Code:
<xf:if is="{$forum.my_condition}">
    <xf:form action="{{ link('forums/check-thread', $forum) }}" ajax="true" class="block" data-xf-init="attachment-manager"
    draft="{{ link('forums/draft', $forum) }}">
        ...
    </form>
<xf:else />
    <xf:form action="{{ link('forums/post-thread', $forum) }}" ajax="true" class="block" data-xf-init="attachment-manager"
    draft="{{ link('forums/draft', $forum) }}">
        ...
    </form>
</xf:if>
 
That just seems unnecessarily complicated as a replacement really, not to mention much harder to read! It's 109 lines in the find and replace template modification when I only want to replace a few characters.

I just tried it and it does work, though my first attempt didn't work because the code inside the form had already had another template modification so I needed to get the template modifications in the correct order. It would be nice to keep the separate modifications independent of each other.

Though it'd be more readable/separable as a regex though, I'll go and see if I can get that to work.
 
Last edited:
You could use a ternary:

HTML:
<xf:form action="{{ $forum.my_condition ? link('forums/check-thread', $forum) : link('forums/post-thread', $forum) }}" ajax="true" class="block" data-xf-init="attachment-manager"
    draft="{{ link('forums/draft', $forum) }}">
 
You could use a ternary:
Thank you, that's a good idea, and works perfectly well, though it's probably less obvious to my mainly-non-coding colleague as to what's going on, but hopefully he never needs to look at it.

I still think the requirement to have the template modification be a valid fully matched template in itself is a bit excessive and non-intuitive though, but perhaps I'm missing something there. I'd guess that even if you made sure both halves of an if/else left things in the same state then there'd be something else to go wrong. Sometimes I'd just like to override the safety checks! (Whilst mostly being glad of them.)
 
Templates are compiled into PHP, and template modifications are applied before this step (they're essentially invisible to the compiler), so the requirement is really that all template modifications result in a template which is able to be parsed by the compiler. Any syntax which would not be valid in a standalone template will not be valid when produced by a template modification either.
 
Templates are compiled into PHP, and template modifications are applied before this step (they're essentially invisible to the compiler), so the requirement is really that all template modifications result in a template which is able to be parsed by the compiler. Any syntax which would not be valid in a standalone template will not be valid when produced by a template modification either.
Thank you for taking the time to explain. It really helps when I get frustrated with stuff to know why it is like that!
 
Back
Top Bottom