Jump to content
  • 0
Sign in to follow this  
Guest Rick

Tables & Templates

Question

Guest Rick

Very new to wiki 'stuff' and I hope this is the right place to ask:

I'm trying to create a template for tables, and a template for rows.  Here's what I have so far:

{{Template:Table
|class=wikitable|sortable=sortable|collapsible=mw-collapsible|collapsed=mw-collapsed|style_center="text-align: center;"
|caption_style="text-align: left; width: 200px"|caption=Table 1
|header1=My Header1|header2=My Header2|header3=My Header3|header4=My Header4
}}

Template:Table

{| class="{{{class}}} {{{sortable}}} {{{collapsible}}} {{{collapsed}}}" style={{{style_center}}}
|+style={{{caption_style}}}|{{{caption}}} 
! scope="col" | {{{header1}}} !! scope="col" | {{{header2}}} !! scope="col" | {{{header3}}} !! scope="col" | {{{header4}}}
|-
|{{Template:Table_Row| cell_value1 = Row 1 Cell 1}}
|}

Template:Table_Row

{|
|data-sort-value="{{{cell_value1}}}"|[[{{{cell_value1}}}]]
|}

The template for the table works fine, well what I see is what i expected to see.  It's getting the rows added to the table that causing the frustration.  It could be that I am way off and making this much more difficult than it needs to be!

Eventually I want to be able to create a table with a variable number of rows...but just trying to get even basics to work.

Thanks very much in advance!

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

In MediaWiki markup, {| indicates the beginning of a new table and |} the end of a table. Your table row template should therefore not include those lines. I'm not entirely sure why you want to turn this into a template (as it is, in my opinion, more complex than just putting the raw table syntax on the page). If you do want to go down this route, you'll need to split things into 3 templates instead of wrapping it all into a single call to {{Table}}:

  1. [[Template:Table header]] would contain the table header, along with the parameters to make it sortable, collapsible, etc.
  2. [[Template:Table row]] (or just [[Template:Row]] if you want to save typing) would contain the markup to create a single row
  3. [[Template:Table footer]] would contain the final closing markup |} for the table and any other footer data you want

Perhaps something like the following:

Template:Table header
{| class="wikitable {{#if:{{{sortable|}}}|sortable}} {{#if:{{{collapsible|}}}|mw-collapsible}} {{#if:{{{collapsed|}}}|mw-collapsed}}" {{#if:{{{center|}}}|style="text-align: center"}}
{{#if:{{{caption|}}}|
{{!}}+ {{#if:{{{caption-style|}}}|style="{{{caption-style}}}" {{!}} }} {{{caption}}} }}
! scope="col" | {{{1}}} {{optheader|{{{2|}}}}} {{optheader|{{{3|}}}}} {{optheader|{{{4|}}}}} {{optheader|{{{5|}}}}} {{optheader|{{{6|}}}}}
Template:Optheader
{{#if:{{{1|}}}| !! scope="col" {{!}} {{{1}}} }}
Template:Table row
|-
| data-sort-value="{{{1}}}" | [[{{{1}}}]] {{optrow|{{{2|}}}}} {{optrow|{{{3|}}}}} {{optrow|{{{4|}}}}} {{optrow|{{{5|}}}}} {{optrow|{{{6|}}}}}
Template:Optrow
{{#if:{{{1|}}}| {{!}}{{!}} data-sort-value="{{{1}}}" {{!}} [[{{{1}}}]] }}
Template:Table footer
|}

Then, you would use it as follows:

{{table header
|sortable=1 (if you want it to be sortable, otherwise leave this line out)
|collapsible=1 (if you want it to be collapsible, otherwise leave this line out)
|collapsed=1 (if it's default collapsed, otherwise leave this line out)
|caption-style=font-weight: bold; ... (optional, you can leave this line out if no style)
|caption=Caption goes here (optional, you can leave this line out if no caption)
|First column header (required)
|Second column header (optional, you can leave this line out)
|Third column header (optional, you can leave this line out)
|Fourth column header (optional, you can leave this line out)
|Fifth column header (optional, you can leave this line out)
|Sixth column header (optional, you can leave this line out)
}}
{{table row|Row 1 Cell 1|Row 1 Cell 2|Row 1 Cell 3|Row 1 Cell 4|Row 1 Cell 5|Row 1 Cell 6}}
{{table row|Row 2 Cell 1|Row 2 Cell 2|Row 2 Cell 3|Row 2 Cell 4|Row 2 Cell 5|Row 2 Cell 6}}
...
{{table footer}}

First header and first cell are required, 2-6 are optional and are left out if omitted.

If you aren't familiar with template syntax, there are a lot of tricks being used here.

  1. The #if parser function tests if a value is blank or not. If not blank, it outputs the text. Requires that Extension:ParserFunctions is installed (it is bundled with every mediawiki download)
  2. Numbered parameters don't require you to specify the name of them when calling the template; they're taken as positional unnamed parameters. You can still explicitly specify the name though (as 1=Value or 2=Value) -- which is required if the Value contains an = sign.
  3. The syntax {{{1|}}} means "If the parameter 1 was passed in, use the value for that. Otherwise use the value after the pipe, which in this case is blank/empty. This plays nicely into trick #1 above.
  4. The {{!}} built-in template expands to the pipe character |. Since | has a special meaning inside of templates, {{!}} is used to "escape" the pipe so it doesn't get looked at as part of the template, but rather as part of the resulting table markup.

Overall though, this is probably more complicated than just encoding the table markup directly on the pages where you care about it...

Share this post


Link to post
Share on other sites
  • 0
Guest Rick

Thanks a ton for the response.  I've been beating my head against my desk for more hours than I'd like to admit.

As far as being to complicated, being new to all this my thought was to keep 'code' as clean as possible.  It's not a stretch for me to agree that I made it overly complicated.  I think where I might have gone a little too far is the idea that when I get DPL working, and therefore I would have a variable number of rows per table, I needed to segregate the row template from the other templates.

I will read through and make sure I thoroughly understand what you've posted.

Thanks again!

Share this post


Link to post
Share on other sites
  • 0
Guest Rick

Well I don't know what happened.  I entered the code as you described and it worked perfectly as of late this past Wednesday.  I checked with the admin and there have been no updates or extension allied since then.  I asked for a reboot and that was done.  Still fails.

I went to go start implementing the templates and it seems the parser isn't working quite right, or the default template{!}.  There seems to be a problem with the optHeader and maybe the optRow templates (row may be incorrect because columns are incorrect).

Output,  Instead of creating a new column for each parameter sent (I used all 6 for example purposes), the parser is combining columns 2 thru 6 together: Second column header !! scope = "col" | Third column header !! scope - "col" | etc..

optHeader:

{{#if:{{{1|}}}| !! scope="col" {{!}} {{{1}}} }}. 

optRow:

{{#if:{{{1|}}}| {{!}}{{!}} data-sort-value="{{{1}}}" {{!}} [[{{{1}}}]] }}.

Header (part of it)

! scope="col" | {{{1}}} {{Template:Table_optHeader|{{{2|}}}}} {{Template:Table_optHeader|{{{3|}}}}} {{Template:Table_optHeader|{{{4|}}}}} {{Template:Table_optHeader|{{{5|}}}}} {{Template:Table_optHeader|{{{6|}}}}}

Row:

|-
| data-sort-value="{{{1}}}" | [[{{{1}}}]] {{Template:Table_optRow|{{{2|}}}}} {{Template:Table_optRow|{{{3|}}}}} {{Template:Table_optRow|{{{4|}}}}} {{Template:Table_optRow|{{{5|}}}}} {{Template:Table_optRow|{{{6|}}}}}

 

Any thoughts?

Rick

Share this post


Link to post
Share on other sites
  • 0

Take another read through https://www.mediawiki.org/wiki/Help:Tables

For putting multiple columns in a single line, use !! to separate them for header rows and || to separate for normal rows. If you have formatting, match the ! or |.

{| class="wikitable"
|+ Caption
! scope="col" style="color:red" | Column 1 !! scope="col" style="color: blue" | Column 2 !! scope="col" style="color: green" | Column 3
|-
| data-sort-value="R1C1" | [[R1C1]] || data-sort-value="R1C2" | [[R1C2]] || data-sort-value="R1C3" | [[R1C3]]
|-
| data-sort-value="R2C1" | [[R2C1]] || data-sort-value="R2C2" | [[R2C2]] || data-sort-value="R2C3" | [[R2C3]]
|}

Produces the following:

image.png.ea0f630d9bc11bf5631c26632f9ff912.png

 

If that is what you are doing, try doing each column on its own line, or make use of HTML table markup (<table>, <tr>, <th>, and <td>) instead of using wiki markup for tables.

Edited by Skizzerz (see edit history)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.