Notice: We are aware that many of the Chewiki’s images are still broken. We promise: we will try our best to fix it, but we don't guarantee that the fix will be trivial.
Difference between revisions of "Help:Creating Templates"
TheFXexpert (talk | contribs) |
TheFXexpert (talk | contribs) |
||
Line 66: | Line 66: | ||
==Various Parser Functions== | ==Various Parser Functions== | ||
+ | You can also use programming in a template. | ||
===#if=== | ===#if=== | ||
+ | Basic syntax: | ||
+ | <pre>{{ #if: test string | value if test string is not empty | value if test string is empty }}</pre> | ||
− | + | If you place | |
+ | <pre>{{ #if: {{{test|}}} | Hey, {{{test}}}| }}</pre> | ||
+ | in your template, and when placing a template on a page you put <pre>{{nameoftemplate|test=I liek pie.}}</pre>, you should get this: | ||
Hey, I liek pie. | Hey, I liek pie. | ||
− | + | In this example, when you leave the test parameter empty the template generates nothing. | |
===#ifeq=== | ===#ifeq=== | ||
− | If you put < | + | If you put <pre>{{ #ifeq: 70 | 0070 | Value if equal here | Value if different here }}</pre> on a page, you should get this: |
+ | |||
+ | {{ #ifeq: 70 | 0070 | Value if equal here | Value if different here }} | ||
+ | |||
+ | This function is case sensitive with letters, but not with numbers. | ||
Additionally, you can use #ifeq to check the namespace/pagename/etc. of a page and produce a certain result. Really helpful, for creating a clean-up template ''or revamp your failed Featured Articles idea''. | Additionally, you can use #ifeq to check the namespace/pagename/etc. of a page and produce a certain result. Really helpful, for creating a clean-up template ''or revamp your failed Featured Articles idea''. | ||
− | < | + | <pre>{{ #ifeq: {{NAMESPACE}} | Help | Yep. | Nah, man }}</pre> produces |
+ | |||
+ | {{ #ifeq: {{NAMESPACE}} | Help | Yep. | Nah, man }}. | ||
+ | |||
+ | <pre>{{ #ifeq: {{NAMESPACE}} | MediaWiki | Yep. | Nah, man }}</pre> produces | ||
+ | |||
+ | {{ #ifeq: {{NAMESPACE}} | MediaWiki | Yep. | Nah, man }}. | ||
+ | |||
+ | ===#switch=== | ||
+ | Basic syntax: | ||
+ | <pre>{{ #switch: test | ||
+ | |one = value when test is one | ||
+ | |two = value when test is two | ||
+ | |three = value when test is three | ||
+ | |#default = value if test doesn't match one, two, or three | ||
+ | }} | ||
+ | </pre> | ||
+ | |||
+ | Example: | ||
+ | <pre>{{ #switch: {{NAMESPACE}} | ||
+ | |Talk= this is a talk page | ||
+ | |Help= this is a help page | ||
+ | }}</pre> | ||
+ | |||
+ | Gives you: | ||
+ | |||
+ | this is a help page | ||
− | + | Another good example is located in the [[Template:User|User template]] which [http://youchew.net/wiki/index.php?title=Template:User&action=edit you can view the code of.] | |
===Magic Words=== | ===Magic Words=== | ||
Revision as of 08:23, 27 March 2012
So you wanna help out the Chewiki by creating a template? Templates save you time by not having to update the coding on each and every page, rather being able to change the template itself. With that in mind, here's some tips on template making.
Creating a Template and Embedding it on Pages
You can create a template in the same way you create a regular pages. Just put Template: before the name of the template you want to create.
To place a template on a page, just put {{name of your template here}} on the page you want to place it on, and it should appear there after you save the changes.
Making a Simple Tag
If you want your template to be placed in a box, put this.
<div class="toc">Hi, I'm a template. I liek to eat pie.</div>
And you'll get something like this.
Formatting
Background colors
This will make the background color red.
<div class="toc" style="background: red;">Hi, I'm a template. I liek to eat pie.</div>
HTML hex values can also be used for more customization.
<div class="toc" style="background: #00ff4b;">Hi, I'm a template. I liek to eat pie.</div>
Customize the template with whatever background color and message you need.
Borders
To add a black border put:
<div class="toc" style="border: 1px solid;">Hi, I'm a template. I liek to eat pie.</div>
This will add a thicker blue border:
<div class="toc" style="border: 4px solid blue;">Hi, I'm a template. I liek to eat pie.</div>
Images
To add an image to the side of the tag simply put this:
<div class="toc">[[File:Image.jpg|38px]] Hi, I'm a template. I liek to eat pie.</div>
Parameters
You can add specify parameters to be added to your template.
Anonymous Parameters
To add an anonymous parameter put:
<div class="toc">Hi, I'm a template. I liek to eat {{{1}}}.</div>
It doesn't look like any has happened yet, but if you place this template on a page like this:
{{nameoftemplate|dinner}}
You will get:
Named Parameters
If you would like to be more organized or you have a lot of parameters. Naming your parameters is a good idea. Here is an example:
<div class="toc">Hi, I'm a template. I liek to eat {{{food}}}.</div>
Here is the template placed this template on a page with the food parameter given the value of cheese:
{{nameoftemplate|food=cheese}}
You will get:
Various Parser Functions
You can also use programming in a template.
#if
Basic syntax:
{{ #if: test string | value if test string is not empty | value if test string is empty }}
If you place
{{ #if: {{{test|}}} | Hey, {{{test}}}| }}
in your template, and when placing a template on a page you put
{{nameoftemplate|test=I liek pie.}}
, you should get this:
Hey, I liek pie.
In this example, when you leave the test parameter empty the template generates nothing.
#ifeq
If you put
{{ #ifeq: 70 | 0070 | Value if equal here | Value if different here }}
on a page, you should get this:
Value if equal here
This function is case sensitive with letters, but not with numbers.
Additionally, you can use #ifeq to check the namespace/pagename/etc. of a page and produce a certain result. Really helpful, for creating a clean-up template or revamp your failed Featured Articles idea.
{{ #ifeq: {{NAMESPACE}} | Help | Yep. | Nah, man }}
produces
Yep..
{{ #ifeq: {{NAMESPACE}} | MediaWiki | Yep. | Nah, man }}
produces
Nah, man.
#switch
Basic syntax:
{{ #switch: test |one = value when test is one |two = value when test is two |three = value when test is three |#default = value if test doesn't match one, two, or three }}
Example:
{{ #switch: {{NAMESPACE}} |Talk= this is a talk page |Help= this is a help page }}
Gives you:
this is a help page
Another good example is located in the User template which you can view the code of.
Magic Words
If you place any of these codes on a page, they should yield the following results:
{{PAGENAME}} = Name of the page; in this case, Creating Templates
{{FULLPAGENAME}} = Entire name of the page including the namespace; in this case, Help:Creating Templates
{{NAMESPACE}} = The namespace of this page, which on this page is Help
{{CURRENTYEAR}} = The year it is right now: 2024
{{CURRENTMONTHNAME}} = The current month: December
{{CURRENTMONTH}} = The current month in date form: 12
For more variables, look here!
Additional Info
- Pages can be embedded just like regular templates. Just put the name of the page between the '{{' and '}}' brackets and put a colon before the title.
- Subpages are similar to templates. To create a subpage, just do it as you would create a regular page, but put a / and the name of the subpage you want to create after the main page. Subpages can be embedded like templates too.
- If you need any more help, just look up Wikipedia/MediaWiki articles on templates.