Get content from more than one page

Hi guys!
Hope you can help me. I want to achieve the following:
I need to email my site to different people. Most content is the same, except for one text. I already set up routes, so “example.org/name1”, “example.org/name2”, etc. will show the same site. I have used the possibility to send custom args with the reroute. Now my idea was to get the text like this:
<?php echo $customarg->text() ?>
This is (of course?) not working. Outsource to a snippet doesn’t work either (If I only use ONE snippet)
I know that it would work if I would use a new snippet for every name – but than I have to do this for like 30 or so. This would be cluttering, also never repeat yourself! :wink:

The folders (name1 with name1.txt, name2 with name2.txt and so on) are invisible by the way.

Maybe there is an even smarter way to achieve what I need?
Thank you very much in advance
Cheers!
kwalx

What is $customarg in your code snippet and where does it come from? To be honest I don’t quite understand your setup, so a few more implementation details would be great.

As I said I used the routing feature in the config.php.
It looks kinda this:
array(
'pattern' => 'name1',
'action' => function() {
$data = array('fullname' => 'The Name of Person', 'mininame' => 'name1');
return array('somewhere', $data); }}

In the template “somewhere” I can now adress $fullname and $mininame as custom variables.
The full name is used to address the viewer (“hello The Name of Person”); where the short name should be used as a variable to get the text from a folder with the same name. This is where I don’t succeed (“get content from more than one page” -> we are on the page “somewhere”, but the URL states page “name1”.)
I hope you can understand it now.
This solution isn’t that great anyway; I’m unhappy with it, because there are now many arrays like this one, just with different names. They point all to the same path…

But as I said: maybe this is all too complicated and there is much nicer way to do want I want.

I think I found the answer to my problem: How to access contents of only one particular folder/page?.

The config is still overcrowded but it works ¯_(ツ)_/¯

In case someone else has a similar problem: You can get the page like this:

$data = array('fullname' => 'The Name of Person', 'namePage' => page('name1'));

The template can then use $namePage->text() to access the data.

An easier solution that doesn’t require a route for every person would be to use URL params like /something/user:name1. Kirby will then automatically load the something page and you can access the value from the URL using param('user'). No routes required. :slight_smile:

PS: Please post your code using Markdown code blocks in the future. They use three backticks at the start and end of the block on separate lines. They will preserve indentation and also support syntax highlighting like in my code block above.

1 Like

Woah, that is something I completey wanted! Good to point that out.
But what do I have to do to make this work? It will give me the error page… :worried:

Alas! Thank you. Completely forgot that. It was too late… :zzz:

Are you on Windows? If so, you need to use semicolons instead of colons: /something/user;name1
If you need to generate these URLs programmatically, it might make sense to use url::paramSeparator(). It will return either ; or : depending on the system.

Please also make sure to validate the value of the user param. Otherwise visitors will be able to use any value (= access any page), which could be a security issue depending on your code.

Aaaah, yes, currently I’m testing my site with xampp…

Where do I use url::paramSeparator()?

You only need that if you generate a URL to a page with a param dynamically. Then you would use it like that:

<?php echo url('something/user' . url::paramSeparator() . $username) ?>

Is there any way to pass the URL param from site do site, so it would be usable on any other page linked?

Do you mean from page to page? You could do this with a lot of effort, but an easier way would probably be to store the username in a session variable (using s::set()). If the param is set, it will overwrite the session variable and then the templates can use the session without having access to the param.

Did it with cookies. Works great. :thumbsup:
Any recommendation for validating the user param fast and safe?

What do you want to validate it against? As there doesn’t seem to be authentication, visitors can easily fake the value if they know the username.