Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-07-11 21:07:32

Myusername
Member
Registered: 2019-12-12
Posts: 141

A way to play with override forms

It’s not specifically a shortcode, but a way that we can play with override forms, and that can be very useful.

Some time ago I wanted to make some changes in the HTML of the article, but they were small changes and didn’t justify the creation of a second form, since this second form would have basically the same HTML as the first one, except for some icons and etc. . If I later wanted to change something in the HTML, I would have to change it in both forms. As a solution, I remember installing the glz_custom_fields plugin, so I can have a checkbox on the article tab and a conditional tag so I can control what to display in those articles.

So, assuming this is my ‘default’ form:

<article>
	<h1><txp:title/></h1>
	<article::image />
</article>

Now I create a second form, for example, called ‘gallery’:

<txp:variable name="article_gallery" value="1"/>
<txp::default />
<txp:variable name="article_gallery" value=""/>

So once ‘gallery’ is selected: the first line creates a variable called ‘article_gallery’ and assigns it the value of ‘1’. So now with this conditional tag, you can control what to display inside the ‘default’ form, which will be incorporated below.

The last line will just erase the variable’s value so it won’t reflect in the articles that follow.

You can even further organize your code that is inside ‘default’ by doing something like this:

<txp:variable name="gallery_code">
    <!-- all gallery code here... -->
</txp:variable>

<txp:variable name="article_gallery" value="1"/>
<txp::default />
<txp:variable name="article_gallery" value=""/>
<article>
	<h1><txp:title/></h1>

	<if::variable name="article_gallery" value>
		<txp:variable name="gallery_code" />
	<txp:else/>
		<article::image/>
	</if::variable>
</article>

Hope it can be useful at some point. If you have a different way of getting the same result, let me know.

Last edited by Myusername (2021-07-11 21:08:04)

Offline

#2 2021-07-12 12:21:40

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,173
Website

Re: A way to play with override forms

Three other variants on your idea:

1. Only a minor difference: You can use txp:if_custom_field in place of setting a variable and then using if_variable. You then don’t need to unset it afterwards.

<txp:if_custom_field name="has_gallery" value="1">
    <!-- gallery code -->
</txp:if_custom_field>

2. You could use the “override_form” method (using the override_form dropdown) and call the “text_post” form from within the “text_post_gallery” form. That way you don’t necessarily have to repeat the code. That depends, of course, on how complex your code is interleaved. Use that together with txp:yield and txp:if_yield to pass conditional content into a (sub)form.

3. You could detect if the article_image field has more thane image using txp:images and put that in a variable and the offset and limit attributes. If it has more than one image id/url, output your gallery or slider, if just one, then just the header image.

EDIT: added txp:yield.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2021-07-12 15:39:53

Myusername
Member
Registered: 2019-12-12
Posts: 141

Re: A way to play with override forms

The problem with using custom_field for this is because:

  • You need to set a value, eg ‘1’, to be able to compare later.
  • It is not very intuitive (for this purpose) where the custom fields are in the write tab.

However, using txp:yield instead of using a variable is definitely better. The same result, but cleaner. I will join.

Thanks for the reply.

Offline

#4 2021-07-12 16:54:39

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,173
Website

Re: A way to play with override forms

My apologies. I misunderstood you completely. I thought you meant you are currently using custom fields as an alternative to the problem of duplicate code in override forms. But now that I re-read your post carefully, I can see you’re doing exactly what I meant with option 2. I posted too hastily.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB