Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Recursive article form
Hello
I want linked some child articles to a parent article with a custom field and display a recursive list. infinite level. For that I wanted use a recursive article form :
<txp:article_custom section="article" parent="" form="article_list" wraptag="ul" break="li" />
article_form:
<txp:title>
<txp:variable name="child"> // a break for better readability
<txp:article_custom section="article" parent='<txp:article_id />' form="article_list" wraptag="ul" break="li" />
</txp:variable>
<txp:if_variable name="child" value="">
<txp:else />
<txp:variable name="child" />
</txp:if_variable>
</txp:variable>
Textpattern display a alert : Textpattern Notice: form_circular_reference: blah_blah
My form is more complicate. Not only <txp:title />.
Someone have a alternative solution ?
Offline
#2 2013-03-06 19:54:11
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Recursive article form
Interesting setup :)
Calling a form from inside itself doesn’t seem to be the right way. What if you create a second form, ‘article_list_2’, so both forms have the same content, except that in each variable value you use the other form? Just a thought, no idea if that would work…
And I doubt if <txp:if_variable name="child" value="">
will ever return true with those line breaks.
Last edited by els (2013-03-06 19:54:35)
Offline
Re: Recursive article form
Interesting setup :)
I’m not a programmer, but I have some notions in javascript, where it is possible to do recursive functions (another language too), a function that call itself. With the risk of an infinite loop that makes a browser crash.
Textpattern does not allow the same principle. It would be nice!
What if you create a second form, ‘article_list_2’, so both forms have the same content, except that in each variable value you use the other form? Just a thought, no idea if that would work…
In my case a child article itself can have a child article, etc..
And I hope that it works whatever the nesting level.
And I do not see how called one form alternately with another.
And has 2 identical forms complicates maintenance.
I tried to replace the module with a variable or misc
And I doubt if <txp:if_variable name=“child” value=”“> will ever return true with those line breaks.
Yes I know. I did it for you easier reading
Offline
Re: Recursive article form
Textpattern safe guards from circular references by preventing a form template from calling itself. Basically you can’t call a form A again inside form A. In normal scenario that would create an infinite recursion.
Indeed, there would be valid cases where recursions would be beneficial. For instance the mentioned code would work if there wasn’t that stopper. The thing is, we kinda do want to prevent recursions from happening too. They can be much more common than you might think when the guys that build the sites aren’t ‘developers’. Not that it would really be our job to fix the code others make, but the guard is already there.
So, could we allow recursions? Kinda, maybe. We could up the limit per level. Similarly to what regular expressions do. We also could change the check to check evaluated code instead of form names.
…but, that would break the prevention. The code would be run in double which isn’t ideal. If the code contains anything that does modifications (writes, prints output), those modification would be run twice or contents printed to the client.
One other problem is that Textpattern, or its markup language, doesn’t allow break and continue statements. If recursion is going to happen, you can’t cut it. There is no way to exit out from a level, as its already executed. You can only break from it on the next round with an if statement.
Last edited by Gocom (2013-03-06 22:38:27)
Offline
Re: Recursive article form
Textpattern safe guards from circular references by preventing a form template from calling itself. Basically you can’t call a form A again inside form A. In normal scenario that would create an infinite recursion.
In fact, I discovered this and I understand.
For me, it was a quick and elegant solution to my need.
And it’s in the core, I think any plugin could work around this limitation.
I seek another solution. But I can not find!
I am obliged to create a module for each level of nesting. modules which are almost similar.
But I do not want to limit the nesting level.
I would actually be able to nest articles in the same ways that we nests categories.
And display articles Tree (as stw_category_tree or azp_menugen does for categories).
If recursive forms were possible, these plugins are no longer of any use.
I’m stuck.
I am at an impasse.
I will propose one level at the moment and look beside our friend AJAX.
Offline
Re: Recursive article form
And why not use categories for that purpose!!
Offline
Re: Recursive article form
You can set your article_list
form (at your own risk) like this:
<txp:title />
<txp:article_custom section="article" parent='<txp:article_id />' wraptag="ul" break="li">
<txp:php>echo parse(fetch_form('article_list'));</txp:php>
<!-- or even echo fetch_form('article_list'); ? no -->
</txp:article_custom>
Last edited by etc (2014-01-21 15:17:34)
Online
Offline
Re: Recursive article form
Why not to introduce some circular
attribute (0
by default) to <txp:output_form />
to limit the recursion depth? It suffices to replace
if (in_array($name, $stack)) {
trigger_error(gTxt('form_circular_reference', array('{name}' => $name)));
return;
}
...
$txp_current_form = $stack[] = $name;
...
array_pop($stack);
in parse_form()
function with
if(!isset($stack[$name])) $stack[$name] = 0;
if ($stack[$name] > $circular) {
trigger_error(gTxt('form_circular_reference', array('{name}' => $name)));
return;
}
...
$txp_current_form = $name;
$stack[$name] += 1;
...
$stack[$name] -= 1;
Then it would be possible to construct recursive forms like
blah blah
<txp:output_form form="this_very_form" circular="5" />
blah blah
Last edited by etc (2014-10-02 12:19:07)
Online
Re: Recursive article form
I have implemented it (atm for testing only!) in etc_tree. If you create (admin-side) an etc_tree
node (say, id=9
) like this
<txp:category_list wraptag="ul" break="li" parent='<txp:category />' exclude='<txp:category />' children="0">
<txp:category title="1" />
<txp:etc_tree id="9" self="1" circular="3" wraptag="" break="" />
</txp:category_list>
and call <txp:etc_tree id="9" self="1" wraptag="" break="" />
on the public side, it should output (at most) 3 levels of your category tree.
Online
Pages: 1