PageFactory (Kirby Plugin)

Variables

Within .md source files you can use variables: {{ varName }}.

Note that variables are inherently language aware. They will always render their content according the currently active language.

To override the current language, append the language code after a dot, e.g. {{ varName.de }}.

How to Define Variables?

Variables can be defined in special resource files. Or alternatively in .md source files within the Frontmatter section using YAML notation.

Example: site/custom/variables/custom-vars.yaml

varName:
    de: Irgenwas in Deutsch
    fr: "N'importe quoi en français"
    _:  Whatever in English (resp. default language)

Note Regarding Translation Process

Worth noting is that this way you have all language variants next to each other and not spread over multiple files. Which simplifies the process of translating.

Although close to any form of keywords would work, it is good practice to use unique variable names. E.g. similar to class names, such as my-specific-variable-name. This way, if you lose track you can find the definition of a variable with a simple global search.

How to Invoke a Language?

Use the URL-command ?lang=XY.

Language Selection Menu

PageFactory automatically defines a language selection menu and makes it available as {{ langSelection }} (respectively {{ page.langSelection|raw }} inside Twig templates).

The output: de Deutsch en English fr Français

 

You can modify the language selection menu by defining variables for each lanuage:

{{ pfy-lang-select-XY }}

where XY stands for the language code. That way you could use icons instead of letters, for instance.

→ See chapter Multi-Language Content.

Language Variants

In many languages (other than English) there are sub-variants, notably for formal and informal speech.

PageFactory supports such variants.

Example:

login-email-prompt:
    de: Ihre E-Mail Adresse
    de2: Deine E-Mail Adresse
    en: Your e-mail address

Note:
You only need to supply the variant where it differs from base language.

I.e. if there is no de2 element, PageFactory automatically falls back to using de.
(And if de is missing, PageFactory falls back to _).

To invoke such a language variante, e.g. "informal German", simply open the page as ./?lang=de2.

Where Can a Variable Be Defined?

  • site/custom/variables/*.yaml(.yaml file(s) with self chosen name)
  • site/plugins/pagefactory/variables/*.yaml(system variables)
  • site/plugins/pagefactory-{XY}/variables/*.yaml(variables in PageFactory Extensions)

    (where XY is the name of a PageFactory extension package)

  • Frontmatter → keyword variables:(within any .md source file)
  • (Macro-) Code → PageFactory::$pg->addVariable('varName', 'value');

Nesting

Variables can be nested. So, a variable can contain a variable – which can contain yet another variable or just as well a macro.

Example:

logged-in-as:
    en : 'Logged in as {{ username }}'   // ← "username" define elsewhere 

→ In this example, {{ username }} is dynamically defined by PageFactory depending on who is logged in.

In-Text Assignments

You can assign values to variables within an MD source.

-3.4px

Example:

{{ n=-3.4px }}   // assign value (output hidden via class .pfy-transvar-assigned)

Note:
In-Text assignments must be right aligned on separate lines, with no blanks around =.

Note:
The output of assignments like {{ n=-3.4px }} is hidden by default, but can be made visible by overriding class .pfy-transvar-assigned.

In-Text Operators

You can apply incrementing and decrementing operators to variables.

Say, if you have a variable n = "-3.4px", you can apply operators: ++ and --.

Examples:

{{ n++ }}  → -3.4px   // post-increment
{{ ++n }}  → -1.4px   // pre-increment
{{ n-- }}  → -1.4px   // post-decrement
{{ --n }}  → -3.4px   // pre-decrement

Note:
Non-numeric parts of variable contents are ignored, only the numeric part is modified.

How to See All Defined Variables?

Append ?variables to the URL in the browser to see a list of all currently defined variables.

Hint:

To see your page with raw variable-names rather than translated variables, open it with ?notranslate appended to the URL.

Note: for this to work you need to be working on a localhost or need to be logged in as admin.

Handling of Unknown Variables

If a Variable is unknown, its name is rendered, e.g. X{{ unknown }}X → XunknownX.

Insert a ^ to render an empty string instead, e.g. X{{^ unknown }}X → XX.