Jump to content

[solved] Sanitizer Field Question (Basic use) Foreach Loop


PCuser
 Share

Recommended Posts

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

@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.

 

  • Like 2
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...