Content Delivery based on variables

Hello there

Currently I’m using Kirby for a multilingual site.

The content structure:
/content/home/home.en.txt
/content/home/home.de.txt
/content/home/home.fr.txt

Each file with variables like: headline, metadata, text, and more

And for the template:
/site/templates/home.php

Until now all works as it should.

Now our customer wants to show different content on the homepage depending on some “variables”.
My Idea was to change the content structure a little bit:
/content/home/home.en.X.txt
/content/home/home.en.Y.txt

Is it possible to set all meta-information on home.en.txt and all content stuff (like ‘text’) in “home.en.X.txt”?

Thanks for your time!

No, you can only ever have one content file per language.

What sort of information are we talking about? Why would you want to separate meta data from the text in a different file? Could you provide some more information what sort of changes the client wants to have on the homepage depending on what exactly? Only for a single language? For all languages?

IMHO It’s generally a bad idea to serve different content on the same URL…

What is your use case?

In general I agree, but the nature of dynamic pages is that their content changes and usually a homepage is such a dynamic page that must not always show the same stuff?

Normally you have:
home.en.txt and home.de.txt

There will be default variables like “text”, “headline”, “pagetitle”, “metedescription” and so on.
Now you have my “cases”. For exmaple:

If your condition says “x == x” you will take home.en.X.txt and overwrite the default variables fomr “home.en.txt” with those in “home.en.X.txt”.

(default)
home.en.txt:

Text:
This Text is in english.

Our Products

(product: a)
(product: b)
(product: c)

home.de.txt:

Text:
Dieser Text ist in deutsch.

Unsere Produkte

(product: a)
(product: b)
(product: c)

And for other cases:

home.en.X.txt:
Text:
This text is in english.

Our Products

(product: b)
(product: c)
(product: xyz)
Some extra Text for case “x”.

home.de.X.txt:
Dieser Text ist in deutsch.

Unsere Produkte

(product: b)
(product: c)
(product: xyz)
Some extra Text for case “x”.


Hope this gives you more information as my previous post.

Note: It’s not only other content, sometimes it only the order of sentences for each case.

If you structure your content differently, for example using a structure field instead of a text field, then you wouldn’t have the need for different text files. For example, use select fields to select the products to show etc. Use checkboxes to determine a state that you can query in your templates. Use if statements to query a language and show different snippets for each language. There are so many options…

But I still. don’t see what variable X is…

Or, you could use two text fields and then use a checkbox to determine which of the two to use.

X could be:

  • geo location
  • previous checkbox/radio button on a form
  • some other conditions

I was playing around with this idea, but my “Text” variable for one site is around 180 lines of text.
And I think it’s more readable with I could outsource the “Text” in a seperate file for each case.

You could use a tabs plugin or use two subpages instead. As I said, you have to find an alternative, because more than one text file is just not possible.

This is serving different content on the same URL, and is a big no-no to me. You better redirect based on your conditions (set in a controller) to dedicated pages then.

@texnixe, I agree on the dynamic nature of the home page which means it changes over time. Not that it serves different content conditionally.

Or use query strings…

Yes, as that would imply different URL’s :wink:

I realy understand your point.
But I can’t redirect base on my conditions.

So if I can’t use a second content-file (home.en.X.txt) I would go to second text-variable inside my normal home.en.txt.

Why can’t you use query strings/parameters?

Do you have an example?

It would basically be a redirect to the same page with a parameter or query string added, unless you get your params/search strings for free with a GET request from a form anyway.

I would (and @texnixe already suggested this)

  1. create a second page (or a subpage) which holds content unique to “X” (e.g. “/home/x”).
  2. In the home controller I would check condition
  3. If “X” is met, redirect to the same URL, but with a URL parameter (e.g. “/home/cond:x”)
  4. In the same home controller check if there is a “cond” URL param set. If set, prepare the extra variables you need in the template by loading ("/home/x"). If the “cond” URL param isn’t set, load the “default” stuff which is in “home” anyway

Currently I build it with ajax requests, but shouln’t be a problem.

The only problem I imagine is the overload of subfolders for each “condition page”.

It’s choosing between an overload of subfolders, or an overload of if-clauses. It’s a messy request anyway :slight_smile:
Who’s gonna manage the content for those “conditions”? It will probably be messy for them too.

As @bvdputte said, you only need one subfolder for the alternative version. It’s really not much different from your original idea… Unless you need different content for each alternative.

There are more than only one alternative version.
But yeah, thanks a lot!

Now I’m going to introduce myself into controllers.