I occasionally have texts with dynamic years, e.g:
“Our company has been in existence for 15 years.”
“Max Müller has been working for our company for 6 years.”
The year should adjust automatically every year. And also by month. Because if Max Müller started at the company in August, he must not have been with the company for 6 years since January. A date with month & year must therefore be stored.
But how do I realize this in a text block? Or what other solution would there be for this?
I have currently split such text blocks into 3 parts and (rather awkwardly) glued them together again in the template:
You could use a placeholder with for example the start date inside your text, e.g. {{ 04/1999 }}, then replace such string templates with relative dates when rendering.
I was just about to optimize my question when I got your answer Great!
Thanks for your tip. That sounds great and very logical.
But I’m going to put my brain cells into standby mode for today.
My temporary solution worked, but it was extremely cumbersome and inflexible. And incomplete text appeared at the latest when the search results were displayed.
Now I’ve invested (too) much time in getting the cookbook to work in my project and unfortunately have to capitulate.
“Replacing placeholders in templates”
That worked. But does not correspond to the desired solution after a system-wide replacement.
“Defining the replacements in config.php”
Does not work, the placeholder is not replaced.
“The final plugin”
Also does not work, the placeholder is not replaced.
I copied the code examples 1:1 from the cookbook. However, I was confused by the various options and I don’t know what the output in the template for the “final plugin” looks like. Is
If you prefer a solution that works on every page, without having to call replace() explicitely, you can use a hook instead, so keep the plugin with the toOptions method and the structure field from the example, but add a page.render:after hook:
'page.render:after' => function (string $contentType, string $html) {
if ($contentType !== 'html') {
return $html;
}
return Str::template($html, site()->placeholders()->toOptions());
},
a huge thank you for your great work! I am overwhelmed.
And your supplement (hook solution) came at exactly the right moment because it worked straight away. The input option with the structure field is also an excellent solution.
However, I didn’t just copy and paste everything, but looked at all the functions in detail to understand the process.
I can use the plug-in in all my projects. This is because telephone numbers, company names, company foundation date or similar are used throughout all sections of a website.
You should definitely include this great solution in the cookbook.
I have integrated ‘hooks’ directly into the plug-in to minimize the number of files.
Are there any problems if ‘hooks’ is already present in confiq.php?
It would be great to integrate the tab ‘placeholders’ for site.yml into the plug-in to make it a copy-and-paste solution. But that would be a luxury feature, the programming of which is beyond my knowledge.
I have now integrated your plug-in in 2 projects. It works perfectly!
Only in a third project I get an error message on all pages in the frontend, caused by the following line from the plug-in:
The config.php code is identical for all three projects.
The Kirby 4.0.3 version is also the same.
There are even fewer plugins installed in the third project. I have temporarily removed all the others, that’s not the problem.
Just create it as tab blueprint, like you would a seo tab or so, that you can then include in your site.yml. Like in this example, only register and put the tab blueprint in the plugin.
Regarding the error message, not sure what’s happening. The callback should return a string but seems to return an array for some reason. Are you using empty placeholders {{ }} somewhere by mistake?
I learned a lot this weekend ( thanks to your great help).
I was able to register the blueprint in the plugin.
However, the following page was helpful:
Thanks to the example, it worked straight away.
Back to the error message: As my projects are only minimally different in terms of structure, I suspect the error is in an unspecific place. The full text search for {{ }} did not return any hits.
I cleaned up my project with your cleanup.php script. This was also unsuccessful. I will continue to look for the cause. I’m sure it’s just a trivial thing.
At least, when I put double empty curly braces into a text file, I get the same error message as you do. However, if you get this error message on every single page, it’s very likely something else. Would need proper debugging.
While I continue to look for the cause, I can show you the error report.
Is there anything useful for you?
Whoops\Exception\ErrorException thrown with message "Array to string conversion"
Stacktrace:
#10 Whoops\Exception\ErrorException in /Users/gerrit/Sites/localhost/website.com/kirby/src/Toolkit/Str.php:1290
#9 preg_replace_callback in /Users/gerrit/Sites/localhost/website.com/kirby/src/Toolkit/Str.php:1290
#8 Kirby\Toolkit\Str:template in /Users/gerrit/Sites/localhost/website.com/site/plugins/gbd-placeholders/index.php:44
#7 Kirby\Cms\App:{closure} in [internal]:0
#6 Closure:call in /Users/gerrit/Sites/localhost/website.com/kirby/src/Toolkit/Controller.php:60
#5 Kirby\Toolkit\Controller:call in /Users/gerrit/Sites/localhost/website.com/kirby/src/Cms/Event.php:139
#4 Kirby\Cms\Event:call in /Users/gerrit/Sites/localhost/website.com/kirby/src/Cms/App.php:225
#3 Kirby\Cms\App:apply in /Users/gerrit/Sites/localhost/website.com/kirby/src/Cms/Page.php:1020
#2 Kirby\Cms\Page:render in /Users/gerrit/Sites/localhost/website.com/kirby/src/Cms/App.php:775
#1 Kirby\Cms\App:io in /Users/gerrit/Sites/localhost/website.com/kirby/src/Cms/App.php:1189
#0 Kirby\Cms\App:render in /Users/gerrit/Sites/localhost/website.com/index.php:5
Edit:
I have only used 3 placeholders on the contact page so far. Everything was entered correctly in the .txt. Even if I delete the three placeholders again, the error message is still displayed. And on all pages.
If I remove the placeholder plugin, the page works.
Allegedly the following line from the plugin is the trigger: return Str::template($html, site()->placeholders()->toOptions());
Of course, I also deleted the media and cache folders.
I wish I were in a different time zone where it was still daytime to do something (without a computer). Kirby had me more mesmerized today than the great weather. Luckily the positive results had taken over.
Thank you so much for your great help.
That’s not a given on a Sunday.
Have a great rest of the Sunday.
@texnixe Version 1.1 of the plug-in is in the oven and I’m trying to optimize the logic
I can choose in the select (or toggle) field whether I want to enter a date or text. This means that there is a risk that there is a value in both fields. As the other field is hidden with “when”, this is not even noticeable. However, there is then a problem with the output.
Is it possible to prevent this? Or to display a message when saving that both fields have been filled in?
-
keyheadline: demo-demo
key: demo-demo
datatype: text
date: 2024-01-30
text: >
Hey date, you're in the wrong place. This is my place!
-