User:Anpang/Directory

currently testing: full guide to making templates

Full Guide to Making Templates

First of all, what are templates? Well, for example, you have 10 pages that use the exact same content, just changed up a bit. Well yes, you can fill them up manually, but 1. takes up more server space and 2. you need more time. The perfect thing to solve that are these things called templates. Templates are usually put in the Template namespace or subpages in the User namespace, however other templates in the (Main) namespace, File namespace, Talk namespaces, etc can also be found, but very rare and not recommended at all.

Creating your first template

Want to create your first template now? Well, of course you'll need a topic first. In this example, I'll make a template that is text "Tigers are animals and they are really scary." Now click here and open it in a new tab. That is your sandbox page. You can test templates in there. If you haven't created the page yet, create it.

For my example, I want a template that says "Tigers are animals and they are really scary.", so in that page, I put in that text. Now I go to another page and put  (replace YOURUSERNAME with your meta username). If things work correctly, the text that you've put in there should be displaying in that page that you've just put the template in.

Parameters

Now, I want my template to not just say "Tigers", I want it to say "Piranha" or "Shark" too. Well yes, again, I could just make seperate templates for all those 3 animals, but that's not very efficient, Introducing, parameters! I can add a parameter like this:

What does that do you might ask? Well, the means a "parameter". A parameter is like a variable. Now, instead of using the template as, it is used like this:. I put "Piranha" in there so it displays piranha. I can put "Shark" in there and it will display as "Shark".

Default values in parameters

In the parameter code, I put "|Tigers". What does that mean? Well, if the user that put the template didn't put any "Piranha" or "Shark" or "Tigers" text, it will result in "Tigers". It's the default value of that parameter.

If I put, it will display Tigers because I set tigers as the default value.

#switch

For example, I want to shorten the usage code of the template a bit. Instead of |Shark, I'd like it to be |s and it will still display Shark. That's very easy! This code does it:

I just need to simply replace the  with that code. The default value is still Tigers and it works!

The code is now this:

By the way, if you think your #switch code is a bit messy and mixed up, you can also do it in multiple lines, something like this:

#ifeq

Another parser function, #ifeq, is used to compare 2 values together and see if it's equal. It's syntax is:

In the example, obviously, Text1 isn't identical to Text2, and the parser function will display "different". But if I change the 2 of the 2nd value to a 1, now the parser function will display "same".

You can also use parameters, something like this:

If the parameter is "p", it will display Piranha. Else, it will display Shark. You can also put ifeqs into other ifeqs, basically making an if else thingy.

See where this is going? You can basically make if else statements telling if something is equal. HOWEVER, did you realize that this is quite similar to #switch. That's why #ifeq isn't commonly used.

#if and #iferror

The #if parser function is even more useless than the #ifeq function. It basically checks if a string exists. It's syntax is like this:

Now for a useful function, #iferror. Some of your parser functions or even just normal text fomatting can go wrong and cause an error. You know, when functions go wrong, they become big bold red text. The way for the page to detect them by themselves is #iferror. Syntax is this:

Substitution

...