Topic on Project:Support desk

How to store category information in templates in Mediawiki? [RESOLVED]

42
Summary by Jonathan3

Page Forms.

114.73.149.245 (talkcontribs)

I created a form to allow users to input data about scientific topics, references and categories in an organized manner. Problem is, I am unable to store the category information within the template. Hence, I cannot automatically categorize the page when a user fills the form with the appropriate category information. Here is the template page from my Mediawiki site:

{{#template_params:Summary (label=Tip: summarize the topic in point form, as a numbered list. Include references between <ref></ref>[1] tags.))|Category (list)}}

Reference(s)

  1. Your references will automatically appear here

Here is the simple form I created to collect the data:

This is the "Summary Article" form. To create a page with this form, enter the page name below; if a page with that name already exists, you will be sent to a form to edit that page.

{{#forminput:form=Summary Article}}

Jonathan3 (talkcontribs)

Try

[[Category:{{{Category|}}}]]

Jonathan3 (talkcontribs)

Assuming "category" isn't something you can't call a field.

114.73.149.245 (talkcontribs)

Hey, thank you for answering! Unfortunately that doesn't help though...

114.73.149.245 (talkcontribs)

I even changed the variable to {{{User_Category|}}}

Jonathan3 (talkcontribs)

Did you put it in the template?

It should work, I think.

Try asking on the page forms talk page. The author will likely reply.

Jonathan3 (talkcontribs)

Looked at your code by "editing" it here.

I think {{{section|Category|level=2}}} is wrong for what you want.

Try field instead of section.

Jonathan3 (talkcontribs)

On the phone so this is a pain to type. Please click edit to see it.

try User_Category in the form

then in the template

{{#arraymap:|,|@@||}}

114.73.149.245 (talkcontribs)

Yeah tried all that, but no hope! Here's my template code:

<noinclude>
{{#template_params:Summary (label=Topic Summary (In point form, as a numbered list. Include references between <ref></ref> tags.))|User_Category}}
</noinclude><includeonly>
{{#template_display:_format=sections}}
[[Category:Summary Article]]
[[Category:{{#arraymap:|,|@@||}}]]
</includeonly>

Here's my form code:

<noinclude>
This is the "Summary Article" form.
To create a page with this form, enter the page name below;
if a page with that name already exists, you will be sent to a form to edit that page.

{{#forminput:form=Summary Article}}

</noinclude><includeonly>
<div id="wikiPreview" style="display: none; padding-bottom: 25px; margin-bottom: 25px; border-bottom: 1px solid #AAAAAA;"></div>
=Summary=
{{{section|Summary|level=1}}}

==Category==
{{{field|User_Category|level=2}}}

</includeonly>
Jonathan3 (talkcontribs)

You'll have to click edit on this page on my last answer to see what to use.

114.73.149.245 (talkcontribs)

It doesn't say edit, do I have to log in for that?

114.73.149.245 (talkcontribs)

My bad, just saw it

114.73.149.245 (talkcontribs)

Yeah, did all the changes but still not working though. But thank you for trying! I really appreciate it!

Jonathan3 (talkcontribs)

No problem. It looks like you copied and pasted from the wrong place. My fault for not bothering with syntaxhighlight. When I'm on the computer I'll get back to this.

114.73.149.245 (talkcontribs)

Ok, thank you!

Jonathan3 (talkcontribs)

Ok, here we go. I've not tested this.

In your original code it mentioned "type in categories separated by commas". So in the form you want to have the "field" tag with "list" parameter (see Extension:Page_Forms/Defining_forms#'field'_tag). The "level" parameter is only for the "section" tag.

==Category==
'''Tip: type in categories separated by commas.'''
{{{field|User_Category|list}}}

You'll end up with User_Category being a comma-separated list. So if you want to do anything with the individual members of the list you need to split them out somehow (see Extension:Page_Forms/Defining_forms#Multiple_values_for_the_same_field).

{{#arraymap:{{{User_Category|}}}|,|@@|[[Category:@@]]|}}

This basically splits User_Category where it finds commas, treats each item as @@ and replaces that with [[Category:@@]]. You can use other text instead of "@@" if you prefer.

114.73.149.245 (talkcontribs)

It's sill not working as the category information doesn't turn up on the page that is created at all...

Jonathan3 (talkcontribs)

Maybe get rid of the _

114.73.149.245 (talkcontribs)

Nope, doesn't work. I tried the PageForms discussion forum to no avail. Sure, someone must have tried to do something as simple as this in the past?

114.73.149.245 (talkcontribs)

I tried to include just a random new category to the template page, and even that did not work. Something is fundamentally wrong with what I'm doing and I don't know what. I followed the documentation to the letter...

Jonathan3 (talkcontribs)

I do it on my wiki and it works fine. Could you share your URL?

Jonathan3 (talkcontribs)

Hold on a minute - are you saying that categorisation via template doesn't work at all?

114.73.149.245 (talkcontribs)

That's right, categorisation via template does not work at all. I am unable to post the URL here as it keeps saying 'link spam'

Jonathan3 (talkcontribs)

Just type it without the http part :-)

Does normal page categorisation work?

114.73.149.245 (talkcontribs)

Yes, normal categorisation works.

Here's the template: 35.232.89.39/wiki/Template:Summary_Article

And here's the form: 35.232.89.39/wiki/Form:Summary_Article

114.73.149.245 (talkcontribs)

Could you paste a copy of your template and form here. I'll paste them onto my wiki and see if it works (since you mentioned that it works for yourself)

Jonathan3 (talkcontribs)
[[Category:{{#arraymap:{{{User_Category|}}}|,|@@|[[Category:@@]]|}}]]

is wrong. You just want the text from the first { to the last } -- see above.

Also you might want to remove the _ here and replace with a space. I can't remember. Try it.

Jonathan3 (talkcontribs)
==Category==
{{{field|UserCategory}}}

is also wrong. You need the field name to be the same as in the template. Though I think the field name here does need the _. On the phone again so harder to check.

Also if you want to input a list you need the list parameter -- see above.

Jonathan3 (talkcontribs)

Just checked. Space is fine. No need for _ on either page.

114.73.149.245 (talkcontribs)

I made the suggested corrections, but it still doesn't work.

114.73.149.245 (talkcontribs)

Does it work on your wiki?

Jonathan3 (talkcontribs)

You were missing

{{{for template|Summary Article}}}

So when the page was saved via the form it didn't contain a template call, so the template wouldn't have known about what was typed into the category input box.

It's always best to create the templates and forms using the extension's special pages, so that you don't miss out on important things: Extension:Page_Forms/Special_pages.

Making minimal changes to your pages, the following should work:

Form:Summary Article

<noinclude>
This is the "Summary Article" form.
To create a page with this form, enter the page name below;
if a page with that name already exists, you will be sent to a form to edit that page.

{{#forminput:form=Summary Article}}

</noinclude><includeonly>
<div id="wikiPreview" style="display: none; padding-bottom: 25px; margin-bottom: 25px; border-bottom: 1px solid #AAAAAA;"></div>
{{{for template|Summary Article}}}
==Summary==
{{{section|Summary|level=1}}}

==Category==
{{{field|User Category|input type=tokens|list}}}

</includeonly>

Template:Summary Article

<noinclude>
{{#template_params:Summary (label=Test)|User Category}}
</noinclude><includeonly>
{{#template_display:_format=text}}
[[Category:Summary Article]]
{{#arraymap:{{{User Category|}}}|,|@@|[[Category:@@]]|}}
</includeonly>

I've tested it on an old version of Page Forms, before #template_display and #template_params existed, but hope that part of it is unproblematic.

114.73.149.245 (talkcontribs)

It nearly worked! Thank you. Here's an example page I created, but note that there is a disembodied "User category:" on top:

35.232.89.39/wiki/Example_page

114.73.149.245 (talkcontribs)

Also it now states "Summary Article" as the category, but does not state "Example Category" which I had input into the form...

Jonathan3 (talkcontribs)

Maybe the tokens input type confused you and you ended up not entering anything in that box.

After typing in that box, hit tab or comma to "tokenise" what you've typed.

114.73.149.245 (talkcontribs)

As I stated above, I entered “Example category” in the category info box. But are you saying that I still need to put a coma at the end even if it’s just one category I entered?

Jonathan3 (talkcontribs)

The tokens input type is a bit counter-intuitive. After typing in that box, hit tab or comma to "tokenise" what you've typed.

Alternatively, change the entire line:

{{{field|User Category|input type=tokens|list}}}

to:

{{{field|User Category|list}}}

I think then it would just be a plain HTML input box and you can't go wrong.

114.73.149.245 (talkcontribs)

So close! Now it keeps saying "User Category:" on top of the page. How do I get rid of that?

114.73.149.245 (talkcontribs)

NVM I figured it out. You can close this issue finally!

Jonathan3 (talkcontribs)

Haha no problem!

Maybe you could add your solution here.

114.73.149.245 (talkcontribs)

Thank you for your help BTW!

114.73.149.245 (talkcontribs)

The solution is the template:

<noinclude>
{{#template_params:Summary|Categories}}
{{#template_display:_format=sections}}
</noinclude><includeonly>
[[Category:Summary Article]]
{{#arraymap:{{{Categories|}}}|,|x|[[Category:x]]|\s}}
</includeonly>

Which brings me to the next question - which I shall post as a new topic above. Thank you for your help BTW.