Str::slug a project title as a data attribute

This is my second using kirby, I’m very much a noob in php and how kirby functions so forgive me. I need to convert a string into a safe string with no spaces. This my current code:

data-slug="<?php
               $string = '<?= $project->title()->html() ?>';
               echo str::slug($string);?>"

What is the correct way of writing this?

If you wrap the PHP code in single quotes, it won’t get executed:

data-slug="<?php
               $string =  $project->title();
               echo str::slug($string);?>"

(Please note that $string is not really a string this case; $page->title() returns an object of type field, which is converted to a string by the __toString() magic method.)

If you needed $string to be a real string, you could use the value() field method:

 $string =  $project->title()->value();

You don’t need the variable, though:

data-slug="<?php echo str::slug($page->title())?>"
1 Like

Thank you for explaining that to me. Kirby is fantastic

1 Like