PCuser Posted November 21, 2020 Share Posted November 21, 2020 I'm trying to get a field value and sanitize it before outputting the value. In this case I want to retrieve the page title and then use it as the id for the container element (sanitizing to remove spaces/uppercase). Example (doesn't work): <? foreach($pages->find("template=sportspage, sort=random") as $sportspage) : ?> <div id="<?= $sanitizer->$sportspage->title; ?>"> </div> <? endforeach; ?> I'm not that used to PHP and formatting particularly with foreach loops so the documentation isn't really helping. Thanks Link to comment Share on other sites More sharing options...
psy Posted November 21, 2020 Share Posted November 21, 2020 @PCuser you need to let Sanitizer know what kind of clean-up to do, eg: $sanitizer->pageName($sportspage->title) will convert the title text to lowercase and replace spaces with hyphens to make it suitable for use in a URL (or HTML id attribute). See what other $sanitizer methods are available at https://processwire.com/api/ref/sanitizer/ PS: Page titles can change and also not be unique. I prefer to use a letter first (HTML ids must start with a letter) then use the page ID, eg s1234. Alternatively, you could use the existing page name rather than the title. 2 Link to comment Share on other sites More sharing options...
PCuser Posted November 21, 2020 Author Share Posted November 21, 2020 Thank you @psy!! It seemed redundant to include pageName but that makes sense if it helps with the conversion. I'll keep that in mind with the IDs, in my use case duplicate entries and numerical pages seem unlikely to be problematic but with a large database and for good practice those suggestions are helpful. Link to comment Share on other sites More sharing options...
psy Posted November 21, 2020 Share Posted November 21, 2020 @PCuser there many ways to sanitise data using $sanitizer, eg pageName, digits, text, camelCase, etc, which is why you need to let $sanitizer know which method to use. They're all listed in the documentation. ? 1 Link to comment Share on other sites More sharing options...
psy Posted November 23, 2020 Share Posted November 23, 2020 @PCuser Glad to help. Suggest editing your original post and inserting [solved] to the start of the title ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now